At many instances, user would really like to check the worth of the present row with the worth of the subsequent row. to resolve this problem in SQL Server’s LEAD() window function are often used.
Function provides access to a row at a set physical offset following this row. LEAD() function will allows to access data of the following row, or the row after the subsequent row, and continue on.
LEAD(return_value, offset [, default]) OVER ( [PARTITION BY partition_expression] ORDER BY sort_expression [ASC | DESC] )
- return_value –
The return_value of the subsequent row supported a specified offset. The return_value must be one value.
- offset –
The offset is that the number of rows forward from the present row from where to access data. The offset should be a positive integer. If you don’t define the default value of offset is 1.
- default –
The LEAD() function results default if offset is beyond the scope of the partition. If not defined, default is NULL.
- PARTITION BY clause –
The PARTITION BY clause is optional, it differs rows of the result set into partitions to where the LEAD() function is used.
- ORDER BY clause –
The ORDER BY clause define logical order of the rows in each partition to where the LEAD() function is used.
Let us suppose we have below table named “CompanySales” :
Select * from CompanySales;
SELECT TOP 10 [Company], [Year], [Amount], LEAD(Amount, 1) OVER ( PARTITION BY Company ORDER BY Year DESC ) AS Lead_amount FROM [CompanySales] ;
- SQL Server LAG() function Overview
- MySQL | LEAD() and LAG() Function
- Difference between Structured Query Language (SQL) and Transact-SQL (T-SQL)
- SQL Server | STUFF() Function
- SIN() and COS() Function in SQL Server
- RANK() Function in SQL Server
- NTILE() Function in SQL Server
- STR() Function in SQL Server
- UNICODE() Function in SQL Server
- SUBSTRING() Function in SQL Server
- RTRIM() Function in SQL Server
- FLOOR() and CEILING() Function in SQL Server
- TAN() and COT() Function in SQL Server
- DEGREES() and RADIANS() Function in SQL Server
- ATN2() Function in SQL Server
- LOG10() Function in SQL Server
- LOG() Function in SQL Server
- ASCII() Function in SQL Server
- DIFFERENCE() Function in SQL Server
- IIF() Function in SQL Server
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.