Lessons › SQL › Go further in SQL: Business questions
SQL lesson
How does revenue build up through March?
How does revenue build up through March, day by day? Each day's row shows that day's revenue and the total so far. SUM OVER (ORDER BY the date) keeps adding as it goes.
Try this lesson free →Example
A running total, day by day
SELECT sale_date,
revenue,
SUM(revenue) OVER (ORDER BY sale_date) AS running_total
FROM daily_sales| sale_date | revenue | running_total |
|---|---|---|
| 2026-03-01 | 420 | 420 |
| 2026-03-02 | 380 | 800 |
| 2026-03-03 | 510 | 1310 |
Step by step
- One row per day.
SELECT sale_date, revenue, SUM(revenue) OVER (ORDER BY sale_date) AS running_total FROM daily_sales - One row per day already.
SELECT sale_date, revenue, SUM(revenue) OVER (ORDER BY sale_date) AS running_total FROM daily_sales - Every day counts.
SELECT sale_date, revenue, SUM(revenue) OVER (ORDER BY sale_date) AS running_total FROM daily_sales - SUM(revenue) OVER (ORDER BY sale_date).
SELECT sale_date, revenue, SUM(revenue) OVER (ORDER BY sale_date) AS running_total FROM daily_sales
Practice
1. How does revenue build up through March, day by day? Show sale_date, revenue and running_total.
SELECT sale_date, revenue, SUM(revenue) OVER (ORDER BY sale_date) AS running_total FROM daily_sales2. Show how revenue builds up through the first week of March. Show sale_date, revenue and running_total.
SELECT sale_date, revenue, SUM(revenue) OVER (ORDER BY sale_date) AS running_total FROM daily_sales WHERE sale_date <= '2026-03-07'3. Show each day's revenue next to March's total so far: sale_date, revenue and running_total.
SELECT sale_date, revenue, SUM(revenue) OVER (ORDER BY sale_date) AS running_total FROM daily_sales4. On which day did March's running total first pass 2,000? Show sale_date and running_total.
WITH r AS (SELECT sale_date, SUM(revenue) OVER (ORDER BY sale_date) AS running_total FROM daily_sales) SELECT sale_date, running_total FROM r WHERE running_total > 2000 ORDER BY sale_date LIMIT 15. Show each store's running revenue through the orders, in date order. Show store, order_date, revenue and running_total.
SELECT store, order_date, revenue, SUM(revenue) OVER (PARTITION BY store ORDER BY order_date) AS running_total FROM ordersLearn it by doing. This lesson is a short hands-on level on Bampaa: see it work, then build and write it yourself, with instant feedback.
Try this lesson free →