BigQuery · Math function
BigQuery LEAST
LEAST(x1, x2, ...) returns the smallest of its arguments within a single row, such as the earliest of several dates or a value capped at a limit. If any argument is NULL, the result is NULL. Use MIN to find the smallest value across rows.
LEAST is the row-wise minimum: it compares values of the same row and returns the smallest, using the same ordering as ORDER BY. Typical uses are capping, as in LEAST(requested_refund, order_total), and picking the earliest of several dates.
Any NULL argument makes LEAST return NULL, unlike PostgreSQL, which skips NULLs. If missing values should be ignored, use (SELECT MIN(v) FROM UNNEST([a, b]) AS v). Arguments must share a supertype: LEAST(3, 2.5) returns the FLOAT64 2.5, and a STRING mixed with a number fails with No matching signature for function LEAST.
Strings compare by code point, so uppercase sorts first: LEAST('apple', 'Banana', 'cherry') returns Banana. With GREATEST, LEAST clamps values into a range.
Syntax
LEAST(X1, ..., XN)Arguments and return value
| Argument | Description |
|---|---|
X1, ..., XN | One 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 smallest value, NULL if any argument is NULL, or NaN if any FLOAT64 argument is NaN.
Examples
Cap a refund at the order total
WITH refunds AS (
SELECT 'R-1' AS refund_id, 80 AS requested, 100 AS order_total UNION ALL
SELECT 'R-2', 150, 120
)
SELECT
refund_id,
LEAST(requested, order_total) AS approved_amount
FROM refunds
ORDER BY refund_id;| refund_id | approved_amount |
|---|---|
| R-1 | 80 |
| R-2 | 120 |
Earliest signup across channels, with NULLs
u2 never signed up in the app, so LEAST returns NULL. MIN over the array ignores the missing value.
WITH users AS (
SELECT 'u1' AS user_id, DATE '2026-03-04' AS web_signup, DATE '2026-02-11' AS app_signup UNION ALL
SELECT 'u2', DATE '2026-05-20', NULL
)
SELECT
user_id,
LEAST(web_signup, app_signup) AS least_result,
(SELECT MIN(d) FROM UNNEST([web_signup, app_signup]) AS d) AS first_signup
FROM users
ORDER BY user_id;| user_id | least_result | first_signup |
|---|---|---|
| u1 | 2026-02-11 | 2026-02-11 |
| u2 | NULL | 2026-05-20 |
Code-point order and mixed numbers
SELECT
LEAST('apple', 'Banana', 'cherry') AS by_code_point,
LEAST(3, 2.5) AS mixed_numeric;| by_code_point | mixed_numeric |
|---|---|
| Banana | 2.5 |
Common mistakes
- Expecting NULL to be ignored. One NULL argument returns NULL; wrap columns in IFNULL or use MIN over UNNEST.
- Writing
MIN(a, b). MIN is an aggregate with one argument; to compare columns within a row, use LEAST. - Capping only one side.
LEAST(discount, 0.5)still allows negative discounts; clamp both ends withGREATEST(LEAST(discount, 0.5), 0). - Mixing DATE and TIMESTAMP arguments. They have no common supertype; convert one side explicitly.
FAQ
What is the difference between LEAST and MIN in BigQuery?
LEAST returns the smallest of several values in one row. MIN is an aggregate that returns the smallest value of one column across a group of rows.
How do I get the earliest of several date columns?
Use LEAST(date_a, date_b, date_c) if none can be NULL; otherwise use (SELECT MIN(d) FROM UNNEST([date_a, date_b, date_c]) AS d).
Why does LEAST return NULL?
At least one argument is NULL. In BigQuery, LEAST and GREATEST return NULL as soon as any input is NULL.
Related
Errors you may hit
More mathematical functions
All BigQuery mathematical functions · All functions · BigQuery error messages
