LAG: Show Previous Day's Sales Amount
SQL coding challenge · Difficulty: easy · Topic: Window Functions · +50 XP
Problem
A daily operations report shows each day's sales next to the previous day's
amount, so a manager can spot a drop at a glance instead of doing the
subtraction in their head.
Engine: queries run on MySQL 8.0, so use MySQL-compatible syntax and functions.
Tables
daily_sales — one row per day.
| column | type | description | | --- | --- | --- | | sale_id | INT | identifies the day, and defines the report's order | | day_name | VARCHAR | short name of the day | | amount | INT | sales taken that day |
Return one row for every row of daily_sales, containing:
- sale_id
- day_name
- amount
- prev_day_sales — the
amountbelonging to the row with the next-lowest
sale_id, or NULL for the earliest day, which has no day before it
Use the LAG() window function. Sort the result by sale_id ascending.
Example input
daily_sales
| sale_id | day_name | amount | | --- | --- | --- | | 1 | Mon | 100 | | 2 | Tue | 200 | | 3 | Wed | 300 | | 4 | Thu | 400 | | 5 | Fri | 500 |
Expected output
| sale_id | day_name | amount | prev_day_sales | | --- | --- | --- | --- | | 1 | Mon | 100 | NULL | | 2 | Tue | 200 | 100 | | 3 | Wed | 300 | 200 | | 4 | Thu | 400 | 300 | | 5 | Fri | 500 | 400 |
Mon holds the lowest sale_id, so there is no day before it and
prev_day_sales is NULL. Every later day simply repeats the amount recorded
by the day that precedes it.
Watch out: NULL belongs to the earliest day only. If the previous day
took 0, the answer is 0 — not NULL. The two mean different things, so
do not collapse one into the other.
Ordering: sale_id values are not guaranteed to be consecutive, and the
rows are not guaranteed to be stored in order. "Previous" always means
the next-lowest sale_id, never the physically preceding row.
What this SQL challenge teaches you
“LAG: Show Previous Day's Sales Amount” is a easy-level SQL challenge focused on Window Functions. Working through it gives you hands-on practice with LAG, OVER, ORDER BY, Previous row — 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
- LAG
- OVER
- ORDER BY
- Previous row
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:
- You need a value from another ROW, not an aggregate over a group. A window function can look backwards without joining the table to itself.
- LAG(col) OVER (ORDER BY ...) returns col from the row immediately before the current one, in whatever order you specify.
- Order the window by sale_id, the report's own order, so "previous" means the previous day rather than whichever row happens to be stored first.
- The earliest sale_id has no row before it, so LAG yields NULL there. Leave it as NULL -- do not turn it into 0, because a real 0 means the previous day took nothing, which is a different fact.
Where this comes up
Variations of this problem have been reported in interviews at Amazon, Google, Databricks, Netflix. 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
- 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 easy and covers Window Functions.