Running Total of Daily Sales
SQL coding challenge · Difficulty: easy · Topic: Window Functions · +50 XP
Problem
A manager wants a running total: for every day, the sum of all sales up to and including that day.
Schema — `daily_sales`
| Column | Type | | --- | --- | | order_date | DATE | | sales | INT |
Example Input — `daily_sales`
| order_date | sales | | --- | --- | | 2024-06-01 | 100 | | 2024-06-02 | 150 | | 2024-06-03 | 120 | | 2024-06-04 | 180 | | 2024-06-05 | 200 |
Expected Output
| order_date | sales | running_total | | --- | --- | --- | | 2024-06-01 | 100 | 100 | | 2024-06-02 | 150 | 250 | | 2024-06-03 | 120 | 370 | | 2024-06-04 | 180 | 550 | | 2024-06-05 | 200 | 750 |
Explanation
Row 1 has nothing before it, so its running total is just its own sales: 100.
Row 2 adds its own 150 to the 100 already accumulated, giving 250. Row 3 adds 120 to that 250, giving 370, and so on. The final row's total, 750, is the sum of all five days.
Note that every input row still appears in the output. A GROUP BY would collapse them into a single row, which is why this needs a window function.
Notes
- Return:
order_date,sales,running_total - Row order is not graded; the example is ordered for readability
- Same problem in PySpark: [Running Total of Daily Sales (PySpark)](/challenges/running-total-of-daily-sales-pyspark)
What this SQL challenge teaches you
“Running Total of Daily Sales” is a easy-level SQL challenge focused on Window Functions. Working through it gives you hands-on practice with SUM, OVER, Window Functions, 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
- OVER
- Window Functions
- 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:
- A window function keeps every row — GROUP BY would collapse them.
- SUM(...) OVER (ORDER BY order_date ROWS UNBOUNDED PRECEDING).
- Row 1 equals its own sales; each later row adds to the one before.
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 easy and covers Window Functions.