LEAD is a window (analytic) function that lets you access a value from the next row or any number of rows ahead of the current one without using a self-join. It is evaluated after the FROM, WHERE, GROUP BY, and HAVING clauses but before the final ORDER BY, so it can reference aggregated or calculated columns. LEAD requires an OVER clause that defines an ordering; without a deterministic ORDER BY the result is implementation-dependent. You can specify how many rows ahead to peek (offset) and what default to return when the lead position is outside the partition. If offset is NULL, negative, or zero most engines raise an error. LEAD cannot be used in WHERE or GROUP BY, only in SELECT, ORDER BY, or HAVING. Because it executes as a window function, it preserves the number of rows in the original result, making it ideal for time-series, cohort, and sessionized analyses. Performance is generally O(n) over the partition but can degrade if the ORDER BY requires large sorts or if partitions are extremely wide.
value_expression
(any) - Column or expression whose future value you want.offset
(integer) - Number of rows ahead to look (default 1).default
(any) - Value to return when the lead row does not exist (default NULL).LAG, FIRST_VALUE, LAST_VALUE, ROW_NUMBER, NTILE, PERCENT_RANK, WINDOW FUNCTIONS
SQL:1999 (window functions); widely adopted in major databases between 2012-2019
LEAD looks forward to future rows, while LAG retrieves values from previous rows. Syntax and parameters are otherwise identical.
Without ORDER BY the database cannot know which row is "next". Some engines allow the omission but produce non-deterministic results.
Supply the third argument (default). For example: LEAD(sale_amount, 1, 0) returns 0 when no future row exists.
LEAD itself is O(n), but the ORDER BY inside the window may require sorting. Indexing the sort columns can reduce cost.