PostgreSQL 18 reference · Window Functions
PostgreSQL LEAD() Function
Return a value from a following row in the window, or a default when there isn't one.
PostgreSQL 18 Since 8.4
Syntax
lead(value, offset, default) OVER (PARTITION BY ... ORDER BY ...)
Parameters
| Parameter | Description |
|---|---|
value |
The expression to read. |
offset optional |
Rows ahead. Default: 1 |
default optional |
Used when there's no such row. Default: NULL |
Return value
The value from that row.
Examples
SELECT name, hire_date, lead(name) OVER (ORDER BY hire_date) AS hired_next
FROM employees
LIMIT 3;
Output
name | hire_date | hired_next ---------------+------------+--------------- Sofia Reyes | 2018-03-01 | Omar Haddad Omar Haddad | 2018-06-15 | Hannah Okafor Hannah Okafor | 2019-02-04 | Mei Chen (3 rows)
Run this on your computer — see “Get Started” for setup.
SELECT customer_id, order_date, lead(order_date) OVER (PARTITION BY customer_id ORDER BY order_date) AS next_order
FROM orders
WHERE customer_id = 1;
Output
customer_id | order_date | next_order
-------------+------------+------------
1 | 2026-01-05 | 2026-02-02
1 | 2026-02-02 | 2026-04-22
1 | 2026-04-22 | NULL
(3 rows)Run this on your computer — see “Get Started” for setup.
Version history
| Version | Change |
|---|---|
| PostgreSQL 8.4 | Added |
In other languages
LEAD()· SQLite
Related
LAG()· Return a value from a previous row in the window, or a default when there isn't one. Ideal for comparing with the last period.LAST_VALUE()· Return a value from the last row of the window frame. Widen the frame or it returns the current row.- SQL Window Functions · tutorial