# first_value

> 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: first_value
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/first-value
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/first-value.adoc
page-edit-url: https://github.com/redpanda-data/cloud-docs/edit/main/modules/reference/pages/sql/sql-functions/window-functions/first-value.adoc
description: The `first_value()` is a window function that retrieves the first value in an ordered set of values within a specified partition.
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/first-value.md -->

The `first_value()` is a window function that retrieves the first value in an ordered set of values within a specified partition.

## [](#syntax)Syntax

The syntax for this function is:

```sql
FIRST_VALUE(expression) OVER (
    [PARTITION BY partition_expression]
    ORDER BY sort_expression
    RANGE BETWEEN start_value AND end_value
)
```

## [](#parameters)Parameters

-   `expression`: Target column or expression.

-   `PARTITION BY`: Optional. Divides the result set into partitions, each processed independently. If omitted, the entire result set is treated as a single partition.

-   `ORDER BY`: Order of rows in each partition to which the function is applied.

-   `RANGE BETWEEN`: Range-based window frame relative to the current row.


## [](#examples)Examples

The following example uses a simplified version of the `film` table from the Pagila database, containing only the `title`, `length` and `rating` columns. The complete schema for the `film` table can be found on the [Pagila](https://www.postgresql.org/ftp/projects/pgFoundry/dbsamples/pagila/pagila/) database website.

```sql
DROP TABLE IF EXISTS film;
CREATE TABLE film (
  title text NOT NULL,
  length int,
  rating text
);
INSERT INTO film(title, length, rating) VALUES
  ('ATTRACTION NEWTON', 83, 'PG-13'),
  ('CHRISTMAS MOONSHINE', 150, 'NC-17'),
  ('DANGEROUS UPTOWN', 121, 'PG'),
  ('KILL BROTHERHOOD', 54, 'G'),
  ('HALLOWEEN NUTS', 47, 'PG-13'),
  ('HOURS RAGE', 122, 'NC-17'),
  ('PIANIST OUTFIELD', 136, 'NC-17'),
  ('PICKUP DRIVING', 77, 'G'),
  ('INDEPENDENCE HOTEL', 157, 'NC-17'),
  ('PRIVATE DROP', 106, 'PG'),
  ('SAINTS BRIDE', 125, 'G'),
  ('FOREVER CANDIDATE', 131, 'NC-17'),
  ('MILLION ACE', 142, 'PG-13'),
  ('SLEEPY JAPANESE', 137, 'PG'),
  ('WRATH MILE', 176, 'NC-17'),
  ('YOUTH KICK', 179, 'NC-17'),
  ('CLOCKWORK PARADISE', 143, 'PG-13');
```

The following query uses the `first_value()` function to retrieve the title of the film with the shortest duration, partitioning results by rating and ordering by length.

```sql
SELECT
  title,
  length,
  rating,
  FIRST_VALUE(title) OVER (
    PARTITION BY rating
    ORDER BY
      length ASC ROWS BETWEEN UNBOUNDED PRECEDING
      AND UNBOUNDED FOLLOWING
  ) AS shortest_film_in_rating
FROM film;
```

The query returns:

```sql
|   title             |   length   |   rating   |   shortest_film_in_rating   |
|---------------------|------------|------------|-----------------------------|
| KILL BROTHERHOOD    | 54         | G          | KILL BROTHERHOOD            |
| PICKUP DRIVING      | 77         | G          | KILL BROTHERHOOD            |
| SAINTS BRIDE        | 125        | G          | KILL BROTHERHOOD            |
| HOURS RAGE          | 122        | NC-17      | HOURS RAGE                  |
| FOREVER CANDIDATE   | 131        | NC-17      | HOURS RAGE                  |
| PIANIST OUTFIELD    | 136        | NC-17      | HOURS RAGE                  |
| CHRISTMAS MOONSHINE | 150        | NC-17      | HOURS RAGE                  |
| INDEPENDENCE HOTEL  | 157        | NC-17      | HOURS RAGE                  |
| WRATH MILE          | 176        | NC-17      | HOURS RAGE                  |
| YOUTH KICK          | 179        | NC-17      | HOURS RAGE                  |
| PRIVATE DROP        | 106        | PG         | PRIVATE DROP                |
| DANGEROUS UPTOWN    | 121        | PG         | PRIVATE DROP                |
| SLEEPY JAPANESE     | 137        | PG         | PRIVATE DROP                |
| HALLOWEEN NUTS      | 47         | PG-13      | HALLOWEEN NUTS              |
| ATTRACTION NEWTON   | 83         | PG-13      | HALLOWEEN NUTS              |
| MILLION ACE         | 142        | PG-13      | HALLOWEEN NUTS              |
| CLOCKWORK PARADISE  | 143        | PG-13      | HALLOWEEN NUTS              |
```