LAG()

SQL · Window Functions

Theory

LAG(column, offset, default) returns the value of column from a row that is offset rows before the current row, in the order set by the window ORDER BY. With the default offset of 1 it is simply 'the previous row's value'. The first row has no previous row, so LAG returns NULL there, or the default you supply as the third argument.

This is how you compare a row with the one before it without a self-join. For monthly sales ordered by month, revenue - LAG(revenue) OVER (ORDER BY month) is the month-on-month change, and dividing by LAG(revenue) gives growth percentage. Add PARTITION BY region so each region compares only with its own previous month and the first month of every region starts with NULL. LAG ignores the window frame; only PARTITION BY and ORDER BY matter.

Why we use it

Period-over-period comparison is everywhere: daily change in stock price, month-on-month revenue growth, time since a customer's previous order. LAG puts the previous value on the same row so the comparison is a simple subtraction.

When to use it

Use LAG when you need a value from an earlier row in a sequence. Use LEAD to look forward instead. For a value from the first row of the group, use FIRST_VALUE; for a total of earlier rows, use SUM() OVER.

How it works

  1. Rows split by PARTITION BY and sorted by ORDER BY
  2. Take the current row
  3. Step back 'offset' rows within the same partition
  4. Does that row exist? Yes: Return its column value. No: Return the default (NULL if none given).
  5. Use it, e.g. revenue - previous revenue
  6. Previous value added to each row

Syntax

LAG(column, offset, default) OVER (ORDER BY col)

Example

SELECT name, salary,
  LAG(salary, 1) OVER (ORDER BY id) AS prev_salary
FROM employees;

Key points

SQL topics

Introduction to SQL

Querying Data

Sorting Rows

Limiting Rows

Filtering Rows

Joining Multiple Tables

Grouping Rows

SET Operators

Working with Tables

Modifying Data

Views & Triggers

Conditional Expressions

SQL Subqueries

Aggregate Functions

Window Functions

String Functions

Date Functions

Math Functions

Extra Concepts

T
Tech Town
Log InSign Up Free
Home SQL Python DSA Power BI Snowflake Databricks Spark Gen AI Practice Problems Contest Blog

Master SQL from Zero to Pro

All SQL topics in one place. Click any topic in the sidebar — content opens right here without leaving the page.

{{ sectionCount }}
Sections
{{ topicCount }}
Topics
Free
Forever
Hands-on practice Practice SQL Online Solve 150 SQL interview problems in a live editor with instant checking. Start practising →
Section {{ sec.num }}

{{ sec.label }}

SQL  /  {{ current.sectionLabel }}  /  {{ current.name }}

{{ current.name }}

Theory

{{ current.def }}

Why we use it

{{ rich.why }}

When to use it

{{ rich.when }}

Diagram

{{ rich.dg.caption }}

{{ tb.title }}
{{ c.v }}
{{ rich.dg.op }}
➜
{{ rich.dg.output.title }}
{{ c.v }}
{{ nd.label }}
{{ nd.sub }}
{{ nd.label }}
{{ nd.sub }}
{{ rich.dg.left.title }}
{{ it }}
{{ rich.dg.right.title }}
{{ it }}
Flowchart — how it works
{{ nd.text }}
{{ nd.text }}
◇ {{ nd.text }}
Yes → {{ nd.yes }}
No → {{ nd.no }}
Syntax
{{ current.syntax }}
Example
{{ current.example }}
▶ Try It Live — runs against a real sample database
{{ liveError }}
{{ col }}
{{ cell }}
Key Points — click to check off as you learn
  • {{ pt.mark }} {{ pt.text }}

Share this topic