View a markdown version of this page

Arg max, arg min, first, and last functions - Amazon Athena

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.

any_value function

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

any_value(arg [ ORDER BY sort_expression [ ASC | DESC ] [, ...] ])

Arguments

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. This argument is optional.

Return type

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

Examples

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.

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.

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.

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

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 or the max function.

Differences from Athena engine version 3

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

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. To also keep the rows where val is NULL, use the arg_max_nulls_last function.

argmax and max_by are aliases of arg_max. To find the row with the smallest value, use the arg_min function.

Syntax

arg_max(arg, val) arg_max(arg, val, n)

Arguments

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

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

Examples

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

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.

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.

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.

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

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

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

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.

Syntax

arg_max_null(arg, val)

Arguments

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

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

Examples

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.

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.

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

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

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

arg_max_nulls_last(arg, val) arg_max_nulls_last(arg, val, n)

Arguments

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

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

Examples

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.

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.

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

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. To also keep the rows where val is NULL, use the arg_min_nulls_last function.

argmin and min_by are aliases of arg_min. To find the row with the largest value, use the arg_max function.

Syntax

arg_min(arg, val) arg_min(arg, val, n)

Arguments

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

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

Examples

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

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.

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.

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

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

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

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.

Syntax

arg_min_null(arg, val)

Arguments

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

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

Examples

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.

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.

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

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

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

arg_min_nulls_last(arg, val) arg_min_nulls_last(arg, val, n)

Arguments

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

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

Examples

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.

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.

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

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. To get the value from the last row, use the last function.

arbitrary is an alias of first.

Syntax

first(expression [ ORDER BY sort_expression [ ASC | DESC ] [, ...] ])

Arguments

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

The type of expression.

Examples

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

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.

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.

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

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

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.

Syntax

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

Arguments

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

The type of arg.

Examples

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

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.

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.

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

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.