Maximum Difference Between Consecutive Values
SQL coding challenge · Difficulty: easy · Topic: Window Functions · +70 XP
Table: numbers
+--------+------+
| Column | Type |
+--------+------+
| id | INT | | value | INT |
+--------+------+
id is the primary key.
Problem
-------
Sort the values in ascending order. Walk through that sorted list and compare
every value with the one immediately before it. Return the largest of those
differences.
Return: a single row with one column named max_diff
IMPORTANT - sort by value, not by id
------------------------------------
"Consecutive" here means consecutive in VALUE order, not in id order. The rows
are not stored sorted, so ordering by id gives a different (wrong) answer.
Example Input
-------------
+----+-------+
| id | value |
+----+-------+
| 1 | 25 | | 2 | 10 | | 3 | 50 | | 4 | 15 | | 5 | 30 |
+----+-------+
Expected Output
---------------
+----------+
| max_diff |
+----------+
| 20 |
+----------+
Explanation
-----------
Step 1 - sort the values ascending:
10, 15, 25, 30, 50
Step 2 - pair each value with the one before it and subtract:
+-------+----------------+------------+
| value | previous value | difference |
+-------+----------------+------------+
| 10 | NULL | NULL | | 15 | 10 | 5 | | 25 | 15 | 10 | | 30 | 25 | 5 | | 50 | 30 | 20 |
+-------+----------------+------------+
The first row has nothing before it, so its difference is NULL. MAX() skips
NULLs, so it needs no special handling.
Step 3 - take the largest difference:
MAX(5, 10, 5, 20) = 20
Note what happens if you sort by id instead: the values run 25, 10, 50, 15, 30,
the differences are -15, 40, -35, 15, and the answer comes out as 40. That is
why the ordering matters.
What this SQL challenge teaches you
“Maximum Difference Between Consecutive Values” is a easy-level SQL challenge focused on Window Functions. Working through it gives you hands-on practice with LAG, Window, MAX, Consecutive, Difference — 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
- Window
- MAX
- Consecutive
- Difference
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:
- LAG(value) OVER (ORDER BY value) gives the previous row's value. Order by value, not by id.
- MySQL will not let you put a window function inside MAX(). Compute the LAG in a subquery or CTE, then take MAX(value - prev_value) outside it.
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.