EDUCBA Logo

EDUCBA

MENUMENU
  • Explore
    • EDUCBA Pro
    • PRO Bundles
    • Featured Skills
    • New & Trending
    • Fresh Entries
    • Finance
    • Data Science
    • Programming and Dev
    • Excel
    • Marketing
    • HR
    • PDP
    • VFX and Design
    • Project Management
    • Exam Prep
    • All Courses
  • Blog
  • Enterprise
  • Free Courses
  • Log in
  • Sign Up
Home Data Science Data Science Tutorials PostgreSQL Tutorial PostgreSQL LEAD()
 

PostgreSQL LEAD()

Updated May 26, 2023

PostgreSQL LEAD()

 

 

Definition of PostgreSQL LEAD() Function

 PostgreSQL LEAD() function is used to access data from next rows, in the name PostgreSQL lead state that leading a next row data, It will access the data from the next row as a result. Lead function will access the data or a row followed by the current row as a specified offset, which means that for current row records lead function will access the data from the next row as a result of a query. Lead Function compares the value of records between the current row and the value of the record which follows a current row.

Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more.

Syntax

Below is the syntax, which is as follows.

Using Partition By Clause

LEAD(expression(Expression is column name of table) [, offset(Which specifies the rows number that access from next row) [, default_value(Default value of lead function)]]) OVER(

[PARTITION BY(It will divide rows into Partition) partition_expression(Partition column name), … ]

ORDER BY(Order by clause is used to sort the data) sort_expression [ASC | DESC], …(Sort column row by ascending or descending order))

By using the Default Value of Partition By Clause

LEAD(expression(Expression is column name of table) [, offset(Which specifies the rows number that access from next row) [, default_value(Default value of lead function)]]) OVER(

ORDER BY(Order by clause is used to sort the data) sort_expression [ASC | DESC], …(Sort column row by ascending or descending order))

Below is the parameter description of the above syntax as follows.

  • Expression – Expression is nothing but a column name used in a lead function to display the data of a specified column. This is a column or a Subquery. It will return a single value in the lead function.
  • LEAD() – The LEAD() function in PostgreSQL allows for accessing data from the next rows, as indicated by its name. It “leads” to the data of the next row, providing it as a result.
  • Offset – An offset is an integer number in the lead function that specifies the number that accesses data from the next row. Default value of offset in the lead function is 1. If we do not specify the offset value, it will take 1 by default.
  • Default value – This is the default value of the lead function, PostgreSQL lead function will return the default value if the offset goes above the scope value of the Partition.
  • Partition by – Partition by is a PostgreSQL clause used in the lead function. You can utilize the Partition By clause within the Lead function in PostgreSQL to actively divide rows into partitions. If we didn’t specify a partition by clause, it would consider a whole single table partition.
  • Partition expression – You can divide table values into partitions by using a column name as a partition expression in the Partition By clause.
  • Order by – This is a PostgreSQL clause used with the lead function to specify the data in ascending or descending order. When specifying “ASC” in PostgreSQL, the data will be fetched in ascending order. Conversely, when specifying “DESC,” the data will be in descending order.
  • ASC – To fetch data in ascending order using the Lead function in PostgreSQL, you can use the “ASC” keyword in the ORDER BY clause.
  • DESC – To fetch data in descending order using the Lead function in PostgreSQL, you can specify the desired column in the ORDER BY clause with the keyword “DESC”.
  • Sort expression – This is the column name used in order by clause to fetch the data using ascending or descending order.

How does this Function work?

Below is the working of the lead function as follows.

  • Lead function defines as for the current row it will access the data from the next rows as a result.
  • The PostgreSQL Lead function enables data access from the next rows, as its name implies. It “leads” to the data of the next row.
  • The lead function compares the value of records between the current row and the next record that follows it.
  • Lead function will access the data or a row followed by the current row as a specified offset, which means that for current row records lead function will access the data from the next row as a result of a query.
  • It will access the data from the next row as a result.
  • Lead Function compares the records between the current row and the next row.
  • Lead Function is important and valuable in PostgreSQL to compare values or data for current and next rows. The Lead function compares values between the current and next rows.
  • You can use the Lead function with an ORDER BY clause to display or fetch the data in ascending or descending order.
  • The Lead function compares the current row with the next rows of a table. If we need to compare two rows at the same time, we use the lead function.
  • Lead Function is more important for comparing the current and next rows.

Examples to Implement LEAD() Function in PostgreSQL

Below is an example of implementing a lead function in PostgreSQL as follows.

  • We have used the employee table to describe an example of the lead Function in PostgreSQL, below image shows the data of the employee table.
testing=# select * from Employee;

PostGRE 1

Figure – Example of employee table to describe an example of lead function in PostgreSQL.

1. Lead Function using Default Partition by Clause

The below example shows the lead function using default partition by clause in PostgreSQL as follows.

testing=# SELECT *,LEAD(emp_salary,1) OVER(ORDER BY emp_salary ASC) AS previous_salary FROM Employee;

Output:

PostgreSQL LEAD() 2

Figure – Example of lead function to fetch data in ascending order.

testing=# SELECT *,LEAD(emp_salary,1) OVER(ORDER BY emp_salary DESC) AS previous_salary FROM Employee;

Output:

PostgreSQL LEAD() 3

Figure – Example of lead function to fetch data in descending order.

2. Lead Function with Partition by Clause

The example below shows the Lead function with Partition by clause in PostgreSQL.

testing=# SELECT *,LEAD(emp_salary,1) OVER(PARTITION BY emp_id ORDER BY emp_salary ASC) AS previous_salary FROM employee;

Output:

with Partition by Clause

Figure – Example of lead function to fetch data by Partition by clause.

Conclusion

The PostgreSQL lead function compares values between the current and next rows. The lead function states that for the current row, it will access the data from the next rows. You can employ the PostgreSQL lead function and an ORDER BY clause when sorting data in ascending or descending order.

Recommended Articles

We hope that this EDUCBA information on “PostgreSQL LEAD()” was beneficial to you. You can view EDUCBA’s recommended articles for more information.

  1. Wildcards in PostgreSQL
  2. PostgreSQL FETCH
  3. Sequence in PostgreSQL
  4. PostgreSQL Database

Primary Sidebar

Footer

Follow us!
  • EDUCBA FacebookEDUCBA TwitterEDUCBA LinkedINEDUCBA Instagram
  • EDUCBA YoutubeEDUCBA CourseraEDUCBA Udemy
APPS
EDUCBA Android AppEDUCBA iOS App
Blog
  • Blog
  • Free Tutorials
  • About us
  • Contact us
  • Log in
Courses
  • Enterprise Solutions
  • Free Courses
  • Explore Programs
  • All Courses
  • All in One Bundles
  • Sign up
Email
  • [email protected]

ISO 10004:2018 & ISO 9001:2015 Certified

© 2025 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA Login

Forgot Password?

🚀 Limited Time Offer! - 🎁 ENROLL NOW