Cumulative Store Sales on Days Above Average
SQL coding challenge · Difficulty: medium · Topic: Window Functions · +100 XP
Problem
Topic: Window Functions · running total (SUM() OVER (PARTITION BY ... ORDER BY ...)) · above-average filter · GROUP BY
The retail analytics team wants to see how each store's strong days add up: for every store, the running total of its sales counted only on days above the store's average daily sales.
Write a query that returns those days with their running total.
- A store's daily sales for a date is the sum of all its
sale_amountvalues on that date (a store can have several sales on one day). - A store's average daily sales is the average of its daily sales over the dates on which it has sales: the average of the daily totals, not of individual sales.
- Keep only the days whose daily sales are above (strictly greater than) the store's average daily sales. A day exactly equal to the average is not kept.
cumulative_salesis the running total of the kept days' daily sales for that store, in date order. It restarts for each store, and days that were not kept add nothing to it.
Return the columns store_id, sale_date, daily_sales and cumulative_sales (the grader checks the rows, not their order).
Schema
Table: sales
| Column | Type | Meaning | | --- | --- | --- | | store_id | INT | The store | | sale_amount | INT | Amount of one sale | | sale_date | DATE | The day of the sale |
Example 1
Input: sales
| store_id | sale_amount | sale_date | | --- | --- | --- | | 1 | 100 | 2025-03-01 | | 1 | 200 | 2025-03-01 | | 1 | 50 | 2025-03-02 | | 1 | 400 | 2025-03-03 | | 1 | 250 | 2025-03-04 | | 2 | 500 | 2025-03-01 | | 2 | 100 | 2025-03-02 | | 2 | 100 | 2025-03-02 | | 2 | 100 | 2025-03-02 | | 2 | 200 | 2025-03-03 |
Output:
| store_id | sale_date | daily_sales | cumulative_sales | | --- | --- | --- | --- | | 1 | 2025-03-01 | 300 | 300 | | 1 | 2025-03-03 | 400 | 700 | | 2 | 2025-03-01 | 500 | 500 |
Explanation:
- Store 1 daily sales: 1 Mar 300 (100 + 200), 2 Mar 50, 3 Mar 400, 4 Mar 250. Average = 1000 / 4 = 250.
Days above it: 1 Mar (300) and 3 Mar (400). 4 Mar equals the average, so it is not kept.
Running total of the kept days: 300, then 300 + 400 = 700 (2 Mar is not added).
- Store 2 daily sales: 1 Mar 500, 2 Mar 300 (three sales of 100), 3 Mar 200. Average = 1000 / 3 ≈ 333.33.
Only 1 Mar is above it: running total 500.
- Note: averaging the individual sales instead (store 2: 1000 / 5 = 200) would wrongly keep 2 Mar.
Constraints
sale_amountis a positive whole number; every row has a store and a date.- A store with no day above its average has no rows in the result.
- The query runs on MySQL 8.
What this SQL challenge teaches you
“Cumulative Store Sales on Days Above Average” is a medium-level SQL challenge focused on Window Functions. Working through it gives you hands-on practice with Window functions, Running total, SUM OVER, GROUP BY, Above average, Retail — 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.
Concepts covered
- Window functions
- Running total
- SUM OVER
- GROUP BY
- Above average
- Retail
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:
- Start with daily totals: SELECT store_id, sale_date, SUM(sale_amount) ... GROUP BY store_id, sale_date.
- Average those daily totals per store -- AVG(sale_amount) on the raw rows averages single sales, not days.
- Filter to daily_sales > the store's average BEFORE computing the running total.
- SUM(daily_sales) OVER (PARTITION BY store_id ORDER BY sale_date) gives the running total of the kept days.
Interview practice
This problem is good practice for data engineering interviews at companies such as Amazon. Interviewers use problems like 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.