MySQL LEAD Function
The LEAD()
function in MySQL is a window function introduced in MySQL 8.0. It allows you to access data from a subsequent row in the same result set without requiring a self-join. This is particularly useful for comparing values in consecutive rows or performing calculations based on subsequent rows.
Syntax
expression
: The column or calculation whose value is returned from a subsequent row.offset
: The number of rows ahead from the current row (default is1
).default
: The value returned if no subsequent row exists (default isNULL
).PARTITION BY
: Divides the result set into partitions; the function is applied to each partition independently.ORDER BY
: Specifies the order of rows in which the function is applied.
Key Points
Default Values:
- If no subsequent row exists, the function returns
NULL
unless adefault
value is specified.
- If no subsequent row exists, the function returns
Offset:
- The
offset
determines how far ahead the function looks for the value. - By default,
offset
is1
.
- The
Partitioning:
- The result set can be partitioned into groups using the
PARTITION BY
clause, and the function will operate within each group.
- The result set can be partitioned into groups using the
Ordering:
- The
ORDER BY
clause ensures rows are processed in a specific order.
- The
Example 1: Simple Use of LEAD()
Suppose you have a table sales
:
sale_id | region | amount |
---|---|---|
1 | North | 100 |
2 | North | 200 |
3 | North | 150 |
4 | South | 300 |
5 | South | 250 |
To fetch the current amount and the next amount:
Output:
sale_id | region | amount | next_amount |
---|---|---|---|
1 | North | 100 | 150 |
3 | North | 150 | 200 |
2 | North | 200 | NULL |
5 | South | 250 | 300 |
4 | South | 300 | NULL |
Example 2: Using LEAD()
with an Offset
To fetch the amount two rows ahead:
Output:
sale_id | region | amount | two_rows_ahead |
---|---|---|---|
1 | North | 100 | 200 |
3 | North | 150 | NULL |
2 | North | 200 | NULL |
5 | South | 250 | NULL |
4 | South | 300 | NULL |
Example 3: Using LEAD()
with a Default Value
To provide a default value when no subsequent row exists:
Output:
sale_id | region | amount | next_amount_with_default |
---|---|---|---|
1 | North | 100 | 150 |
3 | North | 150 | 200 |
2 | North | 200 | 0 |
5 | South | 250 | 300 |
4 | South | 300 | 0 |
Example 4: Calculate Change Between Rows
Using the LEAD()
function to calculate the difference between the current amount and the next amount:
Output:
sale_id | region | amount | next_amount | amount_difference |
---|---|---|---|---|
1 | North | 100 | 150 | 50 |
3 | North | 150 | 200 | 50 |
2 | North | 200 | NULL | NULL |
5 | South | 250 | 300 | 50 |
4 | South | 300 | NULL | NULL |
Use Cases
Comparative Analysis:
- Compare current values to future values within a dataset.
Trend Analysis:
- Analyze changes in metrics like sales or prices over time.
Gaps in Data:
- Identify and analyze gaps between consecutive rows.
Forecasting:
- Use future values to derive trends or project outcomes.
Conclusion
The LEAD()
function is a powerful tool for accessing subsequent rows in a result set. It simplifies tasks like comparisons, trend analysis, and gap detection without requiring complex subqueries or joins. Its flexibility with partitioning and ordering makes it invaluable for advanced analytics in MySQL.