

# Arg max, arg min, first, and last functions

Use these functions to return a value from one row of a group. The row can be the one with the largest or smallest value of another expression, the first or last row in an order that you set, or any row.

For a description of the sample tables that the examples on this page use, see [SELECT statement in DuckDB](duckdb-statement-select.md).

## `any_value` function
<a name="athena-duckdb-functions-aggregate-any-value"></a>

Returns one non-`NULL` value of `arg` from the input rows. Use it when every row of a group has the same value, or when any one value will do. For example, use it to show a descriptive column next to the grouping column without adding that column to `GROUP BY`.

`any_value` skips `NULL` values. It returns `NULL` only when every value is `NULL` or when there are no input rows. Which value it returns depends on the order in which it reads the rows. Add `ORDER BY` inside the call to return the first non-`NULL` value in that order.

### Syntax
<a name="athena-duckdb-functions-aggregate-any-value-syntax"></a>

```sql
any_value({{arg}} [ ORDER BY {{sort_expression}}
    [ ASC | DESC ] [, ...] ])
```

### Arguments
<a name="athena-duckdb-functions-aggregate-any-value-arguments"></a>

The `any_value` function takes the following arguments:

**{{arg}}**  
The value to return. It can be of any type.

**{{sort\_expression}}**  
An expression that sets the order in which the function reads the rows. For more information about `ORDER BY` in aggregate function calls, see [Aggregate function clauses](duckdb-functions-aggregate-general.md#athena-duckdb-functions-aggregate-aggregate-clauses). This argument is optional.

### Return type
<a name="athena-duckdb-functions-aggregate-any-value-return-type"></a>

The type of `arg`, for example `VARCHAR` for `VARCHAR` input or `DECIMAL(2,1)` for the literal `1.5`.

### Examples
<a name="athena-duckdb-functions-aggregate-any-value-examples"></a>

The following SQL query totals the quantity of each order. Every line of an order has the same customer, so `any_value` returns that customer without adding `customer` to `GROUP BY`.

```sql
SELECT
    order_id,
    any_value(customer) AS customer,
    sum(quantity) AS items
FROM (VALUES
    (1, 'alice', 2),
    (1, 'alice', 3),
    (2, 'bob', 1)
) AS lines(order_id, customer, quantity)
GROUP BY order_id
ORDER BY order_id
```

The query returns the following results.


| order\_id | customer | items | 
| --- | --- | --- | 
| `1` | `alice` | `5` | 
| `2` | `bob` | `1` | 

The following SQL query reads the rows in the order of `o`. The `any_value` function skips the `NULL` in the first row and returns `b`. The `arbitrary` function, an alias of `first`, returns the `NULL`.

```sql
SELECT
    any_value(x ORDER BY o) AS any_value_result,
    arbitrary(x ORDER BY o) AS arbitrary_result
FROM (VALUES (NULL, 1), ('b', 2), ('a', 3)) AS v(x, o)
```

The query returns the following result.


| any\_value\_result | arbitrary\_result | 
| --- | --- | 
| `b` | `NULL` | 

The following SQL query passes only `NULL` values, so `any_value` returns `NULL` and the `IS NULL` test returns `true`.

```sql
SELECT any_value(x) IS NULL AS is_null
FROM (VALUES (CAST(NULL AS INTEGER)), (NULL)) AS v(x)
```

The query returns the following result.


| is\_null | 
| --- | 
| `true` | 

### Considerations
<a name="athena-duckdb-functions-aggregate-any-value-considerations"></a>

Without `ORDER BY`, the query does not define which non-`NULL` value `any_value` returns when the values differ. If you need the same value on every run, add `ORDER BY` inside the call, or use the [`min` function](duckdb-functions-aggregate-general.md#athena-duckdb-functions-aggregate-min) or the [`max` function](duckdb-functions-aggregate-general.md#athena-duckdb-functions-aggregate-max).

### Differences from Athena engine version 3
<a name="athena-duckdb-functions-aggregate-any-value-differences-from-engine-version-3"></a>

To port a query from Athena engine version 3 that uses `arbitrary({{x}})`, replace it with `any_value({{x}})`.

In Athena engine version 3, `arbitrary` skips `NULL` values. In DuckDB, `arbitrary` is an alias of `first`, which returns the value from the first row that it reads even when that value is `NULL`. The `any_value` function skips `NULL` values, as `arbitrary` does in Athena engine version 3.

## `arg_max` function
<a name="athena-duckdb-functions-aggregate-arg-max"></a>

Returns the value of `arg` in the row whose `val` is the largest. With `n`, returns a list of the `arg` values from the `n` rows that have the largest `val` values, from the largest to the smallest.

`arg_max` skips rows where `val` is `NULL` and rows where `arg` is `NULL`. If no row has both values, the result is `NULL`. To return `arg` even when it is `NULL`, use the [`arg_max_null` function](#athena-duckdb-functions-aggregate-arg-max-null). To also keep the rows where `val` is `NULL`, use the [`arg_max_nulls_last` function](#athena-duckdb-functions-aggregate-arg-max-nulls-last).

`argmax` and `max_by` are aliases of `arg_max`. To find the row with the smallest value, use the [`arg_min` function](#athena-duckdb-functions-aggregate-arg-min).

### Syntax
<a name="athena-duckdb-functions-aggregate-arg-max-syntax"></a>

```sql
arg_max({{arg}}, {{val}})
arg_max({{arg}}, {{val}}, {{n}})
```

### Arguments
<a name="athena-duckdb-functions-aggregate-arg-max-arguments"></a>

The `arg_max` function takes the following arguments:

**{{arg}}**  
The expression to return from the chosen row. It can be of any type.

**{{val}}**  
The expression whose largest value chooses the row. It can be any type that can be ordered, such as a number, `VARCHAR`, `DATE`, `TIMESTAMP`, or a row value.

**{{n}}**  
The number of rows to return, as a `BIGINT` greater than `0`. This argument is optional.

### Return type
<a name="athena-duckdb-functions-aggregate-arg-max-return-type"></a>

The type of `arg`, such as `VARCHAR` for `VARCHAR` input. With `n`, a list of that type, such as `VARCHAR[]`.

### Examples
<a name="athena-duckdb-functions-aggregate-arg-max-examples"></a>

The following SQL query returns the customer with the largest order.

```sql
SELECT arg_max(customer, amount) AS top_customer
FROM (VALUES
    ('alice', 120.50),
    ('bob', 75.00),
    ('carol', 42.25),
    ('dave', 310.00)
) AS v(customer, amount)
```

The query returns the following result.


| top\_customer | 
| --- | 
| `dave` | 

The following SQL query uses the `max_by` alias with `n` to return the customers with the two largest orders, largest first.

```sql
SELECT max_by(customer, amount, 2) AS top_customers
FROM (VALUES
    ('alice', 120.50),
    ('bob', 75.00),
    ('carol', 42.25),
    ('dave', 310.00)
) AS v(customer, amount)
```

The query returns the following result.


| top\_customers | 
| --- | 
| `[dave, alice]` | 

The following SQL query shows how `NULL` values change the result. The largest amount, `400.00`, belongs to a row whose customer is `NULL`. The `arg_max` function skips that row and the row with a `NULL` amount, and returns `alice`. The `arg_max_null` function returns the `NULL` customer.

```sql
SELECT
    arg_max(customer, amount) AS arg_max_result,
    arg_max_null(
        customer, amount
    ) AS arg_max_null_result
FROM (VALUES
    ('alice', 120.50),
    ('bob', 75.00),
    (NULL, 400.00),
    ('erin', NULL)
) AS v(customer, amount)
```

The query returns the following result.


| arg\_max\_result | arg\_max\_null\_result | 
| --- | --- | 
| `alice` | `NULL` | 

The following SQL query breaks a tie. `alice` and `dave` both have an amount of `310.00`, and the row value `(amount, order_id)` chooses the row with the larger `order_id`.

```sql
SELECT arg_max(customer, (amount, order_id)) AS result
FROM (VALUES
    (1, 'alice', 310.00),
    (2, 'bob', 75.00),
    (3, 'dave', 310.00)
) AS v(order_id, customer, amount)
```

The query returns the following result.


| result | 
| --- | 
| `dave` | 

### Considerations
<a name="athena-duckdb-functions-aggregate-arg-max-considerations"></a>

If more than one row has the largest `val`, `arg_max` returns `arg` from one of them, and the query does not control which. To break ties, pass a row value as `val`, such as `(amount, order_id)`. Row values compare field by field, so among rows with the same `amount`, the one with the largest `order_id` wins.

A row value such as `(amount, order_id)` is not `NULL` even when `amount` is `NULL`. A `NULL` field compares as larger than every other value. As a result, `arg_max` does not skip a row where `amount` is `NULL`, and chooses it over every row that has an `amount`. If `amount` can be `NULL`, add `FILTER (WHERE amount IS NOT NULL)` after the call.

### Differences from Athena engine version 3
<a name="athena-duckdb-functions-aggregate-arg-max-differences-from-engine-version-3"></a>

In Athena engine version 3, `max_by({{x}}, {{y}})` returns `NULL` when `x` is `NULL` in the row with the largest `y`. In DuckDB, `max_by` is an alias of `arg_max`, which skips rows where `x` is `NULL` and returns `x` from the row with the largest `y` among the other rows. To get the Athena engine version 3 result, use `arg_max_null({{x}}, {{y}})`.

## `arg_max_null` function
<a name="athena-duckdb-functions-aggregate-arg-max-null"></a>

Returns the value of `arg` in the row whose `val` is the largest, and returns `NULL` when `arg` is `NULL` in that row. The function leaves out only the rows where `val` is `NULL`. If every `val` is `NULL`, or if there are no input rows, the result is `NULL`.

Use `arg_max_null` when a `NULL` in `arg` is a meaningful answer. `arg_max` instead skips rows where `arg` is `NULL` and returns a value from another row. To also keep the rows where `val` is `NULL`, use the [`arg_max_nulls_last` function](#athena-duckdb-functions-aggregate-arg-max-nulls-last).

### Syntax
<a name="athena-duckdb-functions-aggregate-arg-max-null-syntax"></a>

```sql
arg_max_null({{arg}}, {{val}})
```

### Arguments
<a name="athena-duckdb-functions-aggregate-arg-max-null-arguments"></a>

The `arg_max_null` function takes the following arguments:

**{{arg}}**  
The expression to return from the chosen row. It can be of any type.

**{{val}}**  
The expression whose largest value chooses the row. It can be any type that can be ordered, such as a number, `VARCHAR`, `DATE`, or `TIMESTAMP`.

### Return type
<a name="athena-duckdb-functions-aggregate-arg-max-null-return-type"></a>

The type of `arg`, such as `VARCHAR` for `VARCHAR` input.

### Examples
<a name="athena-duckdb-functions-aggregate-arg-max-null-examples"></a>

The following SQL query returns the customer of the largest order. The customer of that order is `NULL`, and `arg_max_null` returns that `NULL`.

```sql
SELECT arg_max_null(customer, amount) AS result
FROM (VALUES
    ('alice', 120.50),
    ('bob', 75.00),
    (NULL, 400.00)
) AS v(customer, amount)
```

The query returns the following result.


| result | 
| --- | 
| `NULL` | 

The following SQL query shows that a row with a `NULL` amount is skipped, so the result is `alice`.

```sql
SELECT arg_max_null(customer, amount) AS result
FROM (VALUES
    ('alice', 120.50),
    ('erin', NULL)
) AS v(customer, amount)
```

The query returns the following result.


| result | 
| --- | 
| `alice` | 

### Considerations
<a name="athena-duckdb-functions-aggregate-arg-max-null-considerations"></a>

`arg_max_null` has no form that takes a count of rows. To return several rows and keep `NULL` values of `arg`, use `arg_max_nulls_last({{arg}}, {{val}}, {{n}})`.

## `arg_max_nulls_last` function
<a name="athena-duckdb-functions-aggregate-arg-max-nulls-last"></a>

Orders the rows by `val` from largest to smallest, with the rows where `val` is `NULL` at the end, and returns `arg` from the first row. With `n`, returns a list of the `arg` values from the first `n` rows in that order.

Unlike `arg_max`, this function skips no rows. A `NULL` in `arg` is returned as `NULL`, or as a `NULL` element of the list. Rows where `val` is `NULL` come last, so they appear only when fewer than `n` rows have a non-`NULL` `val`, or, without `n`, when every `val` is `NULL`.

### Syntax
<a name="athena-duckdb-functions-aggregate-arg-max-nulls-last-syntax"></a>

```sql
arg_max_nulls_last({{arg}}, {{val}})
arg_max_nulls_last({{arg}}, {{val}}, {{n}})
```

### Arguments
<a name="athena-duckdb-functions-aggregate-arg-max-nulls-last-arguments"></a>

The `arg_max_nulls_last` function takes the following arguments:

**{{arg}}**  
The expression to return from the chosen rows. It can be of any type.

**{{val}}**  
The expression that orders the rows, largest first. It can be any type that can be ordered, such as a number, `VARCHAR`, `DATE`, or `TIMESTAMP`.

**{{n}}**  
The number of rows to return, as a `BIGINT` greater than `0`. This argument is optional.

### Return type
<a name="athena-duckdb-functions-aggregate-arg-max-nulls-last-return-type"></a>

The type of `arg`, such as `VARCHAR` for `VARCHAR` input. With `n`, a list of that type, such as `VARCHAR[]`.

### Examples
<a name="athena-duckdb-functions-aggregate-arg-max-nulls-last-examples"></a>

The following SQL query returns the customers of the four rows in order of amount, largest first. The largest order has a `NULL` customer, which is kept, and the row with a `NULL` amount comes last.

```sql
SELECT arg_max_nulls_last(customer, amount, 4) AS result
FROM (VALUES
    ('alice', 120.50),
    ('bob', 75.00),
    (NULL, 400.00),
    ('erin', NULL)
) AS v(customer, amount)
```

The query returns the following result.


| result | 
| --- | 
| `[NULL, alice, bob, erin]` | 

The following SQL query has one row, and its amount is `NULL`. `arg_max_nulls_last` still returns that row's customer, and `arg_max_null` returns `NULL`.

```sql
SELECT
    arg_max_nulls_last(
        customer, amount
    ) AS nulls_last_result,
    arg_max_null(customer, amount) AS null_result
FROM (VALUES
    ('erin', CAST(NULL AS DECIMAL(5, 2)))
) AS v(customer, amount)
```

The query returns the following result.


| nulls\_last\_result | null\_result | 
| --- | --- | 
| `erin` | `NULL` | 

## `arg_min` function
<a name="athena-duckdb-functions-aggregate-arg-min"></a>

Returns the value of `arg` in the row whose `val` is the smallest. With `n`, returns a list of the `arg` values from the `n` rows that have the smallest `val` values, from the smallest to the largest.

`arg_min` skips rows where `val` is `NULL` and rows where `arg` is `NULL`. If no row has both values, the result is `NULL`. To return `arg` even when it is `NULL`, use the [`arg_min_null` function](#athena-duckdb-functions-aggregate-arg-min-null). To also keep the rows where `val` is `NULL`, use the [`arg_min_nulls_last` function](#athena-duckdb-functions-aggregate-arg-min-nulls-last).

`argmin` and `min_by` are aliases of `arg_min`. To find the row with the largest value, use the [`arg_max` function](#athena-duckdb-functions-aggregate-arg-max).

### Syntax
<a name="athena-duckdb-functions-aggregate-arg-min-syntax"></a>

```sql
arg_min({{arg}}, {{val}})
arg_min({{arg}}, {{val}}, {{n}})
```

### Arguments
<a name="athena-duckdb-functions-aggregate-arg-min-arguments"></a>

The `arg_min` function takes the following arguments:

**{{arg}}**  
The expression to return from the chosen row. It can be of any type.

**{{val}}**  
The expression whose smallest value chooses the row. It can be any type that can be ordered, such as a number, `VARCHAR`, `DATE`, `TIMESTAMP`, or a row value.

**{{n}}**  
The number of rows to return, as a `BIGINT` greater than `0`. This argument is optional.

### Return type
<a name="athena-duckdb-functions-aggregate-arg-min-return-type"></a>

The type of `arg`, such as `VARCHAR` for `VARCHAR` input. With `n`, a list of that type, such as `VARCHAR[]`.

### Examples
<a name="athena-duckdb-functions-aggregate-arg-min-examples"></a>

The following SQL query returns the customer with the smallest order.

```sql
SELECT
    arg_min(customer, amount) AS smallest_order_customer
FROM (VALUES
    ('alice', 120.50),
    ('bob', 75.00),
    ('carol', 42.25),
    ('dave', 310.00)
) AS v(customer, amount)
```

The query returns the following result.


| smallest\_order\_customer | 
| --- | 
| `carol` | 

The following SQL query uses the `min_by` alias with `n` to return the customers with the two smallest orders, smallest first.

```sql
SELECT
    min_by(
        customer, amount, 2
    ) AS smallest_order_customers
FROM (VALUES
    ('alice', 120.50),
    ('bob', 75.00),
    ('carol', 42.25),
    ('dave', 310.00)
) AS v(customer, amount)
```

The query returns the following result.


| smallest\_order\_customers | 
| --- | 
| `[carol, bob]` | 

The following SQL query shows how `NULL` values change the result. The smallest amount, `10.00`, belongs to a row whose customer is `NULL`. The `arg_min` function skips that row and the row with a `NULL` amount, and returns `bob`. The `arg_min_null` function returns the `NULL` customer.

```sql
SELECT
    arg_min(customer, amount) AS arg_min_result,
    arg_min_null(
        customer, amount
    ) AS arg_min_null_result
FROM (VALUES
    ('alice', 120.50),
    ('bob', 75.00),
    (NULL, 10.00),
    ('erin', NULL)
) AS v(customer, amount)
```

The query returns the following result.


| arg\_min\_result | arg\_min\_null\_result | 
| --- | --- | 
| `bob` | `NULL` | 

### Considerations
<a name="athena-duckdb-functions-aggregate-arg-min-considerations"></a>

If more than one row has the smallest `val`, `arg_min` returns `arg` from one of them, and the query does not control which. To break ties, pass a row value as `val`, such as `(amount, order_id)`. Row values compare field by field, so among rows with the same `amount`, the one with the smallest `order_id` wins.

A row value such as `(amount, order_id)` is not `NULL` even when `amount` is `NULL`. A `NULL` field compares as larger than every other value. As a result, when every `amount` is `NULL`, `arg_min` returns `arg` from one of those rows instead of `NULL`. If `amount` can be `NULL`, add `FILTER (WHERE amount IS NOT NULL)` after the call.

### Differences from Athena engine version 3
<a name="athena-duckdb-functions-aggregate-arg-min-differences-from-engine-version-3"></a>

In Athena engine version 3, `min_by({{x}}, {{y}})` returns `NULL` when `x` is `NULL` in the row with the smallest `y`. In DuckDB, `min_by` is an alias of `arg_min`, which skips rows where `x` is `NULL` and returns `x` from the row with the smallest `y` among the other rows. To get the Athena engine version 3 result, use `arg_min_null({{x}}, {{y}})`.

## `arg_min_null` function
<a name="athena-duckdb-functions-aggregate-arg-min-null"></a>

Returns the value of `arg` in the row whose `val` is the smallest, and returns `NULL` when `arg` is `NULL` in that row. The function leaves out only the rows where `val` is `NULL`. If every `val` is `NULL`, or if there are no input rows, the result is `NULL`.

Use `arg_min_null` when a `NULL` in `arg` is a meaningful answer. `arg_min` instead skips rows where `arg` is `NULL` and returns a value from another row. To also keep the rows where `val` is `NULL`, use the [`arg_min_nulls_last` function](#athena-duckdb-functions-aggregate-arg-min-nulls-last).

### Syntax
<a name="athena-duckdb-functions-aggregate-arg-min-null-syntax"></a>

```sql
arg_min_null({{arg}}, {{val}})
```

### Arguments
<a name="athena-duckdb-functions-aggregate-arg-min-null-arguments"></a>

The `arg_min_null` function takes the following arguments:

**{{arg}}**  
The expression to return from the chosen row. It can be of any type.

**{{val}}**  
The expression whose smallest value chooses the row. It can be any type that can be ordered, such as a number, `VARCHAR`, `DATE`, or `TIMESTAMP`.

### Return type
<a name="athena-duckdb-functions-aggregate-arg-min-null-return-type"></a>

The type of `arg`, such as `VARCHAR` for `VARCHAR` input.

### Examples
<a name="athena-duckdb-functions-aggregate-arg-min-null-examples"></a>

The following SQL query returns the customer of the smallest order. The customer of that order is `NULL`, and `arg_min_null` returns that `NULL`.

```sql
SELECT arg_min_null(customer, amount) AS result
FROM (VALUES
    ('alice', 120.50),
    ('bob', 75.00),
    (NULL, 10.00)
) AS v(customer, amount)
```

The query returns the following result.


| result | 
| --- | 
| `NULL` | 

The following SQL query shows that a row with a `NULL` amount is skipped, so the result is `alice`.

```sql
SELECT arg_min_null(customer, amount) AS result
FROM (VALUES
    ('alice', 120.50),
    ('erin', NULL)
) AS v(customer, amount)
```

The query returns the following result.


| result | 
| --- | 
| `alice` | 

### Considerations
<a name="athena-duckdb-functions-aggregate-arg-min-null-considerations"></a>

`arg_min_null` has no form that takes a count of rows. To return several rows and keep `NULL` values of `arg`, use `arg_min_nulls_last({{arg}}, {{val}}, {{n}})`.

## `arg_min_nulls_last` function
<a name="athena-duckdb-functions-aggregate-arg-min-nulls-last"></a>

Orders the rows by `val` from smallest to largest, with the rows where `val` is `NULL` at the end, and returns `arg` from the first row. With `n`, returns a list of the `arg` values from the first `n` rows in that order.

Unlike `arg_min`, this function skips no rows. A `NULL` in `arg` is returned as `NULL`, or as a `NULL` element of the list. Rows where `val` is `NULL` come last, so they appear only when fewer than `n` rows have a non-`NULL` `val`, or, without `n`, when every `val` is `NULL`.

### Syntax
<a name="athena-duckdb-functions-aggregate-arg-min-nulls-last-syntax"></a>

```sql
arg_min_nulls_last({{arg}}, {{val}})
arg_min_nulls_last({{arg}}, {{val}}, {{n}})
```

### Arguments
<a name="athena-duckdb-functions-aggregate-arg-min-nulls-last-arguments"></a>

The `arg_min_nulls_last` function takes the following arguments:

**{{arg}}**  
The expression to return from the chosen rows. It can be of any type.

**{{val}}**  
The expression that orders the rows, smallest first. It can be any type that can be ordered, such as a number, `VARCHAR`, `DATE`, or `TIMESTAMP`.

**{{n}}**  
The number of rows to return, as a `BIGINT` greater than `0`. This argument is optional.

### Return type
<a name="athena-duckdb-functions-aggregate-arg-min-nulls-last-return-type"></a>

The type of `arg`, such as `VARCHAR` for `VARCHAR` input. With `n`, a list of that type, such as `VARCHAR[]`.

### Examples
<a name="athena-duckdb-functions-aggregate-arg-min-nulls-last-examples"></a>

The following SQL query returns the customers of the four rows in order of amount, smallest first. The smallest order has a `NULL` customer, which is kept, and the row with a `NULL` amount comes last.

```sql
SELECT arg_min_nulls_last(customer, amount, 4) AS result
FROM (VALUES
    ('alice', 120.50),
    ('bob', 75.00),
    (NULL, 10.00),
    ('erin', NULL)
) AS v(customer, amount)
```

The query returns the following result.


| result | 
| --- | 
| `[NULL, bob, alice, erin]` | 

The following SQL query has one row, and its amount is `NULL`. `arg_min_nulls_last` still returns that row's customer, and `arg_min_null` returns `NULL`.

```sql
SELECT
    arg_min_nulls_last(
        customer, amount
    ) AS nulls_last_result,
    arg_min_null(customer, amount) AS null_result
FROM (VALUES
    ('erin', CAST(NULL AS DECIMAL(5, 2)))
) AS v(customer, amount)
```

The query returns the following result.


| nulls\_last\_result | null\_result | 
| --- | --- | 
| `erin` | `NULL` | 

## `first` function
<a name="athena-duckdb-functions-aggregate-first"></a>

Returns the value of `expression` from the first row of the group, even when that value is `NULL`. To choose which row comes first, add `ORDER BY` inside the call. Without `ORDER BY`, the row that the function picks depends on how the query runs, and it can change from one run to the next.

To get the first value that is not `NULL`, use the [`any_value` function](#athena-duckdb-functions-aggregate-any-value). To get the value from the last row, use the [`last` function](#athena-duckdb-functions-aggregate-last).

`arbitrary` is an alias of `first`.

### Syntax
<a name="athena-duckdb-functions-aggregate-first-syntax"></a>

```sql
first({{expression}} [ ORDER BY {{sort_expression}}
    [ ASC | DESC ] [, ...] ])
```

### Arguments
<a name="athena-duckdb-functions-aggregate-first-arguments"></a>

The `first` function takes the following arguments:

**{{expression}}**  
The value to return, of any type.

**{{sort\_expression}}**  
An expression that sets the order of the rows. The function returns `expression` from the first row in this order. This argument is optional.

### Return type
<a name="athena-duckdb-functions-aggregate-first-return-type"></a>

The type of `expression`.

### Examples
<a name="athena-duckdb-functions-aggregate-first-examples"></a>

The following SQL query returns the customer from the row with the largest amount.

```sql
SELECT first(customer ORDER BY amount DESC) AS result
FROM (VALUES
    ('alice', 120.50),
    ('bob', 75.00),
    ('dave', 310.00)
) AS v(customer, amount)
```

The query returns the following result.


| result | 
| --- | 
| `dave` | 

The following SQL query compares `first` and `any_value` when the first row has a `NULL` value. `first` returns `NULL`, and `any_value` returns the first value that is not `NULL`.

```sql
SELECT
    first(x ORDER BY id) AS first_value,
    any_value(x ORDER BY id) AS first_non_null
FROM (VALUES (1, NULL), (2, 'b'), (3, 'c')) AS v(id, x)
```

The query returns the following result.


| first\_value | first\_non\_null | 
| --- | --- | 
| `NULL` | `b` | 

The following SQL query uses the `arbitrary` alias to return the customer with the largest amount in each region.

```sql
SELECT
    region,
    arbitrary(
        customer
        ORDER BY amount DESC
    ) AS top_customer
FROM (VALUES
    ('east', 'alice', 120.50),
    ('west', 'bob', 75.00),
    ('east', 'carol', 42.25),
    ('west', 'erin', 18.75)
) AS v(region, customer, amount)
GROUP BY region
ORDER BY region
```

The query returns the following results.


| region | top\_customer | 
| --- | --- | 
| `east` | `alice` | 
| `west` | `bob` | 

### Differences from Athena engine version 3
<a name="athena-duckdb-functions-aggregate-first-differences-from-engine-version-3"></a>

In DuckDB, `arbitrary` is an alias of `first`. To get the Athena engine version 3 result of `arbitrary({{x}})`, use `any_value({{x}})`.

In Athena engine version 3, `arbitrary` ignores `NULL` values, and returns `NULL` only when every value is `NULL`. In DuckDB, `arbitrary` returns `NULL` when the row that it picks has a `NULL` value.

## `last` function
<a name="athena-duckdb-functions-aggregate-last"></a>

Returns the value of `arg` from the last row that the function reads in the group. To choose which row is last, add `ORDER BY` inside the call, as in `last({{arg}} ORDER BY {{sort_expression}})`. Without `ORDER BY`, the result depends on the order in which rows reach the function, which is not guaranteed.

`last` does not skip `NULL` values. If `arg` is `NULL` in the last row, the result is `NULL`. To get the last value that is not `NULL`, add `FILTER (WHERE {{arg}} IS NOT NULL)`. If the group has no rows, the result is `NULL`. To get the value from the first row instead, use the [`first` function](#athena-duckdb-functions-aggregate-first).

### Syntax
<a name="athena-duckdb-functions-aggregate-last-syntax"></a>

```sql
last({{arg}} [ ORDER BY {{sort_expression}} [ ASC | DESC ]
    [, ...] ]) [ FILTER (WHERE {{condition}}) ]
```

### Arguments
<a name="athena-duckdb-functions-aggregate-last-arguments"></a>

The `last` function takes the following arguments:

**{{arg}}**  
The value to return, of any type.

**{{sort\_expression}}**  
An expression that sets the order of the rows. The function returns `arg` from the row that sorts last. This argument is optional.

**{{condition}}**  
The condition that selects the rows to read, as a `BOOLEAN` expression. The function reads only the rows where `condition` is `true`. This argument is optional.

### Return type
<a name="athena-duckdb-functions-aggregate-last-return-type"></a>

The type of `arg`.

### Examples
<a name="athena-duckdb-functions-aggregate-last-examples"></a>

The following SQL query returns the value of `x` from the row with the largest `y`.

```sql
SELECT last(x ORDER BY y) AS result
FROM (VALUES (10, 1), (20, 3), (30, 2)) AS v(x, y)
```

The query returns the following result.


| result | 
| --- | 
| `20` | 

The following SQL query shows that `last` returns the `NULL` value from the last row. With `FILTER`, the function skips that row and returns `20`, the last value that is not `NULL`.

```sql
SELECT
    last(x ORDER BY y) AS with_nulls,
    last(
        x
        ORDER BY y
    ) FILTER (WHERE x IS NOT NULL) AS without_nulls
FROM (VALUES (10, 1), (20, 2), (NULL, 3)) AS v(x, y)
```

The query returns the following result.


| with\_nulls | without\_nulls | 
| --- | --- | 
| `NULL` | `20` | 

The following SQL query returns the customer of the last order in each region of a table.

```sql
SELECT
    region,
    last(customer ORDER BY order_id) AS last_customer
FROM sales_db.sales_parquet
GROUP BY region
ORDER BY region
```

The query returns the following results.


| region | last\_customer | 
| --- | --- | 
| `east` | `frank` | 
| `north` | `dave` | 
| `west` | `erin` | 

### Considerations
<a name="athena-duckdb-functions-aggregate-last-considerations"></a>

`IGNORE NULLS` is not accepted in an aggregate call such as `last({{arg}} IGNORE NULLS)`, and the query fails. Use `FILTER (WHERE {{arg}} IS NOT NULL)` instead.