Lesson 20 — Running total
Running totals with SUM() OVER
A running total (cumulative sum) shows, for each row, the sum of everything up to and including it. The window function SUM(x) OVER (ORDER BY y) does exactly that: it adds up all rows that come before in the given order.
SELECT day, amount, SUM(amount) OVER (ORDER BY day) AS so_far FROM sales
Why it matters: "revenue so far this month" and bank balance histories are running totals.
Make balance a running total of amount, in day order. Expected balances: 100, 70, 120, 100.
Run your code — when the output matches the task, you will see a green check. Then mark the lesson complete.
A quick 3-question quiz on this lesson — instant feedback.
Ready for more? Optional paid resources that pick up where this lesson leaves off.
Some links on this page are affiliate links: we may earn a commission at no extra cost to you. As an Amazon Associate I earn from qualifying purchases.
Project-based SQL Deep Dive courses if you want a structured paid path after the free lessons. Wait for a sale — prices drop often.
Browse on Udemy →Search Amazon for SQL Deep Dive books — check the publication year so the examples match current versions.
Search Amazon →