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.

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:

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).

Only 1 Mar is above it: running total 500.

Constraints

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

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. Start with daily totals: SELECT store_id, sale_date, SUM(sale_amount) ... GROUP BY store_id, sale_date.
  2. Average those daily totals per store -- AVG(sale_amount) on the raw rows averages single sales, not days.
  3. Filter to daily_sales > the store's average BEFORE computing the running total.
  4. 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

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