Request a demo

BigQuery · Math function

BigQuery GREATEST

GREATEST(x1, x2, ...) returns the largest of its arguments within a single row, for example the latest of several date columns. If any argument is NULL, the result is NULL, unlike PostgreSQL. To find the maximum across rows, use MAX instead.

GoogleSQL for BigQuery · Checked against the official reference in November 2026

GREATEST compares values across columns or expressions of the same row and returns the largest one, using the same ordering as ORDER BY. It works with any orderable type: numbers, strings, dates and timestamps. The arguments must share a supertype: INT64 and FLOAT64 mix, but a DATE and a TIMESTAMP don't, and the query fails with No matching signature for function GREATEST.

A single NULL argument makes the result NULL. PostgreSQL ignores NULL arguments in GREATEST, so migrated queries can start returning NULLs. To ignore NULLs in BigQuery, take MAX over an array of the values: (SELECT MAX(v) FROM UNNEST([a, b, c]) AS v). For FLOAT64, a NaN argument makes the result NaN.

Strings compare by Unicode code point, so uppercase letters sort before lowercase ones and GREATEST('apple', 'Banana') returns apple. Together with LEAST, GREATEST clamps a value into a range: LEAST(GREATEST(score, 0), 100).

Syntax

GREATEST(X1, ..., XN)

Arguments and return value

ArgumentDescription
X1, ..., XNOne or more values of an orderable type that share a supertype. A NULL in any of them makes the result NULL.

Returns: The common supertype of the arguments: the largest value, NULL if any argument is NULL, or NaN if any FLOAT64 argument is NaN.

Examples

Latest activity across columns

WITH accounts AS (
  SELECT 'acc-1' AS account_id, DATE '2026-11-02' AS last_login, DATE '2026-11-19' AS last_purchase, DATE '2026-10-30' AS last_ticket UNION ALL
  SELECT 'acc-2', DATE '2026-11-21', DATE '2026-09-14', DATE '2026-11-05'
)
SELECT
  account_id,
  GREATEST(last_login, last_purchase, last_ticket) AS last_activity
FROM accounts
ORDER BY account_id;
Result
account_idlast_activity
acc-12026-11-19
acc-22026-11-21

NULL arguments, and how to ignore them

One NULL makes GREATEST return NULL. MAX over an UNNEST of the values skips NULLs.

WITH accounts AS (
  SELECT 'acc-3' AS account_id, DATE '2026-11-10' AS last_login, CAST(NULL AS DATE) AS last_purchase
)
SELECT
  account_id,
  GREATEST(last_login, last_purchase) AS greatest_result,
  (SELECT MAX(d) FROM UNNEST([last_login, last_purchase]) AS d) AS ignoring_nulls
FROM accounts;
Result
account_idgreatest_resultignoring_nulls
acc-3NULL2026-11-10

Clamp a value into a range

WITH scores AS (
  SELECT raw_score FROM UNNEST([-5, 42, 130]) AS raw_score
)
SELECT
  raw_score,
  LEAST(GREATEST(raw_score, 0), 100) AS clamped_score
FROM scores
ORDER BY raw_score;
Result
raw_scoreclamped_score
-50
4242
130100

Strings and mixed numeric types

'B' (code point 66) sorts before 'a' (97), so apple wins. INT64 and FLOAT64 arguments return FLOAT64.

SELECT
  GREATEST('apple', 'Banana') AS by_code_point,
  GREATEST(10, 7.5) AS mixed_numeric;
Result
by_code_pointmixed_numeric
apple10.0

Common mistakes

  • Expecting NULLs to be skipped as in PostgreSQL. For two columns use GREATEST(IFNULL(a, b), IFNULL(b, a)); for more, take MAX over UNNEST.
  • Writing MAX(a, b). It fails with No matching signature for aggregate function MAX, because MAX aggregates rows; GREATEST compares columns.
  • Mixing DATE and TIMESTAMP. Convert first, for example GREATEST(TIMESTAMP(order_date), shipped_at).
  • Comparing mixed-case strings. 'apple' is greater than 'Banana'; compare LOWER(...) values when case shouldn't matter.

FAQ

What is the difference between GREATEST and MAX in BigQuery?

GREATEST compares several values within one row and returns one value per row. MAX is an aggregate that returns the largest value of one expression across many rows.

How do I make GREATEST ignore NULL values?

Use (SELECT MAX(v) FROM UNNEST([col_a, col_b, col_c]) AS v), which skips NULLs and returns NULL only when all values are NULL. For two columns, GREATEST(IFNULL(a, b), IFNULL(b, a)) also works.

How do I get the maximum of several columns in BigQuery?

Use GREATEST(col_a, col_b, col_c) when none of them can be NULL. Wrap nullable columns with IFNULL or use the UNNEST pattern.

Errors you may hit

More mathematical functions

All BigQuery mathematical functions · All functions · BigQuery error messages

Observability for every BigQuery table, in about 15 minutes

Masthead detects freshness, volume and schema anomalies, monitors pipeline errors and maps column-level lineage across all your projects, from logs and metadata only.