MySQL | LEAD() and LAG() Function
Last Updated :
20 Nov, 2024
The LEAD() and LAG() functions in MySQL are powerful window functions introduced in version 8.0. They allow users to access data from preceding or following rows without the need for self-joins or subqueries, making them invaluable for analyzing sequential or time-series data.
In this article, we will learn the LEAD() and LAG() functions in MySQL, covering their syntax, parameters, and key differences. You’ll learn how to use these functions with real-world examples, explore their role in window function operations
What are LEAD() and LAG() Functions in MySQL?
The LAG() function is used to get value from the row that precedes the current row.
The LEAD() function is used to get value from a row that succeeds the current row.
Syntax:
For LEAD() Function-
LEAD(expr, N, default)
OVER (Window_specification | Window_name)
For LAG() Function-
LAG(expr, N, default)
OVER (Window_specification | Window_name)
The N and default argument in the function is optional.
Parameters used:
- expr: It can be a column or any built-in function.
- N: It is a positive value which determine number of rows preceding/succeeding the current row. If it is omitted in query then its default value is 1.
- default: It is the default value return by function in-case no row precedes/succeedes the current row by N rows. If it is missing then it is by default NULL.
- OVER(): It defines how rows are partitioned into groups. If OVER() is empty then function compute result using all rows.
- Window_specification: It consist of query partition clause which determines how the query rows are partitioned and ordered.
- Window_name: If window is specified elsewhere in the query then it is referenced using this Window_name.
Examples of LEAD() and LAG() Functions
Consider a “contest” table:-
c_id |
start_date |
end_date |
1 |
2015-02-01 |
2015-02-04 |
2 |
2015-02-02 |
2015-02-05 |
3 |
2015-02-03 |
2015-02-07 |
4 |
2015-02-04 |
2015-02-06 |
5 |
2015-02-06 |
2015-02-09 |
6 |
2015-02-08 |
2015-02-10 |
7 |
2015-02-10 |
2015-02-11 |
In the above table, “c_id” represents contest id and “start_date” and “end_date” represents starting and ending date of contest respectively.
Example 1: Calculate Overlapping Days Between Contests
Problem description: We have to find number of days a contest will collapse with the next contest i.e no. of days on which both contests are held.
Query:
SELECT c_id,
start_date,
end_date,
DATEDIFF(LEAD(start_date) OVER (ORDER BY start_date), end_date) + 1 AS no_of_days
FROM contest;
Explanations:
In the above query “end_date” return ending date of the current contest and lead(start_date) return next contest starting date. So, the difference between these dates plus 1 will return no. of days the contest will collide.
Here, window specification is given by “order by” clause, which represent that lead() function will operate on table order by their “start_date” in increasing order. Since there is no partition clause so, whole table is taken as a single window.
Output:
c_id |
start_date |
end_date |
no_of_days |
1 |
2015-02-01 |
2015-02-04 |
3 |
2 |
2015-02-02 |
2015-02-05 |
3 |
3 |
2015-02-03 |
2015-02-07 |
4 |
4 |
2015-02-04 |
2015-02-06 |
1 |
5 |
2015-02-06 |
2015-02-09 |
2 |
6 |
2015-02-08 |
2015-02-10 |
1 |
7 |
2015-02-10 |
2015-02-11 |
NULL |
Since, there is no contest after contest 7 i.e (c_id=7). So, lead(start_date) returns NULL value.
Note:The LEAD() and LAG() function are always used with OVER(). Missing of an over clause will raise an error.
Example 2: Retrieve Preceding Data for Analysis
Query:
SELECT
c_id,
start_date,
end_date,
LAG(end_date) OVER (ORDER BY start_date) AS prev_end_date,
DATEDIFF(start_date, LAG(end_date) OVER (ORDER BY start_date)) AS gap_days
FROM contest;
Output:
c_id |
start_date |
end_date |
prev_end_date |
gap_days |
1 |
2015-02-01 |
2015-02-04 |
NULL |
NULL |
2 |
2015-02-02 |
2015-02-05 |
2015-02-04 |
-2 |
3 |
2015-02-03 |
2015-02-07 |
2015-02-05 |
-2 |
4 |
2015-02-04 |
2015-02-06 |
2015-02-07 |
-3 |
5 |
2015-02-06 |
2015-02-09 |
2015-02-06 |
0 |
6 |
2015-02-08 |
2015-02-10 |
2015-02-09 |
-1 |
7 |
2015-02-10 |
2015-02-11 |
2015-02-10 |
0 |
Conclusion
The LEAD() and LAG() functions in MySQL are essential for performing sequential data analysis, making it easier to access previous or next rows without complex joins. Whether you’re comparing trends, detecting gaps, or monitoring progressions, these functions simplify query writing and enhance performance. By understanding the syntax, use cases, and real-world applications, you can leverage LEAD() and LAG() effectively in your database operations.
Similar Reads
LEAST() Function in MySQL
The LEAST() function in MySQL is a versatile tool that returns the smallest (minimum) value from a list of expressions. It can be used with numbers, strings, or even columns of data to find the minimum value according to their data type. In this article, We will learn about LEAST() Function in MySQL
4 min read
MySQL | LAST_DAY() Function
The MySQL LAST_DAY() function returns the last day of the month for a given date or datetime. This function takes a date value as an argument and returns the last day of the month for that date. The date argument should be a valid date or datetime. SyntaxLAST_DAY( Date );Parameters:Date: The LAST_DA
3 min read
SQL LAG() Function
The LAG() function in SQL is a powerful window function that allows you to retrieve the value of a column from the previous row in the result set. It is commonly used for tasks like calculating differences between rows, tracking trends, and comparing data within specific partitions. In this article,
4 min read
SQL MIN() and MAX() Functions
The SQL MIN() and MAX() functions are essential aggregate functions in SQL used for data analysis. They allow you to extract the minimum and maximum values from a specified column, respectively, making them invaluable when working with numerical, string, or date-based data. In this article, we will
4 min read
LN() Function in MySQL
LN() function : It is the function in MySQL is used to calculate the natural logarithm of a specific number with base e . The number must be greater than 0, otherwise it will return NULL. Syntax : LN(X) Parameter : LN() function accepts one parameter as mentioned above in the syntax and described be
2 min read
PLSQL | LAST_DAY Function
The PLSQL LAST_DAY function is used for returning the last day of the month based on a date value. The last day of the month is defined by the session parameter NLS_CALENDAR. The LAST_DAY function accepts one parameter which is the date value used to calculate the last day of the month. The LAST_DAY
2 min read
AVG() Function in MySQL
AVG() function : This function in MySQL is used to return the average value of the specified expression. Features : This function is used to find the average value of the specified expression.This function comes under Numeric Functions.This function accepts only one parameter namely expression.This
2 min read
LOG() Function in MySQL
LOG() function in MySQL is used to calculate the natural logarithm of a specific number. The number must be >0 Otherwise it will return NULL. Syntax : LOG(X) Parameter : This method accepts one parameter as mentioned above and described below : X : A number whose logarithm value we want to calcul
3 min read
DIV() Function in MySQL
DIV() function : This function in MySQL is used to return a quotient (integer) value when integer division is done. For example, when 7 is divided by 3, then 2 will be returned. Syntax : SELECT x DIV y; Parameter : This method accepts two parameters as given below as follows. x - Specified dividend
1 min read
PLSQL | LOG Function
The PLSQL LOG function is used for returning the logarithm of n base m. The LOG function accepts two parameters which are used to calculate the logarithmic value. The LOG function returns a value of the numeric data type. This function takes as an argument any numeric data type as well as any non-nu
2 min read