LAG() vs. LEAD() functions in SQL
In SQL, the LAG() and LEAD() methods are LAG() and LEAD(), we can understand how these window functions operate and the valuable insights they offer. This comparison will clarify the syntax and functionality of each function and showcase practical examples using a sample database.
The LAG() method
Let’s first create a sample database named sales_data and then demonstrate the LAG() method function.
How to create a sample database
Let’s quickly go over creating a sample database:
-- Creating a dummy database named 'sales_data'CREATE TABLE sales_data (month VARCHAR(10),revenue DECIMAL(10, 2));-- Inserting sample data into the 'sales_data' tableINSERT INTO sales_data (month, revenue) VALUES('Jan', 1000),('Feb', 1200),('Mar', 1500),('Apr', 1100),('May', 1300),('Jun', 1600);SELECT * from sales_data;
Lines 2–5: Here, we create a table named
sales_datawith two columns:monthof typeVARCHAR(10)andrevenueof typeDECIMAL(10, 2).Lines 8–14: The
VALUESclause inserts sample data into thesales_datatable for the months 'Jan' through 'Jun' along with corresponding revenue values.Line 16: This line executes a
SELECTstatement to retrieve and display all data from thesales_datatable.
Now that we have our sample data let's explain LAG() function's syntax and functionality.
Syntax
The LAG() function's syntax is as follows:
LAG(column_expression, offset_N, default_value) OVER (ORDER BY ordering_expression)
column_expression: The column for which we want to retrieve the lagged value.offset_N(Optional): The number of rows before the current row from which to retrieve the value (default is 1).default_value(Optional): The value to return if the offset goes beyond the available rows (default is NULL).ORDER BY ordering_expression: Specifies the order in which the data is considered.
Note: It's essential to use the
clause with this function, as omitting it may lead to errors. OVER() It defines how rows are grouped and sorted in the window.
Example
Below is the example showcasing the implementation of LAG() function:
-- Retrieve each month's revenue along with the revenue from the previous month using the LAG() methodSELECTmonth,revenue,LAG(revenue) OVER () AS previous_month_revenueFROMsales_data;
Line 6: We use the
LAG(revenue)method to retrieve the revenue from the previous month.Line 7: The
OVERclause with()ensures the correct order of data in accordance with the value ofmonth.
The previous_month_revenue column in the output will display the revenue from the preceding month for each row in the result set, with the first row showing NULL as there is no previous month’s revenue.
Using the offset_N and default_value
The following widget implements the LAG() function with offset_N value of 2 and a default_value of 0.
Note: Try changing the value of
offset_Nanddefault_valuein the widget below and see what affect does it generate in the output.
-- Retrieve each month's revenue along with the revenue from the previous month using LAG()SELECTmonth,revenue,LAG(revenue, 2, 0::numeric) OVER () AS previous_month_revenueFROMsales_data;
The LEAD() method
Similar to the LAG() function, let's go over the LEAD() function's syntax and functionality.
Syntax
The LEAD() function’s syntax is as follows:
LEAD(column_expression, offset_N, default_value) OVER (ORDER BY ordering_expression)
column_expression: This is the column for which we want to retrieve the leading value.offset_N(Optional): These are the number of rows after the current row from which to retrieve the value (default is 1).default_value(Optional): This is the value to return if the offset goes beyond the available rows (default is NULL).ORDER BY ordering_expression: This specifies the order in which the data is considered.
Note: It's essential to use the
OVER()clause with this function, as omitting it may lead to errors.
Example
Below is the example showcasing the implementation of LEAD() function:
-- Retrieve each month's revenue along with the revenue from the next month using LEAD()SELECTmonth,revenue,LEAD(revenue) OVER () AS next_month_revenueFROMsales_data;
Line 6: Similar to
LAG(), we use theLEAD(revenue)method to obtain the revenue from the next month.Line 7: The
OVERclause with()ensures the correct order of data in accordance with the value ofmonth.
The next_month_revenue column in the output will display the revenue from the subsequent month for each row in the result set, with the last row showing NULL as there is no revenue for the next month`
Comparison example
Now, let’s illustrate the side-by-side difference between LAG() and LEAD() by applying them simultaneously to our sales_data table scenario.
-- Compare each month's revenue with the previous and next monthsSELECTmonth,to_char(revenue, '99999.99') AS revenue,to_char(LAG(revenue) OVER (), '99999.99') AS previous_month_revenue,to_char(LEAD(revenue) OVER (), '99999.99') AS next_month_revenueFROMsales_data;
Line 6: We use the
LAG(revenue)method to get the revenue from the previous month.Line 7: Similarly,
LEAD(revenue)retrieves the revenue from the next month.Line 8: The
OVERclause with(ORDER BY month)ensures the correct order of data.
Note: We utilized the
to_charfunction with the '99999.99' format pattern to uniformly display therevenue,previous_month_revenue, andnext_month_revenuevariables. This formatting enhances readability by ensuring consistent presentation with two decimal places and leading zeros.
This example demonstrates how LAG() and LEAD() can be employed to compare values from adjacent rows in a result set.
Conclusion
In conclusion, while LAG() looks backward, LEAD() looks forward within a specified window, providing valuable insights for analytical tasks in SQL. Here is a table summarizing the differences between two:
Function Name | Functionality | Syntax | Optional Parameters |
| Retrieves lagged values |
|
|
| Retrieves leading values |
|
|
Free Resources