7-Day Rolling Sales by Region
SQL coding challenge · Difficulty: medium · Topic: Window Functions · +150 XP
Problem
A regional manager reviews trade on a rolling 7-day total: next to every
day, the sum of that day and the six trading days before it, for that region
alone. One day is noise; a week is what the regional forecast is built on.
Engine: queries run on MySQL 8.0, so use MySQL-compatible syntax and functions.
Tables
regional_sales — one row per region and day.
| column | type | description | | --- | --- | --- | | region | VARCHAR | the sales region | | sale_date | DATE | the day the sales were recorded | | sales | INT | sales taken that day |
Return every row of regional_sales, with one extra column:
- rolling_7day_sum — the total
salesover the current row and the **6 rows
before it**, counted within the same region
Sort the result by region, then sale_date — both ascending.
What "7-day" means here
The window is 7 rows, not 7 calendar days: the current row plus the six rows
preceding it in date order. If a region has no row for a date, that date is
simply not there — the window reaches back seven *recorded* days however far
apart they fall on the calendar.
Each region is counted on its own. Two regions trading on the same dates have
two independent running totals, and they never mix — the window restarts at
every new region.
Where fewer than seven rows exist so far, sum the rows that do exist: the first
row of a region is always just its own sales. From the eighth row onward the
window is full and the oldest day starts dropping out, which is where a rolling
total stops matching a running total that never forgets.
Example input
regional_sales
| region | sale_date | sales | | --- | --- | --- | | East | 2024-01-01 | 100 | | East | 2024-01-02 | 200 | | East | 2024-01-03 | 150 | | East | 2024-01-04 | 120 | | East | 2024-01-05 | 180 | | East | 2024-01-06 | 90 | | East | 2024-01-07 | 210 | | East | 2024-01-08 | 60 | | West | 2024-01-01 | 300 | | West | 2024-01-02 | 100 | | West | 2024-01-03 | 250 |
Expected output
| region | sale_date | sales | rolling_7day_sum | | --- | --- | --- | --- | | East | 2024-01-01 | 100 | 100 | | East | 2024-01-02 | 200 | 300 | | East | 2024-01-03 | 150 | 450 | | East | 2024-01-04 | 120 | 570 | | East | 2024-01-05 | 180 | 750 | | East | 2024-01-06 | 90 | 840 | | East | 2024-01-07 | 210 | 1050 | | East | 2024-01-08 | 60 | 1010 | | West | 2024-01-01 | 300 | 300 | | West | 2024-01-02 | 100 | 400 | | West | 2024-01-03 | 250 | 650 |
Watch East's last row — it goes down. By 2024-01-08 the region has eight
rows, one more than the window holds, so the oldest day leaves as the newest
arrives: 1050 - 100 + 60 = 1010. A total that never forgets would report
1110 and keep climbing. A rolling total can fall, and that is the whole point
of it.
West starts again at 300. It trades on the same dates as East, but it is a
different region, so its window begins from scratch rather than continuing
East's numbers.
What this SQL challenge teaches you
“7-Day Rolling Sales by Region” 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 running total that never forgets. This task needs a frame that does.
- PARTITION BY region restarts the window at every region, so one region's history never leaks into another's.
- The frame is seven rows counting the current one, so the offset is 6, not 7. Use ROWS, not RANGE with a day interval: the window reaches back seven recorded rows, and calendar gaps do not shrink it.
- A good check: once a region has more than seven rows, its rolling total can go DOWN as well as up. If yours only ever climbs, you have a running total rather than a rolling one.
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.