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

Create a free account to save this page, take notes and track your progress. Sign in

Syntax

lead(value, offset, default) OVER (PARTITION BY ... ORDER BY ...)

Parameters

ParameterDescription
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

Who came next
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.

Per customer
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

VersionChange
PostgreSQL 8.4Added

In other languages

Was this page helpful?