> ## Documentation Index
> Fetch the complete documentation index at: https://docs.e6data.com/llms.txt
> Use this file to discover all available pages before exploring further.

# MAX_BY

> Returns the value associated with the greatest non-NULL ordering key.

<div className="e6-meta"><span className="e6-eyebrow e6-eyebrow--quiet">Aggregate · Window</span></div>

Rows with NULL keys are ignored. A selected value can be NULL. All-NULL keys yield NULL.

## Syntax

A window function keeps each input row and adds a result for that row. `PARTITION BY` groups rows with matching values; without it, all input rows form one group.

### Aggregate

```sql theme={"theme":{"light":"github-light","dark":"github-dark"}}
max_by(value, key)
```

### Window

```sql theme={"theme":{"light":"github-light","dark":"github-dark"}}
max_by(value, key) OVER ([PARTITION BY key] [ORDER BY ordering_key] [ROWS frame])
```

* Use a frame that includes all rows in the group or starts at the first row (`UNBOUNDED PRECEDING`). Sliding frames that drop earlier rows are not supported. RANGE frames with a numeric or time offset are not supported.

## Arguments

### Aggregate and window

| Name | Type | Required | Description |
| - | - | - | - |
| value | BOOLEAN, TINYINT, SMALLINT, INTEGER, BIGINT, FLOAT, DOUBLE, DECIMAL(p, s), STRING, BINARY, DATE, TIME, TIMESTAMP, ARRAY, MAP or STRUCT | Yes | Expression returned from the selected row. For DECIMAL(p, s), p is precision (total digits) and s is scale (digits after the decimal point). |
| key | BOOLEAN, TINYINT, SMALLINT, INTEGER, BIGINT, FLOAT, DOUBLE, DECIMAL(p, s), STRING, BINARY, DATE, TIME, TIMESTAMP, ARRAY or STRUCT | Yes | Ordering key selecting the row with the maximum key. For DECIMAL(p, s), p is precision (total digits) and s is scale (digits after the decimal point). |

## Returns

* Type: same SQL type as value
* NULL behavior: Rows with NULL keys are ignored. A selected value can be NULL. All-NULL keys yield NULL.
* Empty input: Returns NULL for an empty global aggregate or frame.

## Properties

| Property | Value |
| - | - |
| Deterministic | Partial (Ties retain the first-seen value; distributed input order can choose different tied values.) |
| Supports DISTINCT | No |
| Supports window usage | Partial (Use all rows in the group, or a frame starting at its first row.) |
| Supports window frames | Partial (The frame must start at the first row in the group. See Syntax.) |
| Exact result | Yes |

## Usage notes

* INTEGER and INT mean the same type; STRING means VARCHAR text. NULL can be used with a supported type. Cast NULL explicitly when its type would otherwise be unclear.
* Query tools may report a DECIMAL result as DOUBLE or a TIME result as INTEGER. The returned value still keeps its decimal precision and scale, or its time type. A reported type does not convert the value.
* Supports DISTINCT: Adding DISTINCT does not remove duplicate input values.

## Examples

### Select by key

```sql theme={"theme":{"light":"github-light","dark":"github-dark"}}
WITH t("value", "key") AS (VALUES ('a', 1), ('b', 2)) SELECT max_by("value", "key") AS result FROM t;
```

| result |
| - |
| b |

### NULL selected value

```sql theme={"theme":{"light":"github-light","dark":"github-dark"}}
WITH t("value", "key") AS (VALUES ('a', 1), (CAST(NULL AS VARCHAR), 2)) SELECT max_by("value", "key") AS result FROM t;
```

| result |
| - |
| NULL |

### All-NULL ordering keys

```sql theme={"theme":{"light":"github-light","dark":"github-dark"}}
WITH t("value", "key") AS (VALUES ('a', CAST(NULL AS INT)), ('b', CAST(NULL AS INT))) SELECT max_by("value", "key") AS result FROM t;
```

| result |
| - |
| NULL |

## Limitations

* Use a frame that includes all rows in the group or starts at the first row (`UNBOUNDED PRECEDING`). Sliding frames that drop earlier rows are not supported. RANGE frames with a numeric or time offset are not supported.
* MAX\_BY requires exactly two arguments. A three-argument form is not supported.
* In grouped calls, DISTINCT does not remove duplicate input values. Selection can still vary when rows are tied or their order changes.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.