A running total adds up a value row by row, in order: total sales so far, balance after each payment, points collected over time. Interviewers love it because the old way, a subquery that re-adds everything for each row, is slow and clumsy, while the window way is short. If you can write SUM(...) OVER (ORDER BY ...) and explain it, you are ahead of many candidates. We use the sales table with 14 sales between January and April 2024.
A running total in one line
SELECT sale_date, amount,
SUM(amount) OVER (ORDER BY sale_date, id) AS running_total
FROM sales
ORDER BY sale_date, id;
sale_date | amount | running_total
2024-01-05 | 1200 | 1200
2024-01-18 | 800 | 2000
2024-01-25 | 500 | 2500
2024-02-02 | 1500 | 4000
2024-02-14 | 900 | 4900
2024-02-20 | 700 | 5600
2024-02-27 | 400 | 6000
2024-03-03 | 1100 | 7100
2024-03-12 | 600 | 7700
2024-03-19 | 2000 | 9700
2024-03-28 | 300 | 10000
2024-04-04 | 1800 | 11800
2024-04-15 | 1000 | 12800
2024-04-22 | 500 | 13300
SUM(amount) OVER (ORDER BY sale_date, id) means: for each row, add the amount of this row and all the rows before it in that order. The last row shows 13300, which is the sum of the whole table. We ordered by sale_date, id and not by the date only. If two sales happened on the same day, ordering by the date alone would give them the same position, and the running total could jump by both amounts at once. Adding a unique column such as id makes the order exact.
A running total for each person
Add PARTITION BY employee_id to restart the total for each employee. It is the same idea as grouping, except no rows are removed.
SELECT employee_id, sale_date, amount,
SUM(amount) OVER (
PARTITION BY employee_id ORDER BY sale_date
) AS running_total
FROM sales
WHERE employee_id IN (6, 7)
ORDER BY employee_id, sale_date;
employee_id | sale_date | amount | running_total
6 | 2024-01-05 | 1200 | 1200
6 | 2024-02-02 | 1500 | 2700
6 | 2024-02-27 | 400 | 3100
6 | 2024-03-19 | 2000 | 5100
6 | 2024-04-04 | 1800 | 6900
7 | 2024-01-18 | 800 | 800
7 | 2024-02-14 | 900 | 1700
7 | 2024-03-03 | 1100 | 2800
7 | 2024-03-28 | 300 | 3100
7 | 2024-04-22 | 500 | 3600
Employee 6 reaches 6900 after five sales. Employee 7 starts again from their own first sale, 800, and ends at 3600. When an interviewer says "for each customer" or "per user", this is your cue to add PARTITION BY.
A moving average and the frame
A moving average looks only at the last few rows, not at everything so far. Here we want the average of the current sale and the two before it. The part that says which rows to look at is called the frame.
SELECT sale_date, amount,
ROUND(AVG(amount) OVER (
ORDER BY sale_date, id
ROWS BETWEEN 2 PRECEDING AND CURRENT ROW
)) AS avg_last_3
FROM sales
ORDER BY sale_date, id;
sale_date | amount | avg_last_3
2024-01-05 | 1200 | 1200
2024-01-18 | 800 | 1000
2024-01-25 | 500 | 833
2024-02-02 | 1500 | 933
2024-02-14 | 900 | 967
2024-02-20 | 700 | 1033
2024-02-27 | 400 | 667
2024-03-03 | 1100 | 733
2024-03-12 | 600 | 700
2024-03-19 | 2000 | 1233
2024-03-28 | 300 | 967
2024-04-04 | 1800 | 1367
2024-04-15 | 1000 | 1033
2024-04-22 | 500 | 1100
ROWS BETWEEN 2 PRECEDING AND CURRENT ROW means: this row and the two rows before it. For the first row only one row exists, so the average is that row itself. For the third row, the average of 1200, 800 and 500 is 833. From the third row on, every average uses exactly three sales. We rounded the result to keep it readable.
ROWScounts physical rows.RANGEgroups rows with the same sort value together. When there are ties in the sort column, they can give different numbers, so preferROWSwhen you mean a number of rows.- If you write only
ORDER BYinsideOVER, the default frame runs from the first row to the current row, which is why the running total works without a frame clause. - A running total over a whole partition, without ORDER BY, gives the same total on every row. That is how you show a value next to the grand total.
Percent of total
Which share of all sales did each person make? Use SUM(SUM(amount)) OVER (). Read it from the inside: the inner SUM is the normal group total for each person, and the outer SUM ... OVER () adds those totals across all the groups. The empty brackets mean a single window over everything.
SELECT employee_id,
SUM(amount) AS total,
ROUND(100.0 * SUM(amount) / SUM(SUM(amount)) OVER (), 1) AS pct_of_all
FROM sales
GROUP BY employee_id
ORDER BY total DESC;
employee_id | total | pct_of_all
6 | 6900 | 51.9
7 | 3600 | 27.1
8 | 2800 | 21.1
Employee 6 made 51.9 percent of the sales, employee 7 made 27.1 and employee 8 made 21.1. The shares add up to 100, give or take a rounding of 0.1. We multiplied by 100.0, with a decimal point, because dividing two whole numbers in many databases gives a whole number and the answer would be 0. Lesson 11 explains this trap.
The old way, and why it is worse
Before window functions, people wrote a subquery in the select list that summed every row up to the current date. It repeats work for each row, so it slows down as the table grows, and it is harder to read. If an interviewer says "without window functions", you can still do it that way, but start by saying that window functions are the better tool.
Common mistakes
- Forgetting the
ORDER BYinOVER, which gives the grand total on every row instead of a running one. - Ordering by a column with ties and getting jumpy totals.
- Using
GROUP BYand expecting a running total. Grouping collapses rows, windows keep them. - Integer division in the percent calculation.
Your task
- Add a running total of the amounts, restarting for each month. (Hint: partition by the month text from
strftime('%Y-%m', sale_date).) - Make a moving average of the last two sales only.
- Show each sale as a percent of its employee's total.
- Show the biggest single sale for each employee next to every sale, using
MAX(amount) OVER (PARTITION BY employee_id).
How to check it worked
- The final running total is 13300 and equals
SELECT SUM(amount) FROM sales. - Employee 7's running total starts at 800 and ends at 3600.
- The shares of the three sellers add up to about 100 (rounding can show 100.1).