A topic in the Open Knowledge Graph — a free, open map of 15,290 topics and the order to learn them in.

LAG, LEAD, and OFFSET: Accessing Rows in Windows

College Depth 73 in the knowledge graph ☐ I know this ☆ Set as goal
1topic build on this
332prerequisites beneath it
See this on the map →
Window Functions: Analytical Queries→→Ranking Functions: ROW_NUMBER, RANK, DENSE_RANK
sql window-functions row-access analytics

Core Idea

LAG accesses a previous row in the window, LEAD accesses a following row, and FIRST_VALUE/LAST_VALUE access specific rows within a frame. These enable row-to-row comparisons and sequential analysis.

Explainer

From your introduction to window functions, you know that OVER defines a window of related rows and that window functions compute values across that window without collapsing the result set. LAG, LEAD, and related offset functions solve a specific problem that is awkward without them: accessing a value from a different row in the same result set. Before window functions existed, computing "this month's revenue minus last month's revenue" required a self-join — joining the table to itself on an offset date. LAG and LEAD replace that pattern with a single, readable expression.

LAG(column, offset, default) looks backward. Given rows ordered by some column, `LAG(revenue, 1)` on each row returns the revenue from the previous row. The offset defaults to 1 but can be any positive integer — `LAG(revenue, 3)` looks three rows back. The optional third argument provides a default when there is no previous row (the first row in the window has nothing to look back at, so without a default you get NULL). LEAD is the mirror: `LEAD(revenue, 1)` looks one row forward. Together they let you compute differences, growth rates, and trends in a single pass: `revenue - LAG(revenue, 1) OVER (ORDER BY month)` gives you month-over-month change on every row.

FIRST_VALUE and LAST_VALUE access the first or last row within the current window frame rather than at a fixed offset. `FIRST_VALUE(price) OVER (PARTITION BY product ORDER BY sale_date)` gives you the earliest recorded price for each product, repeated on every row — useful for computing how far the current price has moved from its starting point. LAST_VALUE requires care: the default frame is `ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW`, so LAST_VALUE just returns the current row's value. To get the actual last row in the partition, you need to extend the frame: `ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING`. NTH_VALUE(column, n) generalizes this further, returning the value from the nth row in the frame.

The key mental model is that these functions turn a tabular result into something you can navigate positionally — forward, backward, or to specific landmarks — without restructuring the query. PARTITION BY resets the navigation for each group (so LAG across a partition boundary returns NULL or the default, not a value from a different group), and ORDER BY determines which direction "previous" and "next" mean. Any time you find yourself writing a self-join to compare a row with its neighbor, LAG or LEAD is almost certainly the cleaner solution.

Practice Questions 5 questions

Prerequisite Chain

Understanding Zero → The Number Zero → Counting to Five → Counting to 10 → Counting to 20 → Counting a Set of Objects Up to 20 → Cardinality: The Last Number Counted → Matching Numerals to Quantities → Subitizing Small Quantities → Addition Within 10 → Number Bonds to 10 → Addition Within 20 → Doubles and Near Doubles → Doubles Facts Within 10 → Near Doubles Facts Within 20 → Mental Math Strategies for Addition → Mental Math: Adding and Subtracting Tens → Addition Within 100 → Repeated Addition as Multiplication → Multiplication as Equal Groups → Multiplication: Arrays → Basic Multiplication Facts (0s, 1s, 2s, 5s, 10s) → Multiplication Facts Within 100 → Division as Equal Sharing → Division as Grouping (Measurement Division) → Division: Grouping (Repeated Subtraction) Model → Division: Fair Sharing Model → Division as Equal Sharing → Division as Grouping → Basic Division Facts → Division Facts Within 100 → Multiplication and Division Fact Families → Relationship Between Multiplication and Division → Division Facts as Inverse of Multiplication → Remainders and Quotients in Division → Division Word Problems → Multi-Step Word Problems → Solving Multi-Step Word Problems → Multiplication Word Problems → Division Word Problems → Introduction to Long Division → Factors and Multiples → Prime and Composite Numbers → Equivalent Fractions → Relating Fractions and Decimals → Decimal Place Value → Integers and the Number Line → Comparing and Ordering Integers → Absolute Value → Adding Integers → Subtracting Integers → Multiplying Integers → Introduction to Exponents → Order of Operations → Integer Order of Operations → Variable Expressions → The Distributive Property → Variables and Expressions Review → Introduction to Polynomials → Adding and Subtracting Polynomials → Multiplying Polynomials → Factorial → Permutations → Combinations → Counting Principles: Addition and Multiplication Rules → Introduction to Graph Theory → Propositional Logic Foundations → Logical Equivalences → Set Operations: Union, Intersection, and Complement → Relational Algebra → SQL: SELECT Statement and Basic Queries → SQL Aggregation and GROUP BY → Window Functions: Analytical Queries → LAG, LEAD, and OFFSET: Accessing Rows in Windows

Longest path: 74 steps · 332 total prerequisite topics

Prerequisites (1)

Leads To (1)