---
cuppy_hub: salesforce
cuppy_path: reference/official/data360/data-cloud-query-guide/references/dc-sql-reference/first-value-ordered-set.md
cuppy_section: docs
cuppy_doc: W6BN-6D1K
cuppy_doc_uuid: 4681d55a-f464-477f-ac8f-62125d847292
cuppy_permalink: https://cuppy.io/salesforce/d/W6BN-6D1K
cuppy_version: 1
cuppy_updated: 2026-10-23T16:16:52.575Z
cuppy_canonical: https://cuppy.io/salesforce/docs/reference/official/data360/data-cloud-query-guide/references/dc-sql-reference/first-value-ordered-set
cuppy_mcp: https://cuppy.io/mcp
cuppy_body_sha256: 91070dd9a157d18b65422791666f5ade408ba4868acf1d094d552382ac606254
# cuppy_body_sha256 is over the document body ALONE, excluding both generated blocks.
---

> Mirror of <https://developer.salesforce.com/docs/data/data-cloud-query-guide/references/dc-sql-reference/first-value-ordered-set.html> (Salesforce, Data 360 Query Guide), read 2026-10-23. Salesforce's own page is the authority; this copy exists because that site refuses non-browser readers.

## `FIRST_VALUE`

**Applies to:** ✅ Data 360 SQL ✅ Tableau Hyper API

Returns the `<value>` from the first row in an ordered set. Within each `GROUP BY` group (or over the whole result if there is no `GROUP BY`), rows are sorted according to the `WITHIN GROUP (ORDER BY …)` clause. The function then returns the `<value>` from the row that comes first in that ordering.

Use with `GROUP BY` when the ordering is applied **per group**:

*   `<value>`: Expression evaluated on the chosen row. Its result type is the return type of the aggregate.
*   `WITHIN GROUP (ORDER BY …)`: One or more sort expressions (with optional `ASC` / `DESC` and `NULLS FIRST` / `NULLS LAST` per key). Together they define which row is **first** in the ordered set for each aggregate evaluation.

*   `IGNORE NULLS`: Skips rows containing `NULL` values. This is the default behavior.
*   `RESPECT NULLS`: Considers rows containgin `NULL` values.

The type of `<value>`.

**Null handling:**

*   If the aggregate has no input rows (empty group or table), the result is `NULL`.
*   If the first row in the ordered set evaluates the `<value>` to `NULL`, the result is `NULL`.

**Non-determinism:** If multiple rows are tied at the first position (equal sort keys according to the `ORDER BY`), the engine returns the `<value>` from one of those tied rows. The function doesn’t guarantee which row it chooses unless your sort keys uniquely identify a row.

**Ordered-set aggregate:**`first_value` uses the same `WITHIN GROUP (ORDER BY …)` pattern as [`MODE`](https://developer.salesforce.com/docs/data/data-cloud-query-guide/references/dc-sql-reference/mode.html), [`PERCENTILE_CONT`](https://developer.salesforce.com/docs/data/data-cloud-query-guide/references/dc-sql-reference/percentile-cont.html), and [`PERCENTILE_DISC`](https://developer.salesforce.com/docs/data/data-cloud-query-guide/references/dc-sql-reference/percentile-disc.html). It’s not used with an `OVER` clause.

*   **Ordered-set aggregate:**`first_value` uses the same `WITHIN GROUP (ORDER BY …)` pattern as [`MODE`](https://developer.salesforce.com/docs/data/data-cloud-query-guide/references/dc-sql-reference/mode.html), [`PERCENTILE_CONT`](https://developer.salesforce.com/docs/data/data-cloud-query-guide/references/dc-sql-reference/percentile-cont.html), and [`PERCENTILE_DISC`](https://developer.salesforce.com/docs/data/data-cloud-query-guide/references/dc-sql-reference/percentile-disc.html). It is **not** used with an `OVER` clause in this form.
*   **Grouping:** Any non-aggregated columns in the `SELECT` list must appear in `GROUP BY`. The `WITHIN GROUP` ordering applies **within** each group.
*   **Alternative functions:** To return a column from the row with the smallest or largest value in another column without `WITHIN GROUP`, see [`MIN_BY`](https://developer.salesforce.com/docs/data/data-cloud-query-guide/references/dc-sql-reference/min-by.html) and [`MAX_BY`](https://developer.salesforce.com/docs/data/data-cloud-query-guide/references/dc-sql-reference/max-by.html).
*   **Null ordering:** Ties and ordering of `NULL` sort keys follow your `NULLS FIRST` / `NULLS LAST` choices (and dialect defaults where omitted).
*   **Filtering:** You can restrict which rows feed the aggregate using the [`FILTER` clause](https://developer.salesforce.com/docs/data/data-cloud-query-guide/references/dc-sql-reference/filter-clause.html), same as other aggregates.

Return the `product_name` from the row with the earliest `order_date`.

Return the `status` from the earliest `updated_at` row in each `account_id` group.

Each output row has the `status` taken from the first row in that account’s ordered events.

Treat “first” as the row with the largest `score` by sorting descending.

*   [MODE](https://developer.salesforce.com/docs/data/data-cloud-query-guide/references/dc-sql-reference/mode.html)
*   [PERCENTILE_CONT](https://developer.salesforce.com/docs/data/data-cloud-query-guide/references/dc-sql-reference/percentile-cont.html)
*   [PERCENTILE_DISC](https://developer.salesforce.com/docs/data/data-cloud-query-guide/references/dc-sql-reference/percentile-disc.html)
*   [MIN_BY](https://developer.salesforce.com/docs/data/data-cloud-query-guide/references/dc-sql-reference/min-by.html) / [MAX_BY](https://developer.salesforce.com/docs/data/data-cloud-query-guide/references/dc-sql-reference/max-by.html)
*   [Aggregate Functions](https://developer.salesforce.com/docs/data/data-cloud-query-guide/references/dc-sql-reference/aggregate.html)
*   [GROUP BY clause](https://developer.salesforce.com/docs/data/data-cloud-query-guide/references/dc-sql-reference/select-group-by.html)


---
<!-- cuppy -->
You are reading one file. The same content is free over MCP at https://cuppy.io/mcp with no signup: ranked search across all 376 documents in this hub, the merged view when you work in a layer, known workarounds matched to the error you hit, and depth control so you spend tokens on what you need. Start with the `_hello` tool. How to connect: https://cuppy.io/salesforce/ Something wrong in this document? Report it with the `session feedback` tool on the same endpoint.
<!-- /cuppy -->
