LAG() Function in SQL

Learn via video course
FREE
View all courses
DBMS Course - Master the Fundamentals and Advanced Concepts
DBMS Course - Master the Fundamentals and Advanced Concepts
by Srikanth Varma
1000
5
Start Learning
DBMS Course - Master the Fundamentals and Advanced Concepts
DBMS Course - Master the Fundamentals and Advanced Concepts
by Srikanth Varma
1000
5
Start Learning
Topics Covered

Overview

In SQL Server (Transact-SQL), LAG() is a window function that allows us to compare the current record value with the value of previous records. This previous record value can be returned on the same record without the use of self-join, thus making it easier to compare.

What is Lag Function in SQL?

SQL Server LAG() is a window function that provides access to a row at a specified offset value that comes before the current row. This implies that with the help of the LAG() function, we can access the data from any of the previous rows on the current row.

The LAG() function is used for comparing the value of the current row with the value of the previous rows. LAG() is an analytic function available after the release of SQL Server 2012. This works similar to the LEAD() function in SQL. In the lead function, we access successive rows, but in the lag function, we access previous rows.

Syntax of Lag Function in SQL

The syntax for LAG() in Transact SQL is:

Parameters of Lag Function in SQL

Expression - this argument takes the column name over which the lag function performs calculations. This is a mandatory argument.

Offset - Optional. It is the number of previous rows from the current row from which the function has to fetch the value. If offset is not specified, the default value is 1.

Default - Optional. This value will be returned if the offset value exceeds the partition's range. If this parameter is not specified, NULL is returned.

PARTITION BY clause - Optional. It divides the resultant set by the FROM clause into groups to which the specified function is applied. If we omit the PARTITION BY clause, the whole resultant set is treated as a single partition by the function.

ORDER BY clause - Optional. It is used to order the data values in each partition. By default, it uses ascending order to sort data.

Return Type of Lag Function in SQL

The return type is the same as the data type of the specified expression. NULL is returned if the expression passed as a parameter is nullable or the default is set to NULL. The return value must be a single value and cannot be another window function.

Examples of Lag Function in SQL

Let us consider a table named Employee that stores employees' salary history. SQL query to create the Employee table is:

Employee Table:

employee_idyearsalary
101201717000.00
101201818190.00
101202019463.30
102201717000.00
102201818360.00
102202019278.00
10320179000.00
10320189720.00
103202010206.00
10420176000.00
10420186060.00
10420206605.40
10520174800.00
10520184992.00
10520205391.36
  • SQL Lag function without a default value

    In the following SQL query, we will use the lag function on the Salary column with offset one. We did not specify any default value in this query.

    Output

    employee_idyearsalaryprevious_salary
    10520174800.00NULL
    10520184992.004800.00
    10520205391.364992.00
    10420176000.005391.36
    10420186060.006000.00
    10420206605.406060.00
    10320179000.006605.40
    10320189720.009000.00
    103202010206.009720.00
    101201717000.0010206.00
    102201717000.0017000.00
    101201818190.0017000.00
    102201818360.0018190.00
    102202019278.0018360.00
    101202019463.3019278.00

    In the output, we can note the following:

    • The first row shows the NULL value for the previous_salary column because it does not have any previous rows.
    • The second row contains the previous_salary row value in the previous_salary column. It takes value from the previous row due to offset value 1.
  • SQL Lag Function with a default value

    In the following SQL query, we will use the Lag function on the Salary column with offset one. We did not specify any default value in this query.

    Output

    employee_idyearsalaryprevious_salary
    10520174800.00NO DATA
    10520184992.004800.00
    10520205391.364992.00
    10420176000.005391.36
    10420186060.006000.00
    10420206605.406060.00
    10320179000.006605.40
    10320189720.009000.00
    103202010206.009720.00
    101201717000.0010206.00
    102201717000.0017000.00
    101201818190.0017000.00
    102201818360.0018190.00
    102202019278.0018360.00
    101202019463.3019278.00

    In the output, the first row shows the NO DATA value instead of NULL for the previous_salary column because it does not have any previous rows.

  • SQL Lag function with OFFSET Value 2

    In the above examples, we used default offset value 1 in the lag function, which takes the value from the previous row. In the example, we use offset value 2.

    Output

    employee_idyearsalaryprevious_salary
    10520174800.00NO DATA
    10520184992.00NO DATA
    10520205391.364800.00
    10420176000.004992.00
    10420186060.005391.36
    10420206605.406000.00
    10320179000.006060.00
    10320189720.006605.40
    103202010206.009000.00
    101201717000.009720.00
    102201717000.0010206.00
    101201818190.0017000.00
    102201818360.0017000.00
    102202019278.0018190.00
    101202019463.3018360.00

    In this example, since the offset value is 2, we have given a default value NO DATA for rows 1 and 2. While row 3 takes value from previous row 1.

  • SQL Lag function with PARTITION BY Clause

    Output

    employee_idyearsalaryprevious_salary
    101201717000.00NO DATA
    101201818190.0017000.00
    101202019463.3018190.00
    102201717000.00NO DATA
    102201818360.0017000.00
    102202019278.0018360.00
    10320179000.00NO DATA
    10320189720.009000.00
    103202010206.009720.00
    10420176000.00NO DATA
    10420186060.006000.00
    10420206605.406060.00
    10520174800.00NO DATA
    10520184992.004800.00
    10520205391.364992.00

    In the above example:

    • The PARTITION BY clause divides the result set into groups by employee_id.
    • Now, for each group, the ORDER BY clause sorts the rows by year column in ascending order.
    • The LAG() function is applied to every row of each group independently. The first row in each group is NO DATA because there is no previous year record for salary. The second and third rows get the salary from the first and second rows and populate them into the previous_salary column.

Conclusion

  • The Transact-SQL LAG() function is implemented to access values from previous rows.
  • The lag function accesses the value from the previous rows on the basis of the defined offset in the function.
  • The PARTITION BY clause in the lag function is used to define a logical boundary of data based on the given requirement.
  • The Lag function uses NULL as the default value for data beyond the partition's scope.
  • LAG() function can be used in combination with Stored Procedures, Common Table Expression (CTEs), and other analytic functions for computation purposes.

See Also: