![]() |
VOOZH | about |
The LAG() function in SQL is one of the most powerful and flexible tools available for performing advanced data analysis. It is often used to compare rows, calculate differences, and tracks trends in a dataset, especially for time-series data.
If we are working with sales, stock prices, or even employee performance metrics, the LAG() function can be a game changer.
The SQL LAG() function is a window function that allows us to retrieve the value of a column from a previous row in the result set. Unlike aggregate functions (such as SUM(), AVG(), etc.), the LAG() function does not collapse the result set. Instead, it returns values for each row based on a specific window or partition of the data. It gives us a powerful way to compare rows and analyze changes in values over time.
Syntax:
.LAG (scalar_expression [, offset [, default ]]) OVER ( [ partition_by_clause ] order_by_clause )
Key Terms
Let's look at some examples of SQL LAG function and understand how to use LAG Function in SQL. Suppose we want to track the revenue of a news organization over the years, comparing each year’s revenue to the previous year’s revenue.
Query:
SELECT Organisation, [Year], Revenue,
LAG (Revenue, 1, 0)
OVER (PARTITION BY Organisation ORDER BY [Year]) AS PrevYearRevenue
FROM Org
ORDER BY Organisation, [Year]; Output:
| Organisation | Year | Revenue | PrevYearRevenue |
|---|---|---|---|
| ABCD News | 2013 | 440000 | 0 |
| ABCD News | 2014 | 480000 | 440000 |
| ABCD News | 2015 | 490000 | 480000 |
| ABCD News | 2016 | 500000 | 490000 |
| ABCD News | 2017 | 520000 | 500000 |
| ABCD News | 2018 | 525000 | 520000 |
| ABCD News | 2019 | 540000 | 525000 |
| ABCD News | 2020 | 550000 | 540000 |
| Z News | 2016 | 720000 | 0 |
| Z News | 2017 | 750000 | 720000 |
| Z News | 2018 | 780000 | 750000 |
| Z News | 2019 | 880000 | 780000 |
| Z News | 2020 | 910000 | 880000 |
Explantion:
Now, let’s expand on the first example and calculate the year-on-year (YoY) growth for each organization. We'll subtract the PrevYearRevenue from the current Revenue to get the growth.
Query:
SELECT Z.*, (Z.Revenue - z.PrevYearRevenue) as YearonYearGrowth
FROM (SELECT Organisation, [Year], Revenue,
LAG (Revenue, 1)
OVER (PARTITION BY Organisation ORDER BY [Year] ) AS PrevYearRevenue
FROM Org) Z ORDER BY Organisation, [Year]; Output:
| Organisation | Year | Revenue | PrevYearRevenue | YearOnYearGrowth |
|---|---|---|---|---|
| ABCD News | 2013 | 440000 | NULL | NULL |
| ABCD News | 2014 | 480000 | 440000 | 40000 |
| ABCD News | 2015 | 490000 | 480000 | 10000 |
| ABCD News | 2016 | 500000 | 490000 | 10000 |
| ABCD News | 2017 | 520000 | 500000 | 20000 |
| ABCD News | 2018 | 525000 | 520000 | 5000 |
| ABCD News | 2019 | 540000 | 525000 | 15000 |
| ABCD News | 2020 | 550000 | 540000 | 10000 |
| Z News | 2016 | 720000 | NULL | NULL |
| Z News | 2017 | 750000 | 720000 | 30000 |
| Z News | 2018 | 780000 | 750000 | 30000 |
| Z News | 2019 | 880000 | 780000 | 100000 |
| Z News | 2020 | 910000 | 880000 | 30000 |
Explanation:
The LAG() function can be used in various practical scenarios across different industries:
NULL unless a default value is specified.