SQL reference

Running total (cumulative sum) in SQL

In Excel, you'd reach for
fx=SUM($B$2:B2) dragged down the column

A running total adds each row to everything before it: cumulative signups, revenue year to date, units shipped so far. In Excel it's the formula with one end of the range locked, dragged down. In SQL it's one window function.

Syntax

SUM(value) OVER (ORDER BY date_col)
Try itEdit the query, then Run
monthsignupstotal_customers
2026-014848
2026-0252100
2026-0375175
2026-0465240
2026-0596336
2026-0684420

6 rows

A running total per group (say, per region) restarts with PARTITION BY: SUM(units) OVER (PARTITION BY region ORDER BY month).

If several rows share the same date, the default window adds all of them at once. Add a tie-breaker (ORDER BY date, id) or ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW to step row by row.

Your turn

Cumulative marketing spend by month: month, monthly spend, and spend_to_date, in month order.

This page on one card

Running total (cumulative sum) in SQL: the Excel way (=SUM($B$2:B2) dragged down the column) and the SQL query, side by side
Running total (cumulative sum) in SQL, on one card: the Excel way and the SQL. Download · Save to Pinterest
The practice data (7 tables)

See every column and sample rows