LEAD()

Look ahead to the next row's value — the mirror of LAG

Advanced 10 minLEADwindownext

Looking at the Next Row

LEAD() retrieves a value from the next row in the window — the mirror image of LAG. Where LAG looks backward, LEAD looks forward in the ordered sequence. Here's what it enables:

Forward-looking analysis — While LAG answers "what was the previous value?", LEAD answers "what comes next?" Both take the same three arguments: column, offset (default 1), and default value (default NULL).
Gap detectionLEAD(event_date) OVER (ORDER BY event_date) - event_date computes the time gap between each event and the next one. Essential for analyzing session durations, order intervals, and activity patterns.
Terminal detection — LEAD returns NULL for the last row in a partition because there is no next row to look at. This makes it easy to identify the final action in a sequence or the most recent event per group.
Same three arguments as LAG — Column to retrieve, offset (how far ahead to look), and a default value when there's no subsequent row. LEAD(price, 1, 0) returns 0 instead of NULL for the last row.
Knowledge Canvas

How LEAD Works

Look ahead to the next row

  • LEAD(col, n, default) looks forward n rows
  • Mirror image of LAG — same syntax, opposite direction
  • Last row in partition → returns NULL (or default)
  • ORDER BY defines what "next" means

LAG vs LEAD

Two sides of the same coin

LAG: look backward
LEAD: look forward
Previous row value
Next row value
First row → NULL
Last row → NULL
Common: past comparisons
Common: future predictions

LEAD Patterns

Looking ahead in data

  • Next event timing: LEAD(event_date) - event_date
  • Duration until next action: LEAD(timestamp) OVER (ORDER BY timestamp)
  • Is this the last event? LEAD(id) IS NULL
  • Combine LAG + LEAD for both directions at once

Real-World Uses

When LEAD shines

  • Time to next purchase / event
  • Forecasting: compare current to upcoming
  • Marking terminal events in sequences
  • Joining events with their subsequent state
Syntax
Syntax Template
1-- Next row value (1 row ahead by default)
2LEAD(column) OVER (
3 ORDER BY col
4)
5
6-- With offset and default for the last row
7LEAD(column, 1, default_value) OVER (
8 ORDER BY col
9)
LEAD(col)Next row value (1 row ahead by default)
LEAD(col, n)Value from n rows ahead
Last rowLEAD returns NULL (or default) for the last row
Sample Data
orders
12 rows
idorder_datetotal
50012024-01-1559.98
50022024-02-08449
50032024-02-2012.5
50042024-03-0589.99
50052024-03-15245
50062024-03-22159
50072024-04-0245.5
50082024-04-1279
50092024-04-22425
50102024-05-0518.99
50112024-05-18134
50122024-06-0865.5
products
10 rows
idnamecategorypricestock
P101Wireless MouseElectronics24.99150
P102Office DeskFurniture18925
P103LED Desk LampFurniture45.580
P104USB-C CableElectronics12.99500
P105Bluetooth SpeakerElectronics7960
P106Office ChairFurniture9515
P107Coffee MakerAppliances89.9940
P108Notebook SetStationery8.5200
P109Wireless HeadphonesElectronics15930
P110Standing DeskFurniture4258

Worked Example

Show each order with the next order date and the gap in days.

SQL
1SELECT
2 order_date,
3 total,
4 LEAD(order_date) OVER (ORDER BY order_date) AS next_order_date,
5 JULIANDAY(LEAD(order_date) OVER (ORDER BY order_date) ) - JULIANDAY(order_date) AS days_until_next
6FROM orders
7ORDER BY
8 order_date
9LIMIT 6;
LEAD(order_date) gives the next order's date. JULIANDAY converts dates to numbers so we can subtract them to get the gap in days. The last row has NULL for next_order_date because there is no subsequent row.
Output
5 rows
order_datetotalnext_order_datedays_until_next
2024-01-1559.982024-02-0824
2024-02-084492024-02-2012
2024-02-2012.52024-03-0514
2024-03-0589.992024-03-1510
2024-03-152452024-03-227

Key Concepts

1LEAD is the mirror of LAG — looks forward instead of back
2Last row returns NULL (no next row to look at)
3LEAD(date) - date = time gap until next event
4NULL from LEAD signals the final item in a sequence

Pro Tip

Looking ahead is just as important as looking back. LEAD enables gap analysis, forward predictions, and identifying terminal events in sequences.

When to Use

Time between events, identifying last action before churn, forward-looking comparisons, gap detection in sequential data.

Challenge

Solve the problem below

For each product (ordered by price ascending), show the name, price, and the next higher price (call it next_price). Show all 10 products.

Your Query