LEAST(expr1, expr2, ...) returns the smallest of its arguments and skips nulls, so a row with one missing value still gets an answer. DuckDB's least() skips nulls the same way. Snowflake's and BigQuery's LEAST return null if any argument is null: use LEAST_IGNORE_NULLS in Snowflake and coalesce the arguments in BigQuery. For the null-propagating version in Tableau, see LEASTPROPAGATENULL.
Updated · Checked on the xmetric engine
What is the syntax?
LEAST(expr1, expr2, ...)
expr1, expr2, ...
two or more numbers or numeric expressions.
Returns the smallest of the non-null arguments. Null only if every argument is null.
What does it return, and what is the SQL?
Each example runs on the same 8 sample rows twice: the Tableau formula on the xmetric engine, and the SQL in DuckDB.
Which is smaller, Value or Other?
Tableau
LEAST([Value], [Other])
SQL (DuckDB)
SELECTleast(Value, Other) AS result
FROM input
ORDERBY id
Row 3 has no Other, so it returns the value, 0.125; row 6 has no Value, so it returns Other, 2.
Value
Other
Result
2.5
4
2.5
−2.5
−3
−3
0.13
0.13
−0.4
−1
−1
3.75
8
3.75
2
2
0
0.5
0
16
16
16
✓ The Tableau formula and the SQL return the same 8 rows.
How do I cap values at 1?
Tableau
LEAST([Value], 1)
SQL (DuckDB)
SELECTleast(Value, 1) AS result
FROM input
ORDERBY id
Everything above 1 becomes 1. Row 6 has no value, and the result is 1, the one argument left.
Value
Result
2.5
1
−2.5
−2.5
0.13
0.13
−0.4
−0.4
3.75
1
1
0
0
16
1
✓ The Tableau formula and the SQL return the same 8 rows.
Where does a straight translation go wrong?
Common mistake
Why does a hand-written CASE lose the value when one side is null?
CASE WHEN Value < Other THEN Value ELSE Other END compares with a null on row 3, the test is unknown, and it falls through to Other, which is null. LEAST returns the value that is there, 0.125.
Tableau
LEAST([Value], [Other])
Tempting SQL
SELECTCASEWHEN Value < Other THEN Value ELSE Other ENDAS result
FROM input
ORDERBY id
SQL that matches
SELECTleast(Value, Other) AS result
FROM input
ORDERBY id
Value
Other
Tableau
Tempting SQL
SQL that matches
2.5
4
2.5
2.5
2.5
−2.5
−3
−3
−3
−3
0.13
0.13
null
0.13
−0.4
−1
−1
−1
−1
3.75
8
3.75
3.75
3.75
2
2
2
2
0
0.5
0
0
0
16
16
16
16
16
✓ The SQL that matches returns the Tableau result on every row; the marked cells are where the tempting SQL differs.
How do I write it in Snowflake or BigQuery?
The first example, written for each warehouse from its documentation. These are not run here; DuckDB is.
Snowflake
SELECTLEAST_IGNORE_NULLS(Value, Other) AS result
FROM input
ORDERBY id