# lead

> For the complete documentation index, see [llms.txt](https://docs.redpanda.com/llms.txt). Component-specific: [cloud-data-platform-full.txt](https://docs.redpanda.com/cloud-data-platform-full.txt)

---
title: lead
latest-operator-version: v26.1.4
latest-console-tag: v3.7.3
latest-connect-version: 4.93.0
latest-redpanda-tag: v26.1.9
docname: sql/sql-functions/window-functions/lead
page-component-name: cloud-data-platform
page-version: master
page-component-version: master
page-component-title: Cloud
page-relative-src-path: sql/sql-functions/window-functions/lead.adoc
page-edit-url: https://github.com/redpanda-data/cloud-docs/edit/main/modules/reference/pages/sql/sql-functions/window-functions/lead.adoc
description: The `lead()` window function takes a column and an integer offset as arguments, and returns the value of the cell in the column located at the specified number of rows after the current row.
page-topic-type: reference
page-git-created-date: "2026-05-26"
page-git-modified-date: "2026-05-26"
---

<!-- Source: https://docs.redpanda.com/cloud-data-platform/reference/sql/sql-functions/window-functions/lead.md -->

The `lead()` window function takes a column and an integer offset as arguments, and returns the value of the cell in the column located at the specified number of rows after the current row. It can be used with all [data types supported by Redpanda SQL](https://docs.redpanda.com/cloud-data-platform/reference/sql/sql-data-types/)

## [](#syntax)Syntax

The syntax for this function is:

```sql
LEAD (expression, offset, default)
OVER (
    [PARTITION BY partition_expression, ... ]
    ORDER BY sort_expression [ASC | DESC], ...
)
```

The function returns a value of the same type as the input. If no row meets the offset criteria, the function returns the specified default value, which must be of a type compatible with the input.

## [](#parameters)Parameters

-   `expression`: Column to reference.

-   `offset`: Optional. Number of rows ahead of the current row. Defaults to `1`.

-   `default`: Optional. Value to return if the `offset` is out of range. Defaults to `NULL`.


## [](#examples)Examples

The following examples use the `winsales` table that stores details about some sales transactions:

```sql
CREATE TABLE winsales(
    salesid int,
    dateid date,
    sellerid int,
    buyerid text,
    qty int,
    qty_shipped int);
INSERT INTO winsales VALUES
    (30001, '8/2/2003', 3, 'b', 10, 10),
    (10001, '12/24/2003', 1, 'c', 10, 10),
    (10005, '12/24/2003', 1, 'a', 30, null),
    (40001, '1/9/2004', 4, 'a', 40, null),
    (10006, '1/18/2004', 1, 'c', 10, null),
    (20001, '2/12/2004', 2, 'b', 20, 20),
    (40005, '2/12/2004', 4, 'a', 10, 10),
    (20002, '2/16/2004', 2, 'c', 20, 20),
    (30003, '4/18/2004', 3, 'b', 15, null),
    (30004, '4/18/2004', 3, 'b', 20, null),
    (30007, '9/7/2004', 3, 'c', 30, null);
```

### [](#leadexpression-offset)Lead(expression, offset)

This example executes the `lead()` function with expression and offset parameters' values specified:

```sql
SELECT buyerid, dateid, qty,
  LEAD(qty,1) OVER (ORDER BY buyerid, dateid) AS next_qty
FROM winsales WHERE buyerid = 'c'
ORDER BY buyerid, dateid;
```

This query returns the buyer ID, date ID, quantity and previous quantity for all rows with buyer ID equal to `c`:

```sql
  buyerid |   dateid   | qty | next_qty
---------+------------+-----+----------
 c       | 2003-12-24 |  10 |       10
 c       | 2004-01-18 |  10 |       20
 c       | 2004-02-16 |  20 |       30
 c       | 2004-09-07 |  30 |
(4 rows)
```

### [](#expression-offset-and-default-specified)Expression, offset and default specified

This example executes the `lead()` function with expression, offset and default parameters' values specified:

```sql
SELECT buyerid, dateid, qty
  LEAD(buyerid,1,'unknown') OVER (ORDER BY dateid) AS next_buyerid
FROM winsales
ORDER BY dateid;
```

The query returns the buyer ID, date ID, quantity and following buyer ID for all rows:

```sql
  buyerid |   dateid   | qty | next_buyerid
---------+------------+-----+--------------
 b       | 2003-08-02 |  10 | c
 c       | 2003-12-24 |  10 | a
 a       | 2003-12-24 |  30 | a
 a       | 2004-01-09 |  40 | c
 c       | 2004-01-18 |  10 | b
 b       | 2004-02-12 |  20 | a
 a       | 2004-02-12 |  10 | c
 c       | 2004-02-16 |  20 | b
 b       | 2004-04-18 |  15 | b
 b       | 2004-04-18 |  20 | c
 c       | 2004-09-07 |  30 | unknown
(11 rows)
```

### [](#time-series-lead-to-compare-next-days-sales-quantity)Time series: `lead()` to compare next day’s sales quantity

This example uses `lead()` to compare each day’s sales quantity (`qty`) with the next day’s quantity, ordered by `dateid`:

```sql
SELECT dateid, qty,
  LEAD(qty) OVER (ORDER BY dateid) AS next_day_qty,
  LEAD(qty) OVER (ORDER BY dateid) - qty AS qty_change
FROM winsales
ORDER BY dateid;
```

The query returns:

```sql
   dateid   | qty | next_day_qty | qty_change
------------+-----+--------------+------------
 2003-08-02 |  10 |           10 |          0
 2003-12-24 |  10 |           30 |         20
 2003-12-24 |  30 |           40 |         10
 2004-01-09 |  40 |           10 |        -30
 2004-01-18 |  10 |           20 |         10
 2004-02-12 |  20 |           10 |        -10
 2004-02-12 |  10 |           20 |         10
 2004-02-16 |  20 |           15 |         -5
 2004-04-18 |  15 |           20 |          5
 2004-04-18 |  20 |           30 |         10
 2004-09-07 |  30 |              |
(11 rows)
```