Request a demo

BigQuery · Aggregate function

BigQuery MIN

MIN returns the smallest non-NULL value in a group: the earliest date, the lowest price, the alphabetically first string. It works on any orderable type (not ARRAY or STRUCT), and with OVER it puts each group's minimum on every row.

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

MIN compares values with the type's natural order and skips NULLs. Numbers compare numerically, dates and timestamps chronologically, BOOL with FALSE before TRUE, and strings by Unicode code point, so every uppercase ASCII letter sorts before every lowercase one. 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.

MIN aggregates across rows. To compare values within one row, such as the earlier of first_reply and first_call, use LEAST. Unlike MIN, LEAST returns NULL as soon as one argument is NULL.

MIN only returns the value. To get other columns from the row that holds the minimum, such as the channel of a user's first visit, use MIN_BY, ARRAY_AGG(... ORDER BY ... LIMIT 1) or QUALIFY with ROW_NUMBER.

Syntax

MIN(expression [WHERE where_expression]) [OVER over_clause]

Arguments and return value

ArgumentDescription
expressionAny 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_expressionOptional filter inside the call (Preview).
over_clauseMakes MIN a window function. MIN(order_date) OVER (PARTITION BY customer_id) puts each customer's first order date on every order row.

Returns: The type of expression. NULL for an empty group or all-NULL input.

Examples

First order date and cohort month per customer

WITH orders AS (
  SELECT 'c1' AS customer_id, DATE '2026-03-14' AS order_date UNION ALL
  SELECT 'c1', DATE '2026-01-30' UNION ALL
  SELECT 'c2', DATE '2026-02-02' UNION ALL
  SELECT 'c2', DATE '2026-02-20'
)
SELECT
  customer_id,
  MIN(order_date) AS first_order,
  DATE_TRUNC(MIN(order_date), MONTH) AS cohort_month
FROM orders
GROUP BY customer_id
ORDER BY customer_id;
Result
customer_idfirst_ordercohort_month
c12026-01-302026-01-01
c22026-02-022026-02-01

String comparison is case-sensitive

Uppercase letters have lower code points than lowercase ones, so Berlin and Zurich both sort before amsterdam. Compare LOWER(city) for an alphabetical result.

SELECT
  MIN(city) AS min_city,
  MIN(LOWER(city)) AS min_city_lowercase
FROM UNNEST(['amsterdam', 'Berlin', 'Zurich', NULL]) AS city;
Result
min_citymin_city_lowercase
Berlinamsterdam

Days since each customer's first order

WITH orders AS (
  SELECT 'c1' AS customer_id, DATE '2026-01-30' AS order_date UNION ALL
  SELECT 'c1', DATE '2026-03-14' UNION ALL
  SELECT 'c2', DATE '2026-02-02'
)
SELECT
  customer_id,
  order_date,
  DATE_DIFF(order_date, MIN(order_date) OVER (PARTITION BY customer_id), DAY) AS days_since_first_order
FROM orders
ORDER BY customer_id, order_date;
Result
customer_idorder_datedays_since_first_order
c12026-01-300
c12026-03-1443
c22026-02-020

Smallest of several columns, ignoring NULLs

LEAST returns NULL when one input is NULL. Running MIN over an inline array of the columns skips the NULL instead.

WITH tickets AS (
  SELECT 1 AS ticket_id, DATE '2026-11-03' AS first_reply, DATE '2026-11-02' AS first_call UNION ALL
  SELECT 2, DATE '2026-11-05', NULL
)
SELECT
  ticket_id,
  LEAST(first_reply, first_call) AS least_value,
  (SELECT MIN(d) FROM UNNEST([first_reply, first_call]) AS d) AS first_contact
FROM tickets
ORDER BY ticket_id;
Result
ticket_idleast_valuefirst_contact
12026-11-022026-11-02
2NULL2026-11-05

Common mistakes

  • Using MIN to compare columns of one row. MIN(first_reply, first_call) fails with No matching signature for aggregate function MIN; use LEAST, or MIN over UNNEST([a, b]) to skip NULLs.
  • Comparing numbers stored as strings. As strings, '10' is smaller than '9'. Cast to INT64 or NUMERIC before taking the minimum.
  • Expecting alphabetical order for mixed-case strings. Zurich is smaller than amsterdam. Use MIN(LOWER(city)) or a case-insensitive collation.
  • Expecting MIN to return the whole row. It returns one value; use MIN_BY or ROW_NUMBER with QUALIFY for the other columns.
  • Ignoring NaN. One NaN in a FLOAT64 column makes MIN return NaN. Filter with WHERE NOT IS_NAN(x) if NaN can occur.

Cost and performance

  • To find the first or last partition of a large partitioned table, query INFORMATION_SCHEMA.PARTITIONS, which has one row per partition, instead of running MIN or MAX over the partitioning column. The aggregate reads that column across every partition.

FAQ

How do I get the minimum value per group in BigQuery?

SELECT key, MIN(value) FROM t GROUP BY key. To keep every row and add the group minimum next to it, use MIN(value) OVER (PARTITION BY key).

What is the difference between MIN and LEAST?

MIN aggregates one column across rows and skips NULLs. LEAST compares several values within one row and returns NULL if any of them is NULL.

How do I get the row with the minimum value?

Use MIN_BY for one column, ARRAY_AGG(t ORDER BY value LIMIT 1)[OFFSET(0)] for the whole row, or QUALIFY ROW_NUMBER() OVER (PARTITION BY key ORDER BY value) = 1.

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.