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 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 is used to compare the value of records between the current row and the value of the record which follows a current row.
Syntax
Below is the syntax which are 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 divides 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 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 is as follows.
- Expression – Expression is nothing but a column name used in lead function to display the data of a specified column. This is a column or a Subquery. It will return single value in lead function.
- LEAD() – This 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 next row as a result.
- Offset – Offset is integer number in lead function which specifies the number that access data from the next row. Default value of offset in lead function is 1. If we will not specify the offset value it will take 1 by default.
- Default value – This is a default value of lead function, PostgreSQL lead function will return the default value if the offset will go above the scope value of partition.
- Partition by – Partition by is a clause of PostgreSQL which is used in lead function. Partition by clause in PostgreSQL lead function will divide rows into partitions on which the lead function was applied. If we didn’t specify a partition by clause it will consider a whole single partition of a table.
- Partition expression – Partition expression is nothing but a column name that we have used in the partition by clause for dividing table values into the partition.
- Order by – This is PostgreSQL clause used with the lead function to specify the data in ascending or descending order. If we specify ASC then data will be fetched in ascending order, if we specify DESC then data will be fetched in descending order.
- ASC – Ascending is used with order by clause in lead function to fetch data in ascending order.
- DESC – Descending is used with order by clause in lead function to fetch data in descending order.
- Sort expression – This is column name that we have used in order by clause to fetch the data by using ascending or descending order.
How does this Function work?
Below is the working of lead function is as follows.
- Lead function defines as for current row it will access the data from next rows as a result.
- This function is used to access data from next rows, in the name PostgreSQL lead state that leads a next row data.
- Lead function is used to compare value, if records between current row and next record which following a current row.
- 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 is used to compare the records between the current row and the next row.
- Lead function is very important and useful in PostgreSQL to compare values or data for current rows and next rows. It is used to compare values of current and next rows.
- Lead function is used with order by clause to display or fetch the data by using ascending or descending order.
- Lead function is used to compare between current row and next rows of a table. If we need to do a comparison of two rows same time we have used lead function.
- Lead function is more important for the comparison of current and next rows.
Examples to Implement LEAD() Function in PostgreSQL
Below is the example to implement a lead function in PostgreSQL are 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;
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 are as follows.
testing=# SELECT *,LEAD(emp_salary,1) OVER(ORDER BY emp_salary ASC) AS previous_salary FROM Employee;
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;
Figure – Example of lead function to fetch data in descending order.
2. Lead Function with Partition by Clause
The below example show the Lead function with partition by clause in PostgreSQL are as follows.
testing=# SELECT *,LEAD(emp_salary,1) OVER(PARTITION BY emp_id ORDER BY emp_salary ASC) AS previous_salary FROM employee;
Figure – Example of lead function to fetch data by using partition by clause.
Conclusion
PostgreSQL lead function is used to the comparison of values between current and next rows. Lead function states that for the current row it will access the data from the next rows. PostgreSQL lead function is used with order by clause to sort the data in ascending or descending order.
Recommended Articles
This is a guide to PostgreSQL LEAD(). Here we discuss How PostgreSQL LEAD() Function Works and Implementation of PostgreSQL LEAD(). You may also have a look at the following articles to learn more –