LEAD()

SQL · Window Functions

Theory

LEAD(column, offset, default) is the mirror of LAG: it returns the value from a row that is offset rows after the current row, in the window ORDER BY order. With offset 1 it is 'the next row's value'. The last row of each partition has no next row, so LEAD returns NULL there, or the default you give.

LEAD is handy whenever a row should know what happens next. Ordered by order_date and partitioned by customer_id, LEAD(order_date) gives each order the date of that customer's next order. The gap between the two is the time to repeat purchase, and a NULL next date marks the customer's latest order. It is also used to build validity ranges, such as 'this price is valid from this row's date until the next row's date'. Like LAG, it ignores the window frame.

Why we use it

Some questions look forward: when did the customer come back, how long did a status last, until when was this price valid. LEAD fetches the next value onto the current row so those gaps become simple arithmetic.

When to use it

Use LEAD when you need a value from a later row, such as the next event or the end of a period. Use LAG for earlier rows. A NULL from LEAD is a quick way to spot the last row of each group.

How it works

  1. Rows split by PARTITION BY and sorted by ORDER BY
  2. Take the current row
  3. Step forward '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. next date minus this date
  6. Next value added to each row

Syntax

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

Example

SELECT name, salary,
  LEAD(salary, 1) OVER (ORDER BY id) AS next_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