Back to Library

LEAD() in SQL

Accesses data from a subsequent row in the result set within a specified window partition.

Syntax

LEAD(expression[, offset[, default_value]]) OVER (
    [PARTITION BY column1, column2, ...]
    ORDER BY column1 [ASC|DESC], column2 [ASC|DESC], ...
)
Return type
Same as input expression type

LEAD() Function Example

SELECT product_name, sales_date, sales_amount,
       LEAD(sales_amount) OVER (ORDER BY sales_date) AS next_sales
FROM sales;
-- Output: 'Product A', '2024-12-26', 1000, 1200

What is LEAD() in SQL?

The LEAD() function in SQL allows you to access data from a subsequent (next) row within the same result set, based on a specified window partition and ordering. It is commonly used for trend analysis, forecasting, calculating future differences, and detecting upcoming changes.

This function is available in SQL Server, PostgreSQL, Oracle, and MySQL 8.0+ as part of window functions.

This function is widely used in predictive analytics, financial forecasting, and time-series analysis.

Parameters:

  • expression: Column or expression to retrieve from next row

  • offset (optional): Number of rows forward to look (default: 1)

  • default_value (optional): Value to return when no next row exists

Example Use Cases:

-- Calculate day-over-day change

SELECT date, closing_price,
       LEAD(closing_price) OVER (ORDER BY date) AS next_day_price,
       LEAD(closing_price) OVER (ORDER BY date) - closing_price AS price_change
FROM stock_prices;
-- Output:
date        | closing_price | next_day_price | price_change
------------|--------------|----------------|-------------
2024-01-01  | 150.25      | 152.50         | 2.25
2024-01-02  | 152.50      | 151.75         | -0.75
2024-01-03  | 151.75      | 153.00         | 1.25
2024-01-04  | 153.00      | 152.25         | -0.75
2024-01-05  | 152.25      | NULL           | NULL

-- Find gaps in sequence

SELECT sequence_id,
       CASE WHEN LEAD(sequence_id) OVER (ORDER BY sequence_id) - sequence_id > 1
            THEN 'Gap Found'
            ELSE 'No Gap'
       END AS sequence_check
FROM sequences;
-- Output:
sequence_id | sequence_check
------------|---------------
1           | No Gap
2           | No Gap
3           | Gap Found
6           | No Gap
7           | No Gap
8           | Gap Found
10          | No Gap

-- Analyze upcoming appointments

SELECT patient_name, appointment_date,
       LEAD(appointment_date, 1, 'No future appointment') OVER (PARTITION BY patient_id ORDER BY appointment_date)
       AS next_appointment
FROM appointments;
-- Output:
patient_name  | appointment_date | next_appointment
-------------|-----------------|------------------
John Smith    | 2024-01-15     | 2024-02-15
John Smith    | 2024-02-15     | 2024-03-15
John Smith    | 2024-03-15     | No future appointment
Maria Garcia  | 2024-01-20     | 2024-04-20
Maria Garcia  | 2024-04-20     | No future appointment
James Brown   | 2024-02-01     | 2024-02-15

Notes:

  • Behavior: Returns NULL if no next row and no default specified

  • Performance Considerations: Window function optimization may be needed

  • Version Info: Core window function available in most modern DBMS

  • Deprecated/Recommended Alternatives: None, standard functionality

Error Handling:

  • Error: Syntax error if ORDER BY is omitted

  • Recommendation: Always include ORDER BY clause and consider default values


Supported Databases:

DBMS
Function / Syntax
Example
Behavior with NULL
MySQL

LEAD()

LEAD(col) OVER (ORDER BY date)

Returns NULL

PostgreSQL

LEAD()

LEAD(col) OVER (ORDER BY date)

Returns NULL

SQL Server

LEAD()

LEAD(col) OVER (ORDER BY date)

Returns NULL

SQLite

LEAD()

LEAD(col) OVER (ORDER BY date)

Returns NULL

BigQuery

LEAD()

LEAD(col) OVER (ORDER BY date)

Returns NULL

Snowflake

LEAD()

LEAD(col) OVER (ORDER BY date)

Returns NULL

Athena

LEAD()

LEAD(col) OVER (ORDER BY date)

Returns NULL

Related Functions:

CAST/CONVERT()

Converts a value from one data type to another. Both CAST and CONVERT perform the same function with different syntax.

Converts a value from one data type to another. Both CAST and CONVERT perform the same function with different syntax.

Converts a value from one data type to another. Both CAST and CONVERT perform the same function with different syntax.

Converts a value from one data type to another. Both CAST and CONVERT perform the same function with different syntax.

COALESCE()

Returns the first non-null value in a list of expressions, evaluated from left to right.

Returns the first non-null value in a list of expressions, evaluated from left to right.

Returns the first non-null value in a list of expressions, evaluated from left to right.

Returns the first non-null value in a list of expressions, evaluated from left to right.

LEAD()

Accesses data from a subsequent row in the result set within a specified window partition.

Accesses data from a subsequent row in the result set within a specified window partition.

Accesses data from a subsequent row in the result set within a specified window partition.

Accesses data from a subsequent row in the result set within a specified window partition.

DENSE_RANK()

Assigns a rank to rows within a result set partition, with no gaps in rank numbers when ties occur.

Assigns a rank to rows within a result set partition, with no gaps in rank numbers when ties occur.

Assigns a rank to rows within a result set partition, with no gaps in rank numbers when ties occur.

Assigns a rank to rows within a result set partition, with no gaps in rank numbers when ties occur.

CASE

Provides conditional logic in SQL queries, evaluating conditions and returning specified results based on matching conditions.

Provides conditional logic in SQL queries, evaluating conditions and returning specified results based on matching conditions.

Provides conditional logic in SQL queries, evaluating conditions and returning specified results based on matching conditions.

Provides conditional logic in SQL queries, evaluating conditions and returning specified results based on matching conditions.

RANK()

Assigns a rank to rows within a result set partition, with gaps in rank numbers when ties occur.

Assigns a rank to rows within a result set partition, with gaps in rank numbers when ties occur.

Assigns a rank to rows within a result set partition, with gaps in rank numbers when ties occur.

Assigns a rank to rows within a result set partition, with gaps in rank numbers when ties occur.

One place for all your queries,
directly on your SQL editor