Running Totals & Moving Averages in SQL
Aggregate over a moving frame of rows
Aggregate windows like SUM() OVER (…) compute totals across an ordered frame of rows instead of one final number. SUM(salary) OVER (ORDER BY id) produces a running total that grows row by row; adding ROWS BETWEEN 2 PRECEDING AND CURRENT ROW turns it into a 3-row moving average. This is the SQL way to do cumulative and rolling metrics without pulling data into a spreadsheet.
Define the row sequence
OVER (ORDER BY id) — the frame slides along id order; without ORDER BY the whole partition is one frame and every row sees the same total.
Default frame = running
With ORDER BY, the default frame is RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW — everything from the start up to here.
Shrink the frame for moving windows
ROWS BETWEEN 2 PRECEDING AND CURRENT ROW — at most three rows: the current one and its two predecessors.
The engine streams the frame
One sort, then at each row add the entering row and drop the leaving one — O(n) after the sort, not O(n × frame).
Partition restarts the tape
SUM(…) OVER (PARTITION BY department ORDER BY id) — running totals that reset per department.
1SELECT name, salary,2 SUM(salary) OVER (ORDER BY id) AS running_total,3 AVG(salary) OVER (ORDER BY id ROWS BETWEEN 2 PRECEDING AND CURRENT ROW) AS moving_avg4FROM employees ORDER BY id;
| name | salary | running_total | moving_avg |
|---|---|---|---|
| Alice | 85000 | 85000 | 85000 |
| Bob | 52000 | 137000 | 68500 |
| Cynthia | 91000 | 228000 | 76000 |
| David | 47000 | 275000 | 63333.33 |
| Elena | 58000 | 333000 | 65333.33 |
| Farid | 73000 | 406000 | 59333.33 |
running_total accumulates salaries in id order: 85k, +52k = 137k, +91k = 228k… ending at the 406k payroll. moving_avg averages each row with its two predecessors — by Farid the early big salaries have slid out of the frame, so it dips to 59,333.33.
Running payroll total
Cumulative SUM in id order.
1SELECT name, salary,2 SUM(salary) OVER (ORDER BY id) AS running_total3FROM employees ORDER BY id;
| name | salary | running_total |
|---|---|---|
| Alice | 85000 | 85000 |
| Bob | 52000 | 137000 |
| Cynthia | 91000 | 228000 |
| David | 47000 | 275000 |
| Elena | 58000 | 333000 |
| Farid | 73000 | 406000 |
3-row moving average
The frame limits how far back the aggregate sees.
1SELECT name, salary,2 AVG(salary) OVER (ORDER BY id ROWS BETWEEN 2 PRECEDING AND CURRENT ROW) AS moving_avg3FROM employees ORDER BY id;
| name | salary | moving_avg |
|---|---|---|
| Alice | 85000 | 85000 |
| Bob | 52000 | 68500 |
| Cynthia | 91000 | 76000 |
| David | 47000 | 63333.33 |
| Elena | 58000 | 65333.33 |
| Farid | 73000 | 59333.33 |
Running total per department
PARTITION BY restarts the accumulation.
1SELECT name, department, salary,2 SUM(salary) OVER (PARTITION BY department ORDER BY id) AS dept_running3FROM employees WHERE department IN ('Engineering', 'Sales') ORDER BY department, id;
| name | department | salary | dept_running |
|---|---|---|---|
| Alice | Engineering | 85000 | 85000 |
| Cynthia | Engineering | 91000 | 176000 |
| Farid | Engineering | 73000 | 249000 |
| Bob | Sales | 52000 | 52000 |
| Elena | Sales | 58000 | 110000 |
ROWS vs RANGE — the duplicate trap
RANGE groups peers; ROWS counts physical rows.
1SELECT salary,2 COUNT(*) OVER (ORDER BY salary ROWS BETWEEN 1 PRECEDING AND CURRENT ROW) AS rows_frame,3 COUNT(*) OVER (ORDER BY salary RANGE BETWEEN 1 PRECEDING AND CURRENT ROW) AS range_frame4FROM employees WHERE department = 'Engineering' ORDER BY salary;5-- Engineering salaries: 73000, 85000, 91000 (no dupes here; with 85000 twice the two frames diverge)
| salary | rows_frame | range_frame |
|---|---|---|
| 73000 | 1 | 1 |
| 85000 | 2 | 2 |
| 91000 | 2 | 2 |
Recommended resourcesAffiliate links · may earn us a commission
Recommended for learners
Practice what you learned
Take the queries you just visualized and see the data structures that power real databases.