Request a demo

BigQuery · Aggregate function

BigQuery ANY_VALUE

ANY_VALUE returns the value of an expression from some row of the group, skipping NULLs. Use it for columns that are the same on every row of a group, such as a customer name next to the customer ID, or with HAVING MAX/MIN to take a value from the row with the highest or lowest key.

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

ANY_VALUE picks one row of the group and returns the expression's value from it. Which row is not defined: the choice is nondeterministic, though not random, and can change between runs. That is exactly right when the value is the same on every row of the group, like customer_name per customer_id, and it is the usual fix for the neither grouped nor aggregated error in that situation.

NULLs are skipped whenever the group has at least one non-NULL value, so ANY_VALUE(city) over NULL, 'Berlin', NULL returns Berlin. The result is NULL only when every value is NULL or the group is empty.

With HAVING MAX key or HAVING MIN key, ANY_VALUE first keeps only the rows where key is at its maximum or minimum, then returns a value from one of them: ANY_VALUE(plan HAVING MAX changed_on) is the plan on the latest row. MAX_BY and MIN_BY are shorter synonyms. With HAVING, ANY_VALUE can't be used as a window function.

Syntax

ANY_VALUE(expression [WHERE where_expression] [HAVING {MAX | MIN} having_expression]) [OVER over_clause]

Arguments and return value

ArgumentDescription
expressionAny type. NULL values are skipped unless every value is NULL.
HAVING MAX | HAVING MIN having_expressionLimits the input to rows where having_expression equals its maximum (or minimum) in the group. If several rows tie, any of them can be chosen. Any orderable type except ARRAY.
WHERE where_expressionOptional filter inside the call (Preview).
over_clauseMakes ANY_VALUE a window function. Not allowed together with HAVING MAX or HAVING MIN.

Returns: The type of expression. NULL for an empty group or when every value is NULL.

Examples

A descriptive column next to the grouping key

Each customer ID has one name, so ANY_VALUE returns it without adding the name to GROUP BY.

WITH orders AS (
  SELECT 'c1' AS customer_id, 'Blue Fern Cafe' AS customer_name, 120 AS amount UNION ALL
  SELECT 'c1', 'Blue Fern Cafe', 80 UNION ALL
  SELECT 'c2', 'Harbor Books', 50
)
SELECT
  customer_id,
  ANY_VALUE(customer_name) AS customer_name,
  SUM(amount) AS revenue
FROM orders
GROUP BY customer_id
ORDER BY customer_id;
Result
customer_idcustomer_namerevenue
c1Blue Fern Cafe200
c2Harbor Books50

NULLs are skipped

SELECT ANY_VALUE(city) AS any_city
FROM UNNEST([CAST(NULL AS STRING), 'Berlin', NULL]) AS city;
Result
any_city
Berlin

Value from the latest and the earliest row

WITH subscription_changes AS (
  SELECT 'c1' AS customer_id, 'free' AS plan, DATE '2026-09-01' AS changed_on UNION ALL
  SELECT 'c1', 'pro', DATE '2026-11-10' UNION ALL
  SELECT 'c2', 'pro', DATE '2026-08-15' UNION ALL
  SELECT 'c2', 'free', DATE '2026-10-30'
)
SELECT
  customer_id,
  ANY_VALUE(plan HAVING MAX changed_on) AS current_plan,
  ANY_VALUE(plan HAVING MIN changed_on) AS first_plan
FROM subscription_changes
GROUP BY customer_id
ORDER BY customer_id;
Result
customer_idcurrent_planfirst_plan
c1profree
c2freepro

Common mistakes

  • Using ANY_VALUE on a column that varies within the group. You get one arbitrary value, it can change between runs, and nothing tells you the other rows were different. Check with COUNT(DISTINCT col) > 1 per group first.
  • Treating it as FIRST or LAST. ANY_VALUE has no notion of order; for the first or latest row use HAVING MIN or HAVING MAX, MIN_BY or MAX_BY, or ARRAY_AGG(... ORDER BY ... LIMIT 1).
  • Expecting HAVING MAX to break ties. If two rows share the maximum, either value can come back. When you need a deterministic tie-breaker, use ARRAY_AGG(plan ORDER BY changed_on DESC, change_id DESC LIMIT 1)[OFFSET(0)].
  • Adding OVER to a HAVING call. ANY_VALUE(x HAVING MAX y) OVER (...) is rejected; use FIRST_VALUE or LAST_VALUE with a window ORDER BY instead.

FAQ

What does ANY_VALUE do in BigQuery?

It returns the value of an expression from one row of each group, skipping NULLs. Use it for columns that have a single value per group.

Is ANY_VALUE random?

No. The choice is nondeterministic but not random: you can't predict which row is used and the answer can change between runs, but it isn't a random sample. For sampling, use RAND or TABLESAMPLE.

How do I fix 'SELECT list expression references column which is neither grouped nor aggregated'?

Add the column to GROUP BY, or, if it has one value per group, wrap it in ANY_VALUE. If the values differ and you want a specific one, use MAX_BY, MIN_BY or another aggregate. See the error page.

Errors you may hit

More aggregate functions

All BigQuery aggregate 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.