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.
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
| Argument | Description |
|---|---|
expression | Any 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_expression | Optional filter inside the call (Preview). |
over_clause | Makes 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;| customer_id | first_order | cohort_month |
|---|---|---|
| c1 | 2026-01-30 | 2026-01-01 |
| c2 | 2026-02-02 | 2026-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;| min_city | min_city_lowercase |
|---|---|
| Berlin | amsterdam |
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;| customer_id | order_date | days_since_first_order |
|---|---|---|
| c1 | 2026-01-30 | 0 |
| c1 | 2026-03-14 | 43 |
| c2 | 2026-02-02 | 0 |
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;| ticket_id | least_value | first_contact |
|---|---|---|
| 1 | 2026-11-02 | 2026-11-02 |
| 2 | NULL | 2026-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 overUNNEST([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.
Zurichis smaller thanamsterdam. UseMIN(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.
Related
Errors you may hit
More aggregate functions
All BigQuery aggregate functions · All functions · BigQuery error messages
