MAX Function in BigQuery: Syntax & Examples

Formulas & functions · Updated November 23, 2026

The MAX function in BigQuery returns the maximum value from a set of values, ignoring NULLs. Use MAX(expression) in a SELECT clause or as an aggregate with GROUP BY; for an array, use ARRAY_MAX().

What MAX Does in BigQuery

BigQuery’s MAX is an aggregate function that scans all rows in a group and returns the largest non-NULL value. It works on numeric types (INT64, FLOAT64, NUMERIC, BIGNUMERIC), DATE, TIMESTAMP, TIME, DATETIME, and STRING. For strings, it returns the lexicographically greatest value (based on UTF-8 byte order).

Unlike some SQL dialects, BigQuery’s MAX does not have a DISTINCT variant—it naturally ignores duplicates. It also ignores NULLs; if all values in a group are NULL, the function returns NULL.

Basic Syntax and Examples

The standard syntax is:

MAX(expression) [OVER (...)]

For a simple aggregate over a whole table, use it in a SELECT with no GROUP BY. For example, with a table sales having columns region and amount:

SELECT MAX(amount) AS max_amount FROM sales;

This returns the single highest amount across all rows. To get the max per region, add GROUP BY:

SELECT region, MAX(amount) AS max_amount FROM sales GROUP BY region;

You can also use MAX as a window function with OVER to get a running maximum or a max per partition without collapsing rows:

SELECT region, amount, MAX(amount) OVER (PARTITION BY region) AS region_max FROM sales;

MAX on Different Data Types

MAX works on dates, timestamps, and strings, but the comparison rules matter:

  • DATE and TIMESTAMP: Returns the latest date or timestamp.
  • STRING: Returns the string that sorts last in the collation order. For example, MAX over [‘apple’, ‘banana’, ‘cherry’] returns ‘cherry’.
  • NUMERIC and BIGNUMERIC: Handles decimals exactly; use these for financial data to avoid floating-point errors.
  • ARRAY: Use ARRAY_MAX() instead. For example, ARRAY_MAX([3, 7, 1]) returns 7.

Be careful with FLOAT64: NaN is treated as larger than any other number, so MAX may return NaN if present. Use MAX(IFNULL(column, -inf)) or filter to avoid this.

MAX vs. MIN, and Handling NULLs

MIN is the exact opposite—it returns the smallest non-NULL value. Both ignore NULLs, which is often what you want. But if you need to treat NULL as a value (e.g., as the smallest), use IFNULL or COALESCE before aggregating. For example, to get the max including NULLs as 0:

SELECT MAX(IFNULL(amount, 0)) FROM sales;

A common misconception is that MAX will return NULL if any row has NULL. It does not—it only returns NULL if all values in the group are NULL. If you need to count NULLs separately, use COUNTIF(column IS NULL).

Common Errors and How to Avoid Them

Here are frequent mistakes when using MAX in BigQuery:

  • Type mismatch: Mixing types like INT64 and STRING in the same column raises an error. Ensure the column has a consistent type.
  • Using MAX on an array directly: MAX(array_column) fails because MAX expects a scalar, not an array. Use ARRAY_MAX().
  • Forgetting GROUP BY: If you mix MAX with non-aggregated columns without GROUP BY, you get an error like “SELECT list expression references column … which is neither grouped nor aggregated.”
  • Window function misplacement: When using OVER, the PARTITION BY clause must come before ORDER BY if you use ORDER BY. For MAX, ORDER BY is optional but changes the frame.

Performance and Cost Considerations

BigQuery charges per query based on data scanned. Using MAX on a column scans that entire column unless the table is clustered or partitioned. To minimize cost:

  • Filter with WHERE to reduce rows before MAX runs.
  • Use partitioning on date columns so queries scan only relevant partitions.
  • If you run MAX frequently on a large table, consider a materialized view that precomputes the max.

MAX itself is a simple aggregation and is generally fast. The cost is dominated by how much data you scan, not the function itself.

Alternatives to MAX

Depending on what you need, other functions may be better:

  • ARRAY_MAX: For arrays, as mentioned.
  • GREATEST: Returns the largest value from a list of expressions, not from a column. Example: GREATEST(a, b, c).
  • LAST_VALUE with IGNORE NULLS: In window functions, you can get the last non-null value in an ordered partition, which is similar but respects order.
  • MAX_BY: Returns the value of a column associated with the maximum of another column. For example, MAX_BY(name, score) gives the name of the person with the highest score.

Frequently Asked Questions

Can I use MAX with GROUP BY on multiple columns?

Yes, you can group by multiple columns, and MAX will return the maximum within each combination. For example, GROUP BY region, product gives the max amount per region-product pair.

How do I get the second highest value?

Use a window function with DENSE_RANK or ROW_NUMBER. For example, to get the second highest amount overall: SELECT amount FROM (SELECT amount, ROW_NUMBER() OVER (ORDER BY amount DESC) rn FROM sales) WHERE rn = 2.

Does MAX work on STRUCT or JSON?

No, MAX does not work on STRUCT or JSON directly. You need to extract a scalar field first, e.g., MAX(json_column.field) if the JSON is stored as a STRUCT, or use JSON_EXTRACT_SCALAR to get a string before aggregating.

What is the difference between MAX and MAX_BY?

MAX returns the maximum value itself. MAX_BY returns the value of another column in the row that contains the maximum. For example, MAX(score) gives 95, while MAX_BY(name, score) gives the name of the person with score 95.

How do I get the max date per ID?

Use GROUP BY: SELECT id, MAX(date) FROM table GROUP BY id. If you need other columns from that row, use a window function or ARRAY_AGG with ORDER BY and LIMIT 1.

Why does MAX return NULL when I expect a number?

If all values in the group are NULL, MAX returns NULL. Also, if the column contains only NULLs, the result is NULL. Check your data for NULLs and use IFNULL or COALESCE if you want a default.

Related

Try it: the Formula Explainer: paste any formula and see each function and reference in plain English.

More in Formulas & functions

Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources

Spotted an error in this guide? Let us know.