Rolling 12-Month Revenue per Month
SQL coding challenge · Difficulty: medium · Topic: Window Functions · +200 XP
Problem
Finance reports revenue on a rolling 12-month basis: next to each month, the
total of that month and the eleven months of trading before it. A single month
swings with seasonality; a trailing year is what the board is shown.
Engine: queries run on MySQL 8.0, so use MySQL-compatible syntax and functions.
Tables
monthly_revenue — one row per month.
| column | type | description | | --- | --- | --- | | month_start | DATE | first day of the month | | revenue | INT | revenue booked that month |
Return every row of monthly_revenue, with one extra column:
- rolling_12month_revenue — the total
revenueover the current row and the
11 rows before it, in date order
Sort the result by month_start ascending.
What the window does, and when you can see it
The frame is 12 rows, not 12 calendar months: the current row plus the
eleven recorded rows preceding it. If a month has no row, it is simply not there
— the window reaches back twelve *recorded* months however far apart they fall
on the calendar.
Two consequences worth being precise about:
- Before the 12th row, nothing has dropped out yet. For the first eleven
rows the window holds everything so far, so the figure looks exactly like a
plain cumulative total. That is not a shortcut — it is the same window, simply
not yet full.
- From the 12th row onward the window slides, and the oldest month leaves
it. This is where a rolling total stops matching a cumulative one. A query
with no frame at all keeps adding forever and will agree with the correct
answer right up until the 13th row, then diverge from it on every row after.
The worked example below is only six months long, so it falls entirely in the
first case. The graded tests include longer histories where the window is full
and moving.
Example input
monthly_revenue
| month_start | revenue | | --- | --- | | 2024-01-01 | 500 | | 2024-02-01 | 700 | | 2024-03-01 | 600 | | 2024-04-01 | 900 | | 2024-05-01 | 800 | | 2024-06-01 | 1000 |
Expected output
| month_start | revenue | rolling_12month_revenue | | --- | --- | --- | | 2024-01-01 | 500 | 500 | | 2024-02-01 | 700 | 1200 | | 2024-03-01 | 600 | 1800 | | 2024-04-01 | 900 | 2700 | | 2024-05-01 | 800 | 3500 | | 2024-06-01 | 1000 | 4500 |
Each figure is the sum of every month up to and including that row, because six
months is fewer than twelve and the window has not yet had to discard anything.
On a history longer than twelve rows these numbers would stop growing
indefinitely and begin to reflect only the most recent twelve months.
What this SQL challenge teaches you
“Rolling 12-Month Revenue per Month” is a medium-level SQL challenge focused on Window Functions. Working through it gives you hands-on practice with Window Functions — 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.
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:
- You need a total that moves with each row while keeping every row in the result. That is a window function, not a GROUP BY.
- SUM(col) OVER (...) with no frame clause sums everything up to the current row -- a cumulative total that never forgets. This task needs a frame that does forget.
- The frame is twelve rows counting the current one, so the offset is 11, not 12. Use ROWS, not RANGE with a month interval: the window reaches back twelve recorded rows, and missing months do not shrink it.
- ORDER BY month_start inside the OVER clause is what makes "preceding" mean earlier, rather than whatever order the rows happen to be stored in.
- Test on a history longer than twelve rows. On a short table a cumulative total and a rolling one look identical -- they only diverge once the window is full and starts dropping the oldest month.
Where this comes up
Variations of this problem have been reported in interviews at Amazon. Interviewers use it to check whether you can express the logic cleanly and reason about correctness on edge cases such as ties, nulls and empty groups.
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
- Top 3 Products per Category
- Running Total Revenue
- Median Salary per Department
- Latest Order Per Customer
- 3-Day Rolling Sum of Sales
- 7-Day Rolling Purchase Amount by Customer
Helpful resources
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.