BBampaa

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 →
About 5–10 minutes · no sign-up needed

Example

A running total, day by day

SELECT sale_date,
       revenue,
       SUM(revenue) OVER (ORDER BY sale_date) AS running_total
FROM daily_sales

Result:

sale_daterevenuerunning_total
2026-03-01420420
2026-03-02380800
2026-03-035101310

Step by step

  1. One row per day.
    SELECT sale_date, revenue, SUM(revenue) OVER (ORDER BY sale_date) AS running_total FROM daily_sales
  2. One row per day already.
    SELECT sale_date, revenue, SUM(revenue) OVER (ORDER BY sale_date) AS running_total FROM daily_sales
  3. Every day counts.
    SELECT sale_date, revenue, SUM(revenue) OVER (ORDER BY sale_date) AS running_total FROM daily_sales
  4. 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

Try each one in your head first, then open the answer.

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_sales
2. 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_sales
4. 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 1
5. 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 orders

Learn 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 →
About 5–10 minutes · no sign-up needed