12-Month Rolling Revenue

SQL coding challenge · Difficulty: medium · Topic: Window Functions · +100 XP

Problem

Finance reports revenue on a trailing twelve month basis: for each month, the sum of that month and the eleven before it.

Schema — `monthly_revenue`

| Column | Type |
| --- | --- |
| month_start | DATE |
| revenue | INT |

Example Input — `monthly_revenue`

| month_start | revenue |
| --- | --- |
| 2023-01-01 | 100 |
| 2023-02-01 | 120 |
| 2023-03-01 | 110 |
| 2023-04-01 | 130 |
| 2023-05-01 | 150 |
| 2023-06-01 | 140 |
| 2023-07-01 | 160 |
| 2023-08-01 | 170 |
| 2023-09-01 | 155 |
| 2023-10-01 | 180 |
| 2023-11-01 | 190 |
| 2023-12-01 | 200 |
| 2024-01-01 | 210 |
| 2024-02-01 | 220 |

Expected Output

| month_start | revenue | rolling_12month_revenue |
| --- | --- | --- |
| 2023-01-01 | 100 | 100 |
| 2023-02-01 | 120 | 220 |
| 2023-03-01 | 110 | 330 |
| 2023-04-01 | 130 | 460 |
| 2023-05-01 | 150 | 610 |
| 2023-06-01 | 140 | 750 |
| 2023-07-01 | 160 | 910 |
| 2023-08-01 | 170 | 1080 |
| 2023-09-01 | 155 | 1235 |
| 2023-10-01 | 180 | 1415 |
| 2023-11-01 | 190 | 1605 |
| 2023-12-01 | 200 | 1805 |
| 2024-01-01 | 210 | 1915 |
| 2024-02-01 | 220 | 2015 |

Explanation

The first eleven months do not have twelve months of history behind them, so they sum however many exist — month 1 sums 1 value, month 2 sums 2. Only from the twelfth month onward is it a true 12-month total, and from month 13 the window starts dropping the oldest month as it gains a new one.

Notes

What this SQL challenge teaches you

“12-Month Rolling Revenue” is a medium-level SQL challenge focused on Window Functions. Working through it gives you hands-on practice with SUM, OVER, ROWS BETWEEN, Rolling Window — the kind of transformation you are asked to write in real data engineering work and in technical interviews. You can solve it directly in the browser: the dataset is pre-loaded, so you write the query or DataFrame code, run it, and compare your output against the expected result immediately.

Concepts covered

How to approach it

If you get stuck, work through these steps in order before looking at a full solution — each one narrows the problem down:

  1. ROWS BETWEEN 11 PRECEDING AND CURRENT ROW is a 12-row window.
  2. Without the ROWS clause you get a running total that never drops a month.
  3. Check row 13: it should have dropped the first month.

How to practise it on PySpark.in

Open the challenge, write your SQL query in the editor and press Run to execute it against the sample dataset. Submitting checks your output against every test case, including hidden ones, so you find out straight away whether your logic holds up. You can retry as often as you like, and each solved challenge adds to your XP.

Related SQL challenges

Frequently asked questions

Do I need to install Spark or a database to solve this?

No. The SQL environment runs in your browser with the sample data already loaded, so there is nothing to install or configure.

Is this challenge free?

Yes - the problem, the sample dataset, the hints and unlimited test runs are free.

What level is it?

It is rated medium and covers Window Functions.

Solve this challenge free on PySpark.in