Running Total Revenue
SQL coding challenge · Difficulty: medium · Topic: Window Functions · +100 XP
Problem
For each salesperson, compute a running (cumulative) total of their revenue in order_date order.
The running total restarts from zero for each salesperson.
Engine: queries run on MySQL 8.0, so use MySQL-compatible syntax and functions.
Tables
Table: orders
| Column | Type | | --- | --- | | order_id | INT | | salesperson | VARCHAR | | order_date | DATE | | amount | INT |
Example Input
| order_id | salesperson | order_date | amount | | --- | --- | --- | --- | | 1 | Alice | 2024-01-05 | 2500 | | 2 | Bob | 2024-01-08 | 1800 | | 3 | Alice | 2024-01-12 | 900 | | 4 | Carol | 2024-01-15 | 3200 | | 5 | Bob | 2024-01-20 | 650 | | 6 | Alice | 2024-01-25 | 1100 | | 7 | Carol | 2024-01-28 | 1500 |
Expected Output
| salesperson | order_date | amount | running_total | | --- | --- | --- | --- | | Alice | 2024-01-05 | 2500 | 2500 | | Alice | 2024-01-12 | 900 | 3400 | | Alice | 2024-01-25 | 1100 | 4500 | | Bob | 2024-01-08 | 1800 | 1800 | | Bob | 2024-01-20 | 650 | 2450 | | Carol | 2024-01-15 | 3200 | 3200 | | Carol | 2024-01-28 | 1500 | 4700 |
Explanation
Alice accumulates 2500 -> 3400 -> 4500 across her three orders. Bob starts again at 1800, because the accumulation restarts for each salesperson.
Notes
- Return
salesperson,order_date,amountandrunning_total- one row for every order in the table, not one row per salesperson. - Every input row has to survive. A plain
GROUP BYcollapses Alice's three orders into a single row, which is the mistake this challenge exists to rule out. - The grader compares rows as a set, so the final
ORDER BYis not itself graded. Sort bysalespersonthenorder_dateanyway, so your output lines up with the expected block while you work.
What this SQL challenge teaches you
“Running Total Revenue” is a medium-level SQL challenge focused on Window Functions. Working through it gives you hands-on practice with SUM OVER, running total, ORDER BY — 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
- running total
- ORDER BY
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 input row, which is exactly what this problem needs: Alice has three orders and all three must appear in the result, each with its own total. A GROUP BY would collapse them into one row per salesperson.
- The shape you want is SUM(amount) OVER (...). Inside the OVER, PARTITION BY restarts the accumulation for each salesperson, and ORDER BY decides what "so far" means for any given row.
- Order the window by order_date - that is the column this table actually has, and ordering by it is what makes the total cumulative in date order rather than in whatever order the rows happen to come back.
- You can leave the window frame implicit or spell it out as ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW. Both pass here. They only diverge when two rows share an ORDER BY value: the default RANGE frame treats those tied rows as one group and gives them the same total, while ROWS accumulates them one row at a time. No salesperson has two orders on the same date in this data, so the two agree.
- Return exactly salesperson, order_date, amount and running_total. Row order is not graded, but every order in the table must appear in your result.
Where this comes up
Variations of this problem have been reported in interviews at Swiggy, Zomato, Razorpay. 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
- Median Salary per Department
- Latest Order Per Customer
- 3-Day Rolling Sum of Sales
- 7-Day Rolling Purchase Amount by Customer
- 3-Day Rolling Sum Over Sparse Sales Data
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.