Open In App

SQL LAG() Function

Last Updated : 15 May, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

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

  • scalar_expression - The value to be returned based on the specified offset.
  • offset - The number of rows back from the current row from which to obtain a value. If not specified, the default is 1.
  • default - default is the value to be returned if offset goes beyond the scope of the partition. If a default value is not specified, NULL is returned.
  • partition_by_clause: An optional clause that divides the result set into partitions. The LAG() function is applied to each partition separately.
  • order_by_clause: The order of the rows within each partition. This is mandatory and must be specified.

Why Use the LAG() Function?

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:

  • Comparing Rows: Helps compare the current row with a previous row's data.
  • Trend Analysis: Useful for analyzing changes in values, like stock prices, sales figures, or other time-series data.
  • Finding Differences: Calculate the difference between consecutive rows in terms of time, quantity, or any other metric.

Example 1 : Basic Usage of LAG()

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:

OrganisationYearRevenuePrevYearRevenue
ABCD News20134400000
ABCD News2014480000440000
ABCD News2015490000480000
ABCD News2016500000490000
ABCD News2017520000500000
ABCD News2018525000520000
ABCD News2019540000525000
ABCD News2020550000540000
Z News20167200000
Z News2017750000720000
Z News2018780000750000
Z News2019880000780000
Z News2020910000880000

Explantion:

  • In the above example, We have 2 TV News Channel whose Current and Previous Year's Revenue is presented on the same row using the LAG() function.
  • As You can see that the very first record for each of the TV News channels don't have previous year revenues so it shows the default value of 0.
  • This function can be very useful in yielding data for BI reports when you want to compare values in consecutive periods, for e.g. Year on Year or Quarter on Quarter or Daily Comparisons. 

Example 2 : Calculate Year-on-Year Growth

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:

OrganisationYearRevenuePrevYearRevenueYearOnYearGrowth
ABCD News2013440000NULLNULL
ABCD News201448000044000040000
ABCD News201549000048000010000
ABCD News201650000049000010000
ABCD News201752000050000020000
ABCD News20185250005200005000
ABCD News201954000052500015000
ABCD News202055000054000010000
Z News2016720000NULLNULL
Z News201775000072000030000
Z News201878000075000030000
Z News2019880000780000100000
Z News202091000088000030000

Explanation:

  • In the above example, We can similarly calculate Year On Year Growth for the TV News Channel.
  • In this example is we haven't supplied any default parameter to LAG(), and hence the LAG() function returns NULL in case there are no previous values.
  • The LAG() function can be implemented at the database level and BI Reporting solutions like Power BI and Tableau can avoid using the cumbersome measures at the reporting layer.

Use Cases of SQL LAG() Function

The LAG() function can be used in various practical scenarios across different industries:

  • Sales Trends: Compare daily, monthly, or yearly sales performance and compare current figures with previous periods.
  • Stock Analysis: Analyze changes in stock prices over time and identify trends or fluctuations.
  • Employee Salaries: Analyze salary growth within a company by comparing current salaries with previous years.
  • Data Validation: Identify missing or duplicate rows in a sequence.

Important Points About SQL LAG() Function

  • Window Function: The LAG() function is categorized as a window function. It doesn’t collapse the result set but allows you to access data from previous rows.
  • Order of Rows Matters: The ORDER BY clause is crucial because it defines the order in which the rows are processed. Without it, the function wouldn't know which row is the "previous" row.
  • Null Values for First Row: For the first row in a partition, there’s no previous row, so LAG() returns NULL unless a default value is specified.

Conclusion

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.


Next Article
Article Tags :

Similar Reads