BigQuery · Aggregate function
BigQuery MAX
MAX returns the largest non-NULL value in a group: the latest timestamp, the highest amount, the alphabetically last string. It works on any orderable type (not ARRAY or STRUCT) and is the usual way to check when a table last received data.
MAX compares values with the type's natural order and skips NULLs. Numbers compare numerically, dates and timestamps chronologically, BOOL with TRUE above FALSE, and strings by Unicode code point, so lowercase letters sort after uppercase ones. The result has the input's type. It is NULL for an empty group or when every value is NULL, and NaN if any FLOAT64 value is NaN.
MAX(loaded_at) is the standard freshness check: compare it with the current time to see how stale a table is. As a window function, MAX(amount) OVER (PARTITION BY customer_id) puts each group's maximum on every row, and MAX(x) OVER (ORDER BY day) gives a running peak.
MAX returns a value, not a row. For other columns of the row with the maximum, use MAX_BY, ARRAY_AGG(t ORDER BY ts DESC LIMIT 1)[OFFSET(0)] or ROW_NUMBER with QUALIFY. To compare columns within one row, use GREATEST, which returns NULL if any argument is NULL.
Syntax
MAX(expression [WHERE where_expression]) [OVER over_clause]Arguments and return value
| Argument | Description |
|---|---|
expression | Any orderable type: numbers, STRING, BYTES, DATE, DATETIME, TIME, TIMESTAMP, BOOL and more. ARRAY, STRUCT, JSON and GEOGRAPHY are not orderable. NULLs are skipped. |
WHERE where_expression | Optional filter inside the call (Preview). |
over_clause | Makes MAX a window function, for example the group maximum on every row or a running peak with ORDER BY. |
Returns: The type of expression. NULL for an empty group or all-NULL input.
Examples
Hours since each table's last load
A fixed timestamp keeps the example reproducible; in a monitoring query use CURRENT_TIMESTAMP(). TIMESTAMP_DIFF counts whole hours, so 12.5 hours shows as 12.
WITH loads AS (
SELECT 'orders' AS table_name, TIMESTAMP '2026-11-23 06:00:00 UTC' AS loaded_at UNION ALL
SELECT 'orders', TIMESTAMP '2026-11-23 09:00:00 UTC' UNION ALL
SELECT 'customers', TIMESTAMP '2026-11-22 23:30:00 UTC'
)
SELECT
table_name,
MAX(loaded_at) AS last_load,
TIMESTAMP_DIFF(TIMESTAMP '2026-11-23 12:00:00 UTC', MAX(loaded_at), HOUR) AS hours_since_load
FROM loads
GROUP BY table_name
ORDER BY table_name;| table_name | last_load | hours_since_load |
|---|---|---|
| customers | 2026-11-22 23:30:00 UTC | 12 |
| orders | 2026-11-23 09:00:00 UTC | 3 |
Numbers stored as strings
Strings compare character by character, so '9' beats '10'.
SELECT
MAX(version) AS max_as_string,
MAX(CAST(version AS INT64)) AS max_as_number
FROM UNNEST(['9', '10', '2']) AS version;| max_as_string | max_as_number |
|---|---|
| 9 | 10 |
Flag each customer's largest order
WITH orders AS (
SELECT 'c1' AS customer_id, 101 AS order_id, 40 AS amount UNION ALL
SELECT 'c1', 102, 95 UNION ALL
SELECT 'c2', 103, 60
)
SELECT
customer_id,
order_id,
amount,
amount = MAX(amount) OVER (PARTITION BY customer_id) AS is_largest_order
FROM orders
ORDER BY order_id;| customer_id | order_id | amount | is_largest_order |
|---|---|---|---|
| c1 | 101 | 40 | false |
| c1 | 102 | 95 | true |
| c2 | 103 | 60 | true |
Running peak
WITH daily AS (
SELECT DATE '2026-11-01' AS day, 310 AS active_users UNION ALL
SELECT DATE '2026-11-02', 450 UNION ALL
SELECT DATE '2026-11-03', 380 UNION ALL
SELECT DATE '2026-11-04', 520
)
SELECT
day,
active_users,
MAX(active_users) OVER (ORDER BY day) AS peak_so_far
FROM daily
ORDER BY day;| day | active_users | peak_so_far |
|---|---|---|
| 2026-11-01 | 310 | 310 |
| 2026-11-02 | 450 | 450 |
| 2026-11-03 | 380 | 450 |
| 2026-11-04 | 520 | 520 |
Common mistakes
- Using MAX to compare columns of one row.
MAX(created_at, updated_at)fails with No matching signature for aggregate function MAX; use GREATEST, keeping in mind that it returns NULL if any argument is NULL. - Taking the maximum of numbers stored as strings.
'9'is greater than'10'. Cast to INT64 or NUMERIC first. - Selecting other columns next to MAX without grouping them.
SELECT customer_id, order_id, MAX(amount) ... GROUP BY customer_idfails with SELECT list expression references column which is neither grouped nor aggregated. Use MAX_BY to get the order ID of the largest order. - Ignoring NaN. One NaN in a FLOAT64 column makes MAX return NaN. Filter with
WHERE NOT IS_NAN(x)if NaN can occur.
Cost and performance
- A freshness check such as
MAX(event_ts)reads the column for every row it scans. On a partitioned table, filter on the partitioning column (for example the last two days) so the query reads only recent partitions; for the newest partition itself, INFORMATION_SCHEMA.PARTITIONS has one row per partition.
FAQ
How do I get the latest record per group in BigQuery?
Use MAX_BY for a single column (MAX_BY(status, updated_at)), ARRAY_AGG(t ORDER BY updated_at DESC LIMIT 1)[OFFSET(0)] for the whole row, or QUALIFY ROW_NUMBER() OVER (PARTITION BY id ORDER BY updated_at DESC) = 1.
What is the difference between MAX and GREATEST?
MAX aggregates one column across rows and skips NULLs. GREATEST compares several values within one row and returns NULL if any of them is NULL.
Does MAX ignore NULL values?
Yes. MAX returns NULL only when every value is NULL or there are no rows. A NaN value, on the other hand, makes the result NaN.
Related
Errors you may hit
More aggregate functions
All BigQuery aggregate functions · All functions · BigQuery error messages
