Running Total of Daily Sales
Medium
SQLWindowSUM
Problem
Schema: Sales(sale_date DATE, amount DECIMAL). One row per day.
Return sale_date, amount, and running_total — the cumulative sum of amounts from the first day through that day.
Example 1
Input: (Jan 1, 100) (Jan 2, 50) (Jan 3, 200)
Output: (Jan 1, 100, 100) (Jan 2, 50, 150) (Jan 3, 200, 350)
Constraints
- Dates are unique.
Approach — Core Patterns
This is a Core Patterns problem. The idea: pick the data structure best matched to the constraints and solve it in one clean pass. Work through the reference code below line by line, then re-derive it yourself in the editor — that's how the pattern sticks.
Complexity: see the walkthrough below.
Solution code
SQL
SELECT sale_date, amount,
SUM(amount) OVER (ORDER BY sale_date
ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) AS running_total
FROM Sales
ORDER BY sale_date;
Practice it
Reading a solution isn't the same as being able to write it under pressure. Open this problem in the in-browser editor, hide the solution, and solve it from scratch — your code runs against real test cases instantly.
Solve Running Total of Daily Sales interactively → ← All solutions