LAG & LEAD in SQL
Reach to the previous or next row in one ORDER
LAG(column) fetches a value from the previous row of the window; LEAD(column) from the next. Comparing each row with its neighbour — month-over-month change, gap since last order, time between events — becomes a self-join-free one-liner. Both take an optional offset (LAG(x, 2) = two rows back) and a default for the boundary rows.
Fix the row order
OVER (ORDER BY id) defines the sequence — LAG reaches one row “back” in it, LEAD one row “forward”.
Fetch the neighbour value
LAG(salary) OVER (…) returns the previous row’s salary as an ordinary column on the current row — no self-join, no correlated subquery.
Compute the difference inline
salary − LAG(salary) OVER (…) AS delta — month-over-month change, gap between events, step size.
Handle the edges deliberately
The first row has no predecessor: LAG returns NULL, or your default — LAG(salary, 1, 0). LEAD mirrors at the far end.
Partition to restart per group
LAG(salary) OVER (PARTITION BY department ORDER BY id) — each employee compares to their department-mate, never across departments.
1SELECT name, salary,2 LAG(salary) OVER (ORDER BY id) AS prev_salary,3 salary - LAG(salary) OVER (ORDER BY id) AS delta4FROM employees ORDER BY id;
| name | salary | prev_salary | delta |
|---|---|---|---|
| Alice | 85000 | null | null |
| Bob | 52000 | 85000 | -33000 |
| Cynthia | 91000 | 52000 | 39000 |
| David | 47000 | 91000 | -44000 |
| Elena | 58000 | 47000 | 11000 |
| Farid | 73000 | 58000 | 15000 |
Each row carries its neighbour’s salary alongside its own: Bob’s delta is 52,000 − 85,000 = −33,000. Alice, first in the order, has no previous row — LAG yields NULL and so does the difference. The whole comparison is one pass over sorted data.
Difference from the previous row
One pass, no join — the LAG signature move.
1SELECT name, salary,2 LAG(salary) OVER (ORDER BY id) AS prev_salary,3 salary - LAG(salary) OVER (ORDER BY id) AS delta4FROM employees ORDER BY id;
| name | salary | prev_salary | delta |
|---|---|---|---|
| Alice | 85000 | null | null |
| Bob | 52000 | 85000 | -33000 |
| Cynthia | 91000 | 52000 | 39000 |
| David | 47000 | 91000 | -44000 |
| Elena | 58000 | 47000 | 11000 |
| Farid | 73000 | 58000 | 15000 |
Previous row within the department
PARTITION BY keeps comparisons inside the group.
1SELECT name, department, salary,2 LAG(salary) OVER (PARTITION BY department ORDER BY salary) AS lower_paid3FROM employees WHERE department = 'Engineering' ORDER BY salary;
| name | department | salary | lower_paid |
|---|---|---|---|
| Farid | Engineering | 73000 | null |
| Alice | Engineering | 85000 | 73000 |
| Cynthia | Engineering | 91000 | 85000 |
LEAD with a default
The last row can carry a sentinel instead of NULL.
1SELECT name, salary,2 LEAD(salary, 1, 0) OVER (ORDER BY id) AS next_salary3FROM employees WHERE department = 'Sales' ORDER BY id;
| name | salary | next_salary |
|---|---|---|
| Bob | 52000 | 58000 |
| Elena | 58000 | 0 |
Time between events
Timestamps + LAG = inter-arrival analysis.
1SELECT order_id, placed_at,2 placed_at - LAG(placed_at) OVER (PARTITION BY customer_id ORDER BY placed_at) AS since_prev3FROM orders ORDER BY customer_id, placed_at;
| order_id | placed_at | since_prev |
|---|---|---|
| #101 | 2026-08-28 09:14 | null |
| #102 | 2026-08-30 18:40 | 2 days 09:26 |
| #103 | 2026-09-02 11:05 | 2 days 16:25 |
Recommended resourcesAffiliate links · may earn us a commission
Recommended for learners
Practice what you learned
Take the queries you just visualized and see the data structures that power real databases.