Intermediate Case 24 of 50

Case 024: The Running Total

customersorderspayments

Finance wants to see how each customer's spending actually built up over time — not just where they landed, but the running story behind it. The manager brings the request to Alex.

"Could you show me each customer's spending adding up as it happens, order by order? I don't just want the final total — I want to see it grow."

The data Alex is looking at

customers
idnamecountrysignup_date
1SamGermany2026-01-02
2NinaCanada2026-01-05
3DavidGermany2026-01-10
4MayaJapan2026-01-15
5DanielCanada2026-01-20
… 5 more rows
orders
idcustomer_idorder_date
10112026-01-05
10222026-01-08
10332026-01-12
10412026-01-20
10542026-02-01
… 23 more rows
payments
idorder_idamount
6011016200
6021023500
6031033600
60410460000
60510512000
… 23 more rows
TASK

Help Alex answer the manager:

“For each customer, show every order's date, amount, and running total of spending up to that point, in date order.”
SQL editor Ctrl+Enter to run
Write a query above and hit Run to see what comes back.
No hints requested yet — click "Get a hint" when you're stuck.
Alex's final query — this is for reference. Copy it into your own thinking, not into the editor above.
SELECT customers.name, orders.order_date, payments.amount, SUM(payments.amount) OVER ( PARTITION BY customers.id ORDER BY orders.order_date ) AS running_total FROM customers JOIN orders ON customers.id = orders.customer_id JOIN payments ON payments.order_id = orders.id ORDER BY customers.name, orders.order_date;
What that query returns
Case Debrief SUM() OVER (window running total)
  • Adding ORDER BY inside a window turns a flat SUM() into a running total — the same function, two very different behaviors.

Case closed.

Ready for the next one?

Next: Case 025 — The Tie Breaker →