BigQuery · Aggregate function
BigQuery MAX_BY
MAX_BY(x, y) returns the value of x from the row where y is largest, such as the current status of each order: MAX_BY(status, updated_at). It is a synonym for ANY_VALUE(x HAVING MAX y) and replaces a self-join or a ROW_NUMBER subquery.
MAX_BY answers 'what was x on the row with the highest y?' in a single aggregate. MAX_BY(status, updated_at) grouped by order_id returns each order's latest status without joining back to the table or writing a window function. The reference defines it as a synonym for ANY_VALUE(x HAVING MAX y).
Three rules follow from that definition. Rows where y is NULL never win. If several rows share the maximum y, x comes from any one of them, so the answer can change between runs. And if x is NULL on the winning row, the result is NULL: MAX_BY doesn't fall back to an older non-NULL value.
To return several columns from the same row, aggregate a STRUCT, MAX_BY(STRUCT(status, amount), updated_at), and expand it with .*. MAX_BY takes exactly two arguments and is an aggregate only: the optional third argument of Snowflake and Trino (top N as an array) fails with No matching signature, and an OVER clause is rejected. For the top N rows per group use ARRAY_AGG(x ORDER BY y DESC LIMIT n).
Syntax
MAX_BY(x, y)Arguments and return value
| Argument | Description |
|---|---|
x | The value to return. Any type, including a STRUCT of several columns. |
y | The value to maximize. Any orderable type (not ARRAY or STRUCT). Rows where y is NULL are ignored. |
Returns: The type of x. NULL for an empty group, when every y is NULL, or when x is NULL on the row with the largest y.
Examples
Current status of each order
WITH order_events AS (
SELECT 1001 AS order_id, 'created' AS status, TIMESTAMP '2026-11-20 09:00:00 UTC' AS updated_at UNION ALL
SELECT 1001, 'paid', TIMESTAMP '2026-11-20 09:05:00 UTC' UNION ALL
SELECT 1001, 'shipped', TIMESTAMP '2026-11-21 16:40:00 UTC' UNION ALL
SELECT 1002, 'created', TIMESTAMP '2026-11-22 12:00:00 UTC' UNION ALL
SELECT 1002, 'cancelled', TIMESTAMP '2026-11-22 12:30:00 UTC'
)
SELECT
order_id,
MAX_BY(status, updated_at) AS current_status,
MAX(updated_at) AS last_update
FROM order_events
GROUP BY order_id
ORDER BY order_id;| order_id | current_status | last_update |
|---|---|---|
| 1001 | shipped | 2026-11-21 16:40:00 UTC |
| 1002 | cancelled | 2026-11-22 12:30:00 UTC |
Several columns from the same row
A STRUCT keeps the invoice ID and amount together, so both come from the latest invoice. .* expands the STRUCT into columns.
WITH invoices AS (
SELECT 'c1' AS customer_id, 'INV-7' AS invoice_id, 240 AS amount, DATE '2026-10-31' AS issued_on UNION ALL
SELECT 'c1', 'INV-9', 310, DATE '2026-11-30' UNION ALL
SELECT 'c2', 'INV-8', 95, DATE '2026-11-15'
)
SELECT
customer_id,
MAX_BY(STRUCT(invoice_id, amount), issued_on).*
FROM invoices
GROUP BY customer_id
ORDER BY customer_id;| customer_id | invoice_id | amount |
|---|---|---|
| c1 | INV-9 | 310 |
| c2 | INV-8 | 95 |
A NULL on the newest row wins
The latest update cleared the city, so MAX_BY returns NULL. ARRAY_AGG with IGNORE NULLS returns the latest known value instead.
WITH profile_updates AS (
SELECT 'u1' AS user_id, 'Lisbon' AS city, DATE '2026-11-01' AS updated_on UNION ALL
SELECT 'u1', NULL, DATE '2026-11-20'
)
SELECT
user_id,
MAX_BY(city, updated_on) AS city_on_latest_row,
ARRAY_AGG(city IGNORE NULLS ORDER BY updated_on DESC LIMIT 1)[SAFE_OFFSET(0)] AS latest_known_city
FROM profile_updates
GROUP BY user_id;| user_id | city_on_latest_row | latest_known_city |
|---|---|---|
| u1 | NULL | Lisbon |
Common mistakes
- Assuming ties are resolved. If two rows share the maximum
y, eitherxcan come back and the answer can change between runs. Add a tie-breaker withARRAY_AGG(x ORDER BY y DESC, id DESC LIMIT 1)[OFFSET(0)]. - Expecting the latest non-NULL value. If
xis NULL on the newest row, MAX_BY returns NULL; use ARRAY_AGG with IGNORE NULLS. - Passing a third argument.
MAX_BY(x, y, 3)works in Snowflake and Trino but fails in BigQuery with No matching signature. - Using it as a window function.
MAX_BY(x, y) OVER (...)is rejected. UseFIRST_VALUE(x) OVER (PARTITION BY ... ORDER BY y DESC)to put the value on every row.
Cost and performance
- MAX_BY replaces joining a table to its own per-key maximum (
JOIN (SELECT id, MAX(ts) AS ts ... GROUP BY id) USING (id, ts)). That pattern needs a second aggregation and a join, and it returns several rows per key when two rows share the maximum; MAX_BY does the work in one aggregation and returns one value.
FAQ
How do I get the value from the row with the latest date in BigQuery?
Use MAX_BY(value, date_column) in a GROUP BY query. It returns value from the row with the largest date_column in each group.
What is the difference between MAX and MAX_BY?
MAX(updated_at) returns the largest timestamp. MAX_BY(status, updated_at) returns the status on the row that has that timestamp.
Related
Errors you may hit
More aggregate functions
All BigQuery aggregate functions · All functions · BigQuery error messages
