BeginnerCoder

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.

Want it another way?
Task

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.

← Back
Next →
Test your knowledge

A quick 3-question quiz on this lesson — instant feedback.

Enjoying this lesson? Share it.

Keep going with SQL Deep Dive

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.