PostgreSQL - LEAD Function
Last Updated :
26 Jul, 2024
In PostgreSQL, the LEAD() function is a powerful window function used to access a row that follows the current row at a specific physical offset. This function is generally employed to compare the value of the current row with the value of the next row following the current row.
Let us better understand the LEAD Function in PostgreSQL from this article.
Syntax
LEAD(expression [, offset [, default_value]])
OVER (
[PARTITION BY partition_expression, ... ]
ORDER BY sort_expression [ASC | DESC], ...
)
Parameters
Let's analyze the above syntax:
- expression: This can be a column, expression, or subquery that evaluates to a single value.
- offset: A positive integer that specifies the number of rows to move forward from the current row. It can be an expression, subquery, or column. If the offset is not set, it defaults to 1.
- default_value: This is returned if the leaded row does not exist. If not specified, it defaults to NULL.
- PARTITION BY: Divides rows into partitions. By default, it considers the query result as a single partition.
- ORDER BY: Sorts the query result rows in each partition.
PostgreSQL LEAD Function Examples
Let us take a look at some of the examples of the LEAD Function in PostgreSQL to better understand the concept.
Let’s set up a new table for the demonstration named 'Match':
PostgreSQL
CREATE TABLE Match(
year SMALLINT CHECK(year > 0),
match_id INT NOT NULL,
overs DECIMAL(10,2) NOT NULL,
PRIMARY KEY(year,match_id)
);
INSERT INTO
Match(year, match_id, overs)
VALUES
(2018, 1, 140),
(2018, 2, 174),
(2018, 3, 130),
(2019, 1, 90),
(2019, 2, 100),
(2019, 3, 120),
(2020, 1, 50),
(2020, 2, 70),
(2020, 3, 20);
Example 1: Using LEAD() for Yearly Averages
The below query uses the LEAD() function to return the overs of the current year and the average overs per year.
Query:
WITH cte AS (
SELECT
year,
SUM(overs) overs
FROM Match
GROUP BY year
ORDER BY year
)
SELECT
year,
overs,
LEAD(overs, 1) OVER (
ORDER BY year
) year_average
FROM
cte;
Output:

Example 2: Comparing Overs by Year and Match ID
The following statement uses the LEAD() function to compare the overs of the current year with overs of the next year for each group.
Query:
SELECT
year,
overs,
match_id,
LEAD(overs, 1) OVER (
PARTITION BY match_id
ORDER BY year
) next_year_overs
FROM
Match;
Output:

Important Points About PostgreSQL LEAD Function
- If there is no row at the specified offset, the LEAD() function returns NULL by default.
- The LEAD() function will include NULL values in its calculations. If there are NULL values in the ordered column, they will be treated as any other value.
- The LEAD() function can be used within CTEs to simplify complex queries and make them more readable.
- Combining LEAD() and LAG() can help you analyze trends both forward and backward.
Similar Reads
PostgreSQL- LPAD Function The LPAD() function in PostgreSQL is a powerful tool for padding a string to the left, ensuring it reaches a specified length by filling it with a designated character or characters. This function can be particularly useful in data formatting and report generation.Let us better understand the LPAD F
2 min read
PostgreSQL - LAG Function In PostgreSQL, the LAG() function is a powerful window function that allows you to access data from a previous row within the same result set. Itâs particularly useful for comparing values in the current row with values in the preceding row, making it ideal for analytical queries in PostgreSQL.For e
5 min read
PostgreSQL - NOW() Function The NOW() function in PostgreSQL is a powerful and essential tool for retrieving the current date and time. This is particularly useful when recording actions like adding or updating records in our database. Using PostgreSQL NOW() function, we can efficiently track when events occur, making the data
4 min read
PostgreSQL - NULLIF() Function Effectively handling NULL values is important in database management, especially for ensuring data integrity and avoiding errors. PostgreSQL offers several powerful functions, such as NULLIF and COALESCE, to help manage NULL and empty values efficiently. In this article, we will guide us through the
4 min read
PostgreSQL - Function Overloading In PostgreSQL, it's possible to create multiple functions with the same name, provided that each function has different arguments. This feature, known as function overloading, allows you to define functions that perform similar operations but handle different types or numbers of inputs. PostgreSQL d
3 min read