LAG: Calculate Day-Over-Day Sales Change
SQL coding challenge · Difficulty: easy · Topic: Window Functions · +50 XP
Problem
The growth team's daily digest shows each day's sales next to **how much they
moved since the day before** — a positive number is growth, a negative one is a
decline. Showing the change directly saves every reader 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 | name of the day | | amount | INT | sales taken that day |
Return every row of daily_sales, with two extra columns:
- prev_amount — the
amountfrom the row with the next-lowestsale_id,
or NULL for the earliest day
- day_over_day_change —
amount - prev_amount, orNULLfor the earliest
day
Use the LAG() window function. Sort the result by sale_id ascending.
NULL and zero are different answers
The earliest day has nothing to compare against, so both new columns are
NULL there — not 0. A 0 means something else entirely: that the day
genuinely matched the one before it. Both appear in the tests, so do not
collapse one into the other.
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.
Example input
daily_sales
| sale_id | day_name | amount | | --- | --- | --- | | 1 | Monday | 1000 | | 2 | Tuesday | 1500 | | 3 | Wednesday | 800 | | 4 | Thursday | 1200 |
Expected output
| sale_id | day_name | amount | prev_amount | day_over_day_change | | --- | --- | --- | --- | --- | | 1 | Monday | 1000 | NULL | NULL | | 2 | Tuesday | 1500 | 1000 | 500 | | 3 | Wednesday | 800 | 1500 | -700 | | 4 | Thursday | 1200 | 800 | 400 |
Monday holds the lowest sale_id, so there is no day before it and both new
columns are NULL. Tuesday rose by 1500 - 1000 = 500. Wednesday fell, so its
change is negative: 800 - 1500 = -700. Thursday recovered to
1200 - 800 = 400.
Direction matters: the change is this day minus the previous day. Reversing
the subtraction flips every sign and is a different answer.
What this SQL challenge teaches you
“LAG: Calculate Day-Over-Day Sales Change” is a easy-level SQL challenge focused on Window Functions. Working through it gives you hands-on practice with LAG, Day-over-Day, Difference, OVER — 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
- Day-over-Day
- Difference
- OVER
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 the previous 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 before the current one, in the order you specify. Order the window by sale_id so "previous" means the previous day rather than whichever row is stored first.
- The change is amount minus the previous amount, in that order. Reversing it flips every sign. You can repeat the LAG() expression, or wrap the query and subtract in an outer SELECT.
- The earliest day has no row before it, so LAG yields NULL and the subtraction yields NULL too. Leave both as NULL -- do not turn them into 0. A real 0 means the day matched the one before it, which is a different fact.
Where this comes up
Variations of this problem have been reported in interviews at Amazon, Databricks, Google, Cloudflare. 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.