Last Updated : 15 May, 2025
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. In this article, we will explain the function in detail and see how we can use it effectively in our SQL Queries.
What is the SQL LAG() Function?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
The LAG() function is especially useful when we need to compare the current row's value with a previous row's value. This is essential in tasks like:
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 880000Explantion:
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 30000Explanation:
The LAG() function can be used in various practical scenarios across different industries:
NULL
unless a default value is specified.The LAG() function in SQL is a flexible and powerful tool for comparing rows, analyzing trends, and calculating differences in datasets. Its ability to access previous row data makes it ideal for tasks like year-over-year analysis, sales trends, and financial reporting. By mastering the LAG() function, we can perform advanced analytics directly within SQL and reduce the complexity of your reporting queries. Use the examples and tips in this article to implement the function effectively in your database operations.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4