7-Day Rolling Average Sales
SQL coding challenge · Difficulty: easy · Topic: Window Functions · +100 XP
Problem
The dashboard shows a 7-day rolling average of sales to reveal the weekly trend.
For each day, average that day's sales together with the six days before it.
Engine: queries run on MySQL 8.0, so use MySQL-compatible syntax and functions.
Tables
Table: daily_sales
| Column | Type | | --- | --- | | sale_date | DATE | | sales | INT |
Example Input
| sale_date | sales | | --- | --- | | 2024-01-01 | 100 | | 2024-01-02 | 200 | | 2024-01-03 | 150 | | 2024-01-04 | 300 | | 2024-01-05 | 250 | | 2024-01-06 | 180 | | 2024-01-07 | 220 | | 2024-01-08 | 90 | | 2024-01-09 | 310 |
Expected Output
| sale_date | sales | rolling_7day_avg | | --- | --- | --- | | 2024-01-01 | 100 | 100.0000 | | 2024-01-02 | 200 | 150.0000 | | 2024-01-03 | 150 | 150.0000 | | 2024-01-04 | 300 | 187.5000 | | 2024-01-05 | 250 | 200.0000 | | 2024-01-06 | 180 | 196.6667 | | 2024-01-07 | 220 | 200.0000 | | 2024-01-08 | 90 | 198.5714 | | 2024-01-09 | 310 | 214.2857 |
Explanation
The first rows have fewer than seven days behind them, so they average over what is there: day one is just its own 100, day two is the mean of 100 and 200. From 2024-01-07 onward every row averages a full seven days - which is why 2024-01-08 drops to 198.5714 as the opening 100 falls out of the window.
Notes
- Return
sale_date,salesandrolling_7day_avg- one row per day, every day in the table. - Early rows average over fewer than seven days rather than returning
NULL. - Values are compared numerically at full precision, so do not round the result:
214.29will not match214.2857. - The grader compares rows as a set, so the final
ORDER BYis not itself graded. Sort bysale_dateanyway, so your output lines up with the expected block while you work.
What this SQL challenge teaches you
“7-Day Rolling Average Sales” is a easy-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:
- This needs a window function rather than a GROUP BY: every day stays in the result and gets its own average, computed from a moving slice of the rows around it.
- AVG(sales) OVER (ORDER BY sale_date) on its own gives you a cumulative average - every row from the beginning of time up to this one. That is not a rolling average, and it is the single most common way to get this wrong. What makes it rolling is the window frame you add inside the OVER.
- Count the rows the frame has to cover. A 7-day window is the current row plus the six before it, so the bound is 6 PRECEDING, not 7. Saying 7 PRECEDING averages eight days and gives 212.5000 on the last row instead of 214.2857.
- Two ways to express it, and both are accepted here. A row-counting frame takes the previous six rows; a date-ranged frame takes the previous six days by interval. They agree on this data because it has one row per day with no gaps - but on a table with missing days they diverge, and the date-ranged version is the one that still means "7 days".
- Do not round. The expected values carry four decimal places, and the comparison is numeric, so ROUND(..., 2) turns 214.2857 into 214.29 and fails. Return exactly sale_date, sales and rolling_7day_avg; row order is not graded.
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 easy and covers Window Functions.