Open In App
Related Articles

SQL Server LAG() function Overview

Like Article
Save Article
Report issue

At many instances, user would like to access data of the previous row or any row before the previous row from the current row. To solve this problem SQL Server’s LAG() window function can be used. LAG() : SQL Server provides LAG() function which is very useful in case the current row values need to be compared with the data/value of the previous record or any record before the previous record. The previous value can be returned on the same record without the use of self join making it straightforward to compare. 


LAG (scalar_expression [, offset [, default ]]) 
    OVER ( [ partition_by_clause ] order_by_clause )


  • 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.


SELECT Organisation, [Year], Revenue,
LAG (Revenue, 1, 0) 
OVER (PARTITION BY Organisation ORDER BY [Year]) AS PrevYearRevenue  
ORDER BY Organisation, [Year]; 


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

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. 


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]; 


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

In the above example, We can similarly calculate Year On Year Growth for the TV News Channel. Also, one thing to notice 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.

Last Updated : 24 Apr, 2023
Like Article
Save Article
Share your thoughts in the comments
Similar Reads