Skip to content

LAG

LAG(value_expression, offset, default_value) OVER (PARTITION BY column_name ORDER BY column_name)

Section titled “LAG(value_expression, offset, default_value) OVER (PARTITION BY column_name ORDER BY column_name)”
  • value_expression: This refers to the expression that will be returned as the result. It can be a column or an arithmetic operation between columns.
  • offset: This is the position of the previous row to return in comparison to the current row. The position is 1-based, meaning 1 refers to the first row, 2 refers to the second row, and so on. The offset must be a non-negative integer.
  • default_value: If no row is found at the offset position or the value is NULL, the default value will be returned. The type of this default value must match or be implicitly convertible to the value expression type.
  • partition by column_name: This clause divides the result set into partitions. The LAG function is applied within each partition independently.
  • order by column_name: This clause specifies the order of the rows in each partition. The LAG function uses this order to determine which row is the ‘lagging’ row.
CREATE TABLE sales (
id INT AUTO_INCREMENT,
year INT NOT NULL,
sale_amount INT NOT NULL,
PRIMARY KEY (id)
);
INSERT INTO sales(year, sale_amount)
VALUES(2018, 100),
(2019, 200),
(2020, 300);
SELECT year,
LAG(sale_amount) OVER (ORDER BY year) as previous_year_sale
FROM sales;
year | previous_year_sale
-----|-------------------
2018 | NULL
2019 | 100
2020 | 200

The LAG() function is a window function in SQL that allows you to look at a row that is a certain number of rows behind the current one. In the given example, the LAG() function returns the sale_amount from the previous year. For the first row (year 2018), there is no previous sale_amount, so the value is NULL. The sale_amount for the year 2019 is 100 (from the year 2018), and the sale_amount for the year 2020 is 200 (from the year 2019).