
MySQL LEAD And LAG Function
The MySQL 8.0 and later versions, you can use the LEAD()
and LAG()
window functions to access the values of rows that are physically next or prior to the current row within a result set, based on a specified order. These functions are particularly useful for tasks such as calculating the difference between adjacent rows or comparing the current row to its neighboring rows.
Here’s an explanation of the LEAD()
and LAG()
functions:
- LEAD() Function:
- The
LEAD()
function retrieves the value from the next row in the result set, according to the specified order. If the current row is the last row in the result set, the function returnsNULL
. - The syntax is as follows:
LEAD(column_name, offset, default_value) OVER (ORDER BY order_column)
column_name
: The column from which you want to retrieve the value from the next row.offset
: An optional parameter specifying the number of rows to skip ahead. The default is 1, meaning the next row.default_value
: An optional parameter specifying the value to return if there is no next row (default isNULL
).
- LAG() Function:
- The
LAG()
function retrieves the value from the previous row in the result set, according to the specified order. If the current row is the first row in the result set, the function returnsNULL
. - The syntax is similar to
LEAD()
:sql LAG(column_name, offset, default_value) OVER (ORDER BY order_column)
column_name
: The column from which you want to retrieve the value from the previous row.offset
: An optional parameter specifying the number of rows to skip back. The default is 1, meaning the previous row.default_value
: An optional parameter specifying the value to return if there is no previous row (default isNULL
).
Here’s an example of how you might use the LEAD()
and LAG()
functions:
Suppose you have a table named sales
with columns sale_date
and revenue
, and you want to calculate the difference in revenue between the current and next sale:
SELECT
sale_date,
revenue,
LEAD(revenue) OVER (ORDER BY sale_date) AS next_sale_revenue,
revenue - LEAD(revenue) OVER (ORDER BY sale_date) AS revenue_difference
FROM sales;
In this query, the LEAD()
function retrieves the revenue of the next sale, and the difference is calculated between the current and next sale. You can use the LAG()
function in a similar way to access the previous sale’s revenue.
These functions are useful for performing time-series analysis, detecting trends, and identifying outliers in datasets.