7-Day Rolling Sum of Positive Sales Only

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

Problem

Sales are recorded net of refunds, so a bad day can come back negative.

Finance wants a rolling 7-day view of takings only — what actually came in

over the last seven trading days, with refund days contributing nothing rather

than cancelling out earlier sales.

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

Tables

daily_sales — one row per day.

| column | type | description |
| --- | --- | --- |
| sale_date | DATE | the day the sales were recorded |
| sales | INT | net sales that day; negative on a refund day |

Return every row of daily_sales, with one extra column:

the total of the sales values that are greater than zero

Sort the result by sale_date ascending.

Refund days count as zero, they do not subtract

A negative day contributes 0 to the total — it is not added, and it is not

subtracted either. Two consequences the tests check:

adding it would change nothing anyway.

not a negative number.

The sales column itself is always reported as recorded, negatives included.

Only the rolling column is filtered.

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. A refund day still occupies a row in the window — it

just adds nothing. If a date has no row at all, it is simply not there, and the

window reaches back seven *recorded* days however far apart they fall.

From the eighth row onward the window is full and the oldest day drops out,

which is where the total can fall.

Example input

daily_sales

| sale_date | sales |
| --- | --- |
| 2024-01-01 | 100 |
| 2024-01-02 | -50 |
| 2024-01-03 | 200 |
| 2024-01-04 | -30 |
| 2024-01-05 | 150 |
| 2024-01-06 | 80 |
| 2024-01-07 | -20 |
| 2024-01-08 | 60 |

Expected output

| sale_date | sales | rolling_7day_positive |
| --- | --- | --- |
| 2024-01-01 | 100 | 100 |
| 2024-01-02 | -50 | 100 |
| 2024-01-03 | 200 | 300 |
| 2024-01-04 | -30 | 300 |
| 2024-01-05 | 150 | 450 |
| 2024-01-06 | 80 | 530 |
| 2024-01-07 | -20 | 530 |
| 2024-01-08 | 60 | 490 |

Refund days hold the total flat: 2024-01-02 leaves it at 100 and

2024-01-04 leaves it at 300. If refunds were subtracted instead of ignored,

those rows would read 50 and 220.

2024-01-08 is where the total falls, from 530 to 490. The window now

holds eight days' worth of rows, so 2024-01-01 drops out, taking its 100

with it, while the new day adds 60. A total that never forgets would report

590 and could only ever climb.

What this SQL challenge teaches you

“7-Day Rolling Sum of Positive Sales Only” 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 -- including the refund days. That is a window function, not a WHERE filter: filtering the rows out would lose them from the output entirely.
  2. Filter the VALUE, not the rows. Feed the window a expression that yields the sales figure on a good day and 0 on a refund day, then sum that.
  3. A day of exactly 0 adds nothing, and it makes no difference whether your condition is > 0 or >= 0 -- adding a zero changes no total. Do not go hunting for a distinction there.
  4. If every day in the window is a refund the answer is 0, not NULL. Summing an expression that returns 0 gives 0; summing NULLs would not.
  5. The frame is seven rows counting the current one, so the offset is 6, not 7. Use ROWS, not RANGE with a day interval -- calendar gaps do not shrink the window, and a refund day still occupies a row in it.

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