Cumulative Sales per Product
SQL coding challenge · Difficulty: medium · Topic: Window Functions · +100 XP
Problem
Each product needs its own running total. The total must restart at every new product rather than carrying over from the previous one.
Schema — `product_sales`
| Column | Type | | --- | --- | | order_date | DATE | | product | VARCHAR(10) | | sales | INT |
Example Input — `product_sales`
| order_date | product | sales | | --- | --- | --- | | 2024-06-01 | A | 100 | | 2024-06-02 | A | 150 | | 2024-06-03 | A | 200 | | 2024-06-04 | B | 120 | | 2024-06-05 | B | 180 |
Expected Output
| order_date | product | sales | cumulative_sales | | --- | --- | --- | --- | | 2024-06-01 | A | 100 | 100 | | 2024-06-02 | A | 150 | 250 | | 2024-06-03 | A | 200 | 450 | | 2024-06-04 | B | 120 | 120 | | 2024-06-05 | B | 180 | 300 |
Explanation
The running total restarts for each product rather than carrying across.
Product A accumulates 100, then 250, then 450. Product B's first row starts again from its own 120 — it does not continue from A's 450 — and then reaches 300.
That restart is what PARTITION BY product does. Without it you would get one continuous total running through both products.
Notes
- Return:
order_date,product,sales,cumulative_sales - Row order is not graded; the example is ordered for readability
- Same problem in PySpark: [Cumulative Sales per Product (PySpark)](/challenges/cumulative-sales-per-product-pyspark)
What this SQL challenge teaches you
“Cumulative Sales per Product” is a medium-level SQL challenge focused on Window Functions. Working through it gives you hands-on practice with SUM, PARTITION BY, OVER, Running Total — 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
- SUM
- PARTITION BY
- OVER
- Running Total
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:
- PARTITION BY product restarts the window for each product.
- Combine it with ORDER BY order_date inside the same OVER clause.
- Product B's first row starts from its own sales, not from A's total.
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
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.