2020-06-18 08:24:31 +00:00
---
2022-08-28 14:53:34 +00:00
slug: /en/sql-reference/aggregate-functions/reference/argmax
2022-04-09 13:29:05 +00:00
sidebar_position: 106
2020-06-18 08:24:31 +00:00
---
2022-06-02 10:55:18 +00:00
# argMax
2020-06-18 08:24:31 +00:00
2024-04-11 19:51:05 +00:00
Calculates the `arg` value for a maximum `val` value. If there are multiple rows with equal `val` being the maximum, which of the associated `arg` is returned is not deterministic.
2023-06-07 10:07:35 +00:00
Both parts the `arg` and the `max` behave as [aggregate functions ](/docs/en/sql-reference/aggregate-functions/index.md ), they both [skip `Null` ](/docs/en/sql-reference/aggregate-functions/index.md#null-processing ) during processing and return not `Null` values if not `Null` values are available.
2020-06-18 08:24:31 +00:00
2021-02-05 08:14:51 +00:00
**Syntax**
2020-11-24 11:25:45 +00:00
2021-02-05 08:14:51 +00:00
``` sql
argMax(arg, val)
```
2021-02-15 21:33:53 +00:00
**Arguments**
2021-02-05 08:14:51 +00:00
2023-04-19 15:55:29 +00:00
- `arg` — Argument.
- `val` — Value.
2021-02-05 08:14:51 +00:00
**Returned value**
2023-04-19 15:55:29 +00:00
- `arg` value that corresponds to maximum `val` value.
2021-02-05 08:14:51 +00:00
2021-04-20 21:22:29 +00:00
Type: matches `arg` type.
2021-02-05 08:14:51 +00:00
**Example**
Input table:
2020-11-24 11:25:45 +00:00
``` text
┌─user─────┬─salary─┐
│ director │ 5000 │
│ manager │ 3000 │
│ worker │ 1000 │
└──────────┴────────┘
```
2021-02-05 08:14:51 +00:00
Query:
2020-11-24 11:25:45 +00:00
``` sql
2021-04-20 21:22:29 +00:00
SELECT argMax(user, salary) FROM salary;
2020-11-24 11:25:45 +00:00
```
2021-02-05 08:14:51 +00:00
Result:
2020-11-24 11:25:45 +00:00
``` text
2021-04-20 21:22:29 +00:00
┌─argMax(user, salary)─┐
│ director │
└──────────────────────┘
2020-11-24 11:25:45 +00:00
```
2023-06-05 19:19:50 +00:00
**Extended example**
```sql
CREATE TABLE test
(
a Nullable(String),
b Nullable(Int64)
)
ENGINE = Memory AS
SELECT *
2023-06-06 13:30:10 +00:00
FROM VALUES(('a', 1), ('b', 2), ('c', 2), (NULL, 3), (NULL, NULL), ('d', NULL));
2023-06-05 19:19:50 +00:00
select * from test;
┌─a────┬────b─┐
│ a │ 1 │
│ b │ 2 │
│ c │ 2 │
│ ᴺᵁᴸᴸ │ 3 │
│ ᴺᵁᴸᴸ │ ᴺᵁᴸᴸ │
│ d │ ᴺᵁᴸᴸ │
└──────┴──────┘
2023-06-06 13:30:10 +00:00
SELECT argMax(a, b), max(b) FROM test;
2023-06-05 19:19:50 +00:00
┌─argMax(a, b)─┬─max(b)─┐
2023-06-06 13:30:10 +00:00
│ b │ 3 │ -- argMax = 'b' because it the first not Null value, max(b) is from another row!
2023-06-05 19:19:50 +00:00
└──────────────┴────────┘
2023-06-06 13:30:10 +00:00
SELECT argMax(tuple(a), b) FROM test;
2023-06-05 19:19:50 +00:00
┌─argMax(tuple(a), b)─┐
2023-06-06 13:17:55 +00:00
│ (NULL) │ -- The a `Tuple` that contains only a `NULL` value is not `NULL` , so the aggregate functions won't skip that row because of that `NULL` value
2023-06-05 19:19:50 +00:00
└─────────────────────┘
2023-06-06 13:30:10 +00:00
SELECT (argMax((a, b), b) as t).1 argMaxA, t.2 argMaxB FROM test;
2023-06-05 19:19:50 +00:00
┌─argMaxA─┬─argMaxB─┐
2023-06-05 19:57:42 +00:00
│ ᴺᵁᴸᴸ │ 3 │ -- you can use Tuple and get both (all - tuple(*)) columns for the according max(b)
2023-06-05 19:19:50 +00:00
└─────────┴─────────┘
2023-06-06 13:30:10 +00:00
SELECT argMax(a, b), max(b) FROM test WHERE a IS NULL AND b IS NULL;
2023-06-05 19:19:50 +00:00
┌─argMax(a, b)─┬─max(b)─┐
2023-06-06 13:17:21 +00:00
│ ᴺᵁᴸᴸ │ ᴺᵁᴸᴸ │ -- All aggregated rows contains at least one `NULL` value because of the filter, so all rows are skipped, therefore the result will be `NULL`
2023-06-05 19:19:50 +00:00
└──────────────┴────────┘
2023-06-06 13:30:10 +00:00
SELECT argMax(a, (b,a)) FROM test;
2023-06-05 19:19:50 +00:00
┌─argMax(a, tuple(b, a))─┐
2023-06-06 13:30:10 +00:00
│ c │ -- There are two rows with b=2, `Tuple` in the `Max` allows to get not the first `arg`
2023-06-05 19:19:50 +00:00
└────────────────────────┘
2023-06-06 13:30:10 +00:00
SELECT argMax(a, tuple(b)) FROM test;
2023-06-05 19:19:50 +00:00
┌─argMax(a, tuple(b))─┐
2023-06-06 13:30:10 +00:00
│ b │ -- `Tuple` can be used in `Max` to not skip Nulls in `Max`
2023-06-05 19:19:50 +00:00
└─────────────────────┘
```
**See also**
2023-06-07 10:07:35 +00:00
- [Tuple ](/docs/en/sql-reference/data-types/tuple.md )