SQL Topic
Running Totals
A running total accumulates a value across ordered rows with SUM() OVER (... ORDER BY ... ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW). Rolling averages are the same pattern with AVG and a bounded frame.
When is Running Totals used?
Cumulative sums per account, rolling averages, percentage-of-total-with-partition computations.
Core syntax
SUM(amount) OVER (PARTITION BY account_id ORDER BY sale_date ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW).Common mistakes
- Omitting the frame so the default (up to current row) surprises you.
- Forgetting PARTITION BY, mixing accounts together.
- Using a non-deterministic order that changes the running value.
Practice questions
No dedicated practice questions yet — explore the related topics and the full question catalogue.