Running Totals

SQL · Extra Concepts

Theory

A running total (cumulative sum) adds each row's value to the total of all rows before it, in a chosen order. For daily sales of ₹1,200, ₹850 and ₹2,300, the running totals are ₹1,200, ₹2,050 and ₹4,350. It answers questions like 'how much revenue had we made by each day of the month?'.

In modern SQL you write it with a window function: SUM(amount) OVER (ORDER BY order_date). The ORDER BY inside OVER defines the sequence, and every row keeps its own detail while gaining a cumulative column; nothing is collapsed like with GROUP BY. Add PARTITION BY customer_id to restart the total for each customer. One subtle point: when ORDER BY is present, the default frame is RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW, which treats rows with the same order value as peers and gives them the same total. If you need strictly row-by-row totals, order by a unique column or write ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW explicitly.

Why we use it

Cumulative figures show progress over time: revenue to date, stock levels, account balances, progress toward a target. A window function computes them in one pass without self-joins.

When to use it

Use SUM() OVER (ORDER BY ...) for to-date totals, balances and cumulative counts. Add PARTITION BY to restart per group. Use a moving average or a fixed ROWS frame when you need a rolling window rather than everything so far.

How it works

  1. Choose the order: ORDER BY order_date
  2. Restart per group? Yes: Add PARTITION BY customer_id. No: One total for all rows.
  3. For each row, add its amount to the total of rows before it
  4. Ties in the ORDER BY value? Yes: Use a ROWS frame or tie-breaker. No: Default frame is fine.
  5. Each row shows its own cumulative total

Syntax

SUM(column) OVER (ORDER BY col)

Example

SELECT name, salary,
  SUM(salary) OVER (ORDER BY id) AS running_total
FROM employees;

Key points

SQL topics

Introduction to SQL

Querying Data

Sorting Rows

Limiting Rows

Filtering Rows

Joining Multiple Tables

Grouping Rows

SET Operators

Working with Tables

Modifying Data

Views & Triggers

Conditional Expressions

SQL Subqueries

Aggregate Functions

Window Functions

String Functions

Date Functions

Math Functions

Extra Concepts

T
Tech Town
Log InSign Up Free
Home SQL Python DSA Power BI Snowflake Databricks Spark Gen AI Practice Problems Contest Blog

Master SQL from Zero to Pro

All SQL topics in one place. Click any topic in the sidebar — content opens right here without leaving the page.

{{ sectionCount }}
Sections
{{ topicCount }}
Topics
Free
Forever
Hands-on practice Practice SQL Online Solve 150 SQL interview problems in a live editor with instant checking. Start practising →
Section {{ sec.num }}

{{ sec.label }}

SQL  /  {{ current.sectionLabel }}  /  {{ current.name }}

{{ current.name }}

Theory

{{ current.def }}

Why we use it

{{ rich.why }}

When to use it

{{ rich.when }}

Diagram

{{ rich.dg.caption }}

{{ tb.title }}
{{ c.v }}
{{ rich.dg.op }}
➜
{{ rich.dg.output.title }}
{{ c.v }}
{{ nd.label }}
{{ nd.sub }}
{{ nd.label }}
{{ nd.sub }}
{{ rich.dg.left.title }}
{{ it }}
{{ rich.dg.right.title }}
{{ it }}
Flowchart — how it works
{{ nd.text }}
{{ nd.text }}
◇ {{ nd.text }}
Yes → {{ nd.yes }}
No → {{ nd.no }}
Syntax
{{ current.syntax }}
Example
{{ current.example }}
▶ Try It Live — runs against a real sample database
{{ liveError }}
{{ col }}
{{ cell }}
Key Points — click to check off as you learn
  • {{ pt.mark }} {{ pt.text }}

Share this topic