Thursday, June 18, 2026

LEAD() and LAG() are window functions

 

LEAD() and LAG() are window functions used to access values from the next row or previous row without using a self-join.

Sample Employees Table

EmployeeIDEmployeeNameSalary
1Alice3000
2Bob4000
3Charlie2500
4David5000
5Emma3500

1. LAG() Example

Get the previous employee's salary:

SELECT
    EmployeeID,
    EmployeeName,
    Salary,
    LAG(Salary) OVER (
        ORDER BY EmployeeID
    ) AS PreviousSalary
FROM Employees;

Output

EmployeeIDEmployeeNameSalaryPreviousSalary
1Alice3000NULL
2Bob40003000
3Charlie25004000
4David50002500
5Emma35005000

LAG() looks backward.


2. LEAD() Example

Get the next employee's salary:

SELECT
    EmployeeID,
    EmployeeName,
    Salary,
    LEAD(Salary) OVER (
        ORDER BY EmployeeID
    ) AS NextSalary
FROM Employees;

Output

EmployeeIDEmployeeNameSalaryNextSalary
1Alice30004000
2Bob40002500
3Charlie25005000
4David50003500
5Emma3500NULL

LEAD() looks forward.


3. Compare Salary with Previous Employee

SELECT
    EmployeeID,
    EmployeeName,
    Salary,
    LAG(Salary) OVER (ORDER BY EmployeeID) AS PrevSalary,
    Salary - LAG(Salary) OVER (ORDER BY EmployeeID) AS Difference
FROM Employees;

Output

EmployeeSalaryPrevSalaryDifference
Alice3000NULLNULL
Bob400030001000
Charlie25004000-1500
David500025002500
Emma35005000-1500

Useful for salary growth/decline analysis.


4. Using Offset

Previous 2nd Employee Salary

SELECT
    EmployeeID,
    EmployeeName,
    Salary,
    LAG(Salary, 2) OVER (
        ORDER BY EmployeeID
    ) AS Salary2RowsBack
FROM Employees;

Output:

EmployeeSalarySalary2RowsBack
Alice3000NULL
Bob4000NULL
Charlie25003000
David50004000
Emma35002500

5. Default Value Example

Instead of NULL:

SELECT
    EmployeeID,
    EmployeeName,
    Salary,
    LAG(Salary, 1, 0) OVER (
        ORDER BY EmployeeID
    ) AS PreviousSalary
FROM Employees;

Output:

EmployeeSalaryPreviousSalary
Alice30000
Bob40003000
Charlie25004000
David50002500
Emma35005000

Syntax

LAG

LAG(column_name, offset, default_value)
OVER (ORDER BY column_name)

LEAD

LEAD(column_name, offset, default_value)
OVER (ORDER BY column_name)

Interview Use Cases

  • Compare current row with previous row.

  • Find salary increase/decrease.

  • Calculate month-over-month sales growth.

  • Detect gaps in dates or sequences.

  • Compare employee performance with previous/next employee.

  • Find consecutive records (attendance, login history, transactions).

No comments:

Post a Comment