Before continuing: This entry is part of the window functions guide. If you are new to the topic, start with OVER.
LEAD example
SELECT id,
date,
total,
LEAD(date) OVER (ORDER BY date, id) AS next_date
FROM orders
WHERE customer_id = 1
ORDER BY date, id;What to look for: For each order, next_date shows the date of the same customer's next order, in the order defined. On the last row there is no next date, so the result will be NULL, unless you supply a fallback value.
Check the result with small data
Three events have dates given as the day of the month.
WITH appointments(day, event) AS (
VALUES (1,'Start'),(3,'Control'),(7,'Closing')
)
SELECT event, day,
LEAD(day) OVER (ORDER BY day) AS next_day
FROM appointments ORDER BY day;| event | day | next_day |
|---|---|---|
| Start | 1 | 3 |
| Control | 3 | 7 |
| Closing | 7 | NULL |
LEAD looks at the next row in the given order; it does not add a calendar day. The last event has no next row, so it returns NULL.
Check your understanding: Why is the next value after Start 3 and not 2?
Show answer
Because LEAD takes the value from the next row in the given order; it does not add a day. After Start (day 1) comes Control, which is day 3; there is no row for day 2.
How to use LEAD
The default offset is 1. The last row usually returns NULL, because there is no row after it.
What changes from case to case
SELECT customer_id,
id,
date,
LEAD(date) OVER (
PARTITION BY customer_id
ORDER BY date, id
) AS next_date
FROM orders
ORDER BY customer_id, date, id;PARTITION BY customer_id stops a customer's last order from picking up the date of the next customer's first order.
Common mix-ups with LEAD
Confusing the next row with the next distinct value
LEAD moves forward row by row, not value by value. If two orders have the same date, the next record can have that same date. If you need the next distinct value, prepare the data set first or use a different approach.
The next row depends on the sequence you build
The window's ordering defines what «next» means. Ordering by date gives the next observation in time; ordering by amount gives a different relationship. Add a stable key if dates are tied. The partition marks out the sequence: without separating customers, the next purchase may belong to someone else.
A missing row at the end and a null value in a real next row are different situations. The fallback argument of LEAD is used when there is no row at that distance, not to replace every null that is read. Keep a key or the next date as well as the measure if you need to tell the two cases apart.
Filtering before calculating the window changes the neighbours. If you remove every day with no sales before applying LEAD, you get the next recorded sale, not the next calendar day. To measure intervals, decide whether you want consecutive events or a full series of dates. And do not rely on the query's final ordering to define the window: the ORDER BY inside the window and the outer presentation do different jobs.
Technical questions: SQLite: window functions (opens in a new tab).