7-Day Rolling Sales by Product

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

Problem

A merchandising team tracks each product's sales on a rolling 7-day total:

next to every day, the sum of that day and the six trading days before it, for

that product alone. A single day is noise; a week is what they reorder on.

Engine: queries run on MySQL 8.0, so use MySQL-compatible syntax and functions.

Tables

product_sales — one row per product and day.

| column | type | description |
| --- | --- | --- |
| product_id | INT | the product |
| sale_date | DATE | the day the sales were recorded |
| sales | INT | units sold that day |

Return every row of product_sales, with one extra column:

before it**, counted within the same product_id

Sort the result by product_id, 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 product 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 product is counted on its own. Two products selling on the same dates have

two independent running totals, and they never mix — the window restarts at

every new product_id.

Where fewer than seven rows exist so far, sum the rows that do exist: the first

row of a product 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

product_sales

| product_id | sale_date | sales |
| --- | --- | --- |
| 1 | 2024-01-01 | 10 |
| 1 | 2024-01-02 | 20 |
| 1 | 2024-01-03 | 30 |
| 1 | 2024-01-04 | 40 |
| 1 | 2024-01-05 | 50 |
| 1 | 2024-01-06 | 60 |
| 1 | 2024-01-07 | 70 |
| 1 | 2024-01-08 | 80 |
| 2 | 2024-01-01 | 100 |
| 2 | 2024-01-02 | 50 |
| 2 | 2024-01-03 | 70 |

Expected output

| product_id | sale_date | sales | rolling_7day_sum |
| --- | --- | --- | --- |
| 1 | 2024-01-01 | 10 | 10 |
| 1 | 2024-01-02 | 20 | 30 |
| 1 | 2024-01-03 | 30 | 60 |
| 1 | 2024-01-04 | 40 | 100 |
| 1 | 2024-01-05 | 50 | 150 |
| 1 | 2024-01-06 | 60 | 210 |
| 1 | 2024-01-07 | 70 | 280 |
| 1 | 2024-01-08 | 80 | 350 |
| 2 | 2024-01-01 | 100 | 100 |
| 2 | 2024-01-02 | 50 | 150 |
| 2 | 2024-01-03 | 70 | 220 |

Why product 1 ends at 350, not 360. By 2024-01-08 it has eight rows, one

more than the window holds, so the oldest day leaves:

20+30+40+50+60+70+80 = 350. A total that never forgets would report 360

that is the difference between a rolling sum and a running one.

Why product 2 starts again at 100. It trades on the same dates as product 1,

but it is a different product, so its window begins from scratch rather than

continuing product 1's numbers.

What this SQL challenge teaches you

“7-Day Rolling Sales by Product” 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:

  1. 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.
  2. 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.
  3. PARTITION BY product_id restarts the window at every product, so one product's history never leaks into another's.
  4. 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.
  5. ORDER BY sale_date inside the OVER clause is what makes "preceding" mean earlier, rather than whatever order the rows happen to be stored in.

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

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.

Solve this challenge free on PySpark.in