> ## Documentation Index
> Fetch the complete documentation index at: https://private-7c7dfe99-fix-nav-issues.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

> Calculates the `arg` and `val` value for a maximum `val` value. If there are multiple rows with equal `val` being the maximum, which of the associated `arg` and `val` is returned is not deterministic.

# argAndMax

<h2 id="argAndMax">
  argAndMax
</h2>

Introduced in: v1.1.0

Calculates the `arg` and `val` value for a maximum `val` value.
If there are multiple rows with equal `val` being the maximum, which of the associated `arg` and `val` is returned is not deterministic.
Both parts the `arg` and the `max` behave as [aggregate functions](/reference/functions/aggregate-functions/index), they both [skip `Null`](/reference/functions/aggregate-functions/index#null-processing) during processing and return not `Null` values if not `Null` values are available.

<Note>
  The only difference with `argMax` is that `argAndMax` returns both argument and value.
</Note>

**See also**

* [argMax](/reference/functions/aggregate-functions/argMax)
* [Tuple](/reference/data-types/tuple)

**Syntax**

```sql theme={null}
argAndMax(arg, val)
```

**Arguments**

* `arg` — Argument for which to find the maximum value. [`const String`](/reference/data-types/string)
* `val` — The maximum value. [`(U)Int8/16/32/64`](/reference/data-types/int-uint) or [`Float*`](/reference/data-types/float) or [`Date`](/reference/data-types/date) or [`DateTime`](/reference/data-types/datetime) or [`Tuple`](/reference/data-types/tuple)

**Returned value**

Returns a tuple containing the `arg` value that corresponds to maximum `val` value and the maximum `val` value. [`Tuple`](/reference/data-types/tuple)

**Examples**

**Basic usage**

```sql title=Query theme={null}
SELECT argAndMax(user, salary) FROM salary;
```

```response title=Response theme={null}
┌─argAndMax(user, salary)─┐
│ ('director',5000)       │
└─────────────────────────┘
```

**Extended example with NULL handling**

```sql title=Query theme={null}
CREATE TABLE test
(
    a Nullable(String),
    b Nullable(Int64)
)
ENGINE = Memory AS
SELECT *
FROM VALUES(('a', 1), ('b', 2), ('c', 2), (NULL, 3), (NULL, NULL), ('d', NULL));

SELECT argMax(a, b), argAndMax(a, b), max(b) FROM test;
```

```response title=Response theme={null}
┌─argMax(a, b)─┬─argAndMax(a, b)─┬─max(b)─┐
│ b            │ ('b',2)         │      3 │
└──────────────┴─────────────────┴────────┘
```

**Using Tuple in arguments**

```sql title=Query theme={null}
SELECT argAndMax(a, (b,a)) FROM test;
```

```response title=Response theme={null}
┌─argAndMax(a, (b, a))─┐
│ ('c',(2,'c'))        │
└──────────────────────┘
```

**See also**

* [argMax](/reference/functions/aggregate-functions/argMax)
* [Tuple](/reference/data-types/tuple)
