LEAST¶
Returns the least (minimum) value from a list of expressions.
Syntax¶
Parameters¶
LEAST(expression1, expression2, ...)Returns the smallest value among all provided expressions. Requires at least two expressions. NULL values are considered smaller than any non-NULL value.
Returns¶
Returns the least value with the same type as the input expressions. If any value is NULL, returns NULL. All expressions must be of compatible types.
Supported Types¶
LEAST supports the following types:
STRING- Lexicographic comparisonBOOLEAN- FALSE < TRUEDOUBLE- Numeric comparisonFLOAT- Numeric comparisonINTEGER- Numeric comparisonBIGINT- Numeric comparison
Not Supported: ARRAY, STRUCT, BYTES
Examples¶
Setup¶
For these examples, assume we have a products table:
CREATE TABLE products(
id BIGINT,
name STRING,
price_usd BIGINT,
price_eur BIGINT,
price_gbp BIGINT,
PRIMARY KEY(id))
INSERT INTO products VALUES
(1, 'Widget', 100, 90, 80),
(2, 'Gadget', 150, 140, 120),
(3, 'Doohickey', 200, 180, 160)
LEAST - Find Minimum Price¶
Find the lowest price across all currencies for each product:
SELECT name, LEAST(price_usd, price_eur, price_gbp) AS min_price
FROM products
|
|
|---|---|
|
|
|
|
|
|
LEAST with Constants¶
Compare values with constants:
SELECT name, LEAST(price_usd, 175) AS capped_price
FROM products
|
|
|---|---|
|
|
|
|
|
|
This ensures a maximum price cap of 175 for all products.
Important Notes¶
LEASTreturns the smallest value among all provided expressionsIf any value is NULL,
LEASTreturns NULLAll expressions must be of compatible types
The function requires at least two arguments
For string comparisons, lexicographic ordering is used