Request a demo

BigQuery · Aggregate function

BigQuery MIN_BY

MIN_BY(x, y) returns the value of x from the row where y is smallest, such as each user's first marketing channel: MIN_BY(channel, visited_at). It is a synonym for ANY_VALUE(x HAVING MIN y); rows with a NULL y are ignored and ties return any matching row.

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

MIN_BY answers 'what was x on the row with the lowest y?' in one aggregate: the first-touch channel of a user, the cheapest supplier of a product, the plan a customer started on. The reference defines it as a synonym for ANY_VALUE(x HAVING MIN y), so it needs no window function, self-join or subquery.

Rows where y is NULL are ignored, so a missing price can't be the cheapest. If several rows tie for the minimum, x comes from any one of them and the choice can change between runs. If x is NULL on the winning row, the result is NULL.

To return several columns from the winning row, aggregate a STRUCT: MIN_BY(STRUCT(supplier, lead_time_days), price).*. MIN_BY takes exactly two arguments and can't be used with OVER; for the bottom N per group use ARRAY_AGG(x ORDER BY y LIMIT n), and for a value on every row use FIRST_VALUE.

Syntax

MIN_BY(x, y)

Arguments and return value

ArgumentDescription
xThe value to return. Any type, including a STRUCT of several columns.
yThe value to minimize. 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 smallest y.

Examples

First-touch channel per user

WITH visits AS (
  SELECT 'u1' AS user_id, 'newsletter' AS channel, TIMESTAMP '2026-11-03 08:15:00 UTC' AS visited_at UNION ALL
  SELECT 'u1', 'paid_search', TIMESTAMP '2026-11-01 19:40:00 UTC' UNION ALL
  SELECT 'u1', 'direct', TIMESTAMP '2026-11-10 12:00:00 UTC' UNION ALL
  SELECT 'u2', 'organic', TIMESTAMP '2026-11-05 07:30:00 UTC'
)
SELECT
  user_id,
  MIN_BY(channel, visited_at) AS first_touch_channel,
  MIN(visited_at) AS first_visit
FROM visits
GROUP BY user_id
ORDER BY user_id;
Result
user_idfirst_touch_channelfirst_visit
u1paid_search2026-11-01 19:40:00 UTC
u2organic2026-11-05 07:30:00 UTC

Cheapest offer, ignoring missing prices

supplier_b has no price, so it can't win. The STRUCT returns the supplier and the lead time of the same offer.

WITH offers AS (
  SELECT 'sku-1' AS sku, 'supplier_a' AS supplier, 12.50 AS price, 3 AS lead_time_days UNION ALL
  SELECT 'sku-1', 'supplier_b', NULL, 1 UNION ALL
  SELECT 'sku-1', 'supplier_c', 11.90, 7
)
SELECT
  sku,
  MIN_BY(STRUCT(supplier, lead_time_days), price).*,
  MIN(price) AS lowest_price
FROM offers
GROUP BY sku;
Result
skusupplierlead_time_dayslowest_price
sku-1supplier_c711.9

Plan a customer started on

WITH subscription_changes AS (
  SELECT 'c1' AS customer_id, 'free' AS plan, DATE '2026-03-01' AS changed_on UNION ALL
  SELECT 'c1', 'pro', DATE '2026-07-14' UNION ALL
  SELECT 'c2', 'team', DATE '2026-05-20'
)
SELECT
  customer_id,
  MIN_BY(plan, changed_on) AS first_plan,
  MAX_BY(plan, changed_on) AS current_plan
FROM subscription_changes
GROUP BY customer_id
ORDER BY customer_id;
Result
customer_idfirst_plancurrent_plan
c1freepro
c2teamteam

Common mistakes

  • Assuming ties are resolved. Two visits with the same timestamp make the first-touch channel arbitrary. Use ARRAY_AGG(channel ORDER BY visited_at, visit_id LIMIT 1)[OFFSET(0)] for a stable answer.
  • Expecting MIN_BY to skip NULL values of x. Only a NULL y is skipped; if the earliest row has a NULL x, the result is NULL.
  • Passing a third argument or adding OVER. BigQuery's MIN_BY takes exactly two arguments (No matching signature otherwise) and is not a window function.
  • Using MIN when you need the other column. MIN(visited_at) gives the first time, not the channel of that visit.

FAQ

How do I get the first value per user in BigQuery?

Use MIN_BY(value, event_timestamp) grouped by user. It returns value from the user's earliest row.

What is the difference between MIN and MIN_BY?

MIN(price) returns the lowest price. MIN_BY(supplier, price) returns the supplier of the row with the lowest price.

Can I use MIN_BY as a window function?

No. To show the first value on every row, use FIRST_VALUE: FIRST_VALUE(channel) OVER (PARTITION BY user_id ORDER BY visited_at).

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.