---
cuppy_hub: salesforce
cuppy_path: reference/official/data360/data-cloud-query-guide/references/dc-sql-reference/max-by.md
cuppy_section: docs
cuppy_doc: XR7J-N1AM
cuppy_doc_uuid: 8d20b149-63e7-4220-bf58-943bec7f1982
cuppy_permalink: https://cuppy.io/salesforce/d/XR7J-N1AM
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/max-by
cuppy_mcp: https://cuppy.io/mcp
cuppy_body_sha256: cae03cbae36d586e59be0259cd5c21662611096cd7ca10b63c59cf9a1a604b86
# 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/max-by.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.

## `MAX_BY`

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

Returns the `<value>` from the row whose `<key>` is greatest among all rows in the aggregate (or within each group when used with `GROUP BY`).

*   `<value>`: Any expression whose result is returned when its row wins the comparison. The aggregate result type matches this expression’s type.
*   `<key>`: The expression `MAX-BY` uses to order the rows. The type must support comparison.

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

The `<value>` from the row with the maximum `<key>` among considered rows.

**Null handling:**

*   You can combine `max_by` with other aggregates, `GROUP BY`, `HAVING`, subqueries, and expressions in the arguments (for example, `max_by(upper(name), score * 2)`).
*   `max_by` only supports a single value to sort on. To sort using multiple sort criteria, use [FIRST_VALUE WITHIN GROUP](https://developer.salesforce.com/docs/data/data-cloud-query-guide/references/dc-sql-reference/first-value-ordered-set.html).
*   If the aggregate has no input rows (empty table or empty group), the result is `NULL`.
*   If every row has a `NULL` key, those rows are skipped; if no row has a non-`NULL` key, the result is `NULL`.
*   If the winning row’s `<value>` is `NULL`, the result is `NULL` (the key still determines which row wins).

**Tie-breaking:** If multiple rows share the same maximum key, one of the tied `<value>` results is returned; which tied row wins is implementation-dependent.

*   `DISTINCT` is not supported (for example, `max_by(DISTINCT name, score)` is a syntax error).
*   Rows with a `NULL``<key>` are ignored when searching for the maximum key.
*   You can combine `max_by` with other aggregates, `GROUP BY`, `HAVING`, subqueries, and expressions in the arguments (for example, `max_by(upper(name), score * 2)`).

Returns the name and score from the row with the highest `score`.

Returns `'David'`.

Uses `GROUP BY` so each region gets its own arg-max product and returns the regions and their maximum revenue.

Returns one row per region. For example `South` / `'A'` and `North` / `'B'`.

Keys that are `NULL` are skipped. In this example, the largest non-`NULL` key is `15`, and that row’s `value` is `NULL`.

Returns `NULL`.

*   [`MIN_BY`](https://developer.salesforce.com/docs/data/data-cloud-query-guide/references/dc-sql-reference/min-by.html)
*   [`MAX`](https://developer.salesforce.com/docs/data/data-cloud-query-guide/references/dc-sql-reference/max.html)
*   [Aggregate Functions](https://developer.salesforce.com/docs/data/data-cloud-query-guide/references/dc-sql-reference/aggregate.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 -->
