Conditional

LEAST

Returns the smallest value in a list.

Syntax

LEAST(<value1>, <value2>, …)

Compares several values in the same row and returns the smallest. Its mirror function is GREATEST, which returns the largest. Don't confuse it with MIN: that one searches across many rows, while LEAST looks at values side by side. Handy for capping a number: “no more than”.

Data for the examples

employees

namedepartmentsalarybonusphone
Emmabackend1200300+44 7700 900123
Liambackend1000NULLNULL
Oliviafrontend1350150+44 7700 900456
Noahfrontend950NULL+44 7700 900789
Sophiadesign1200200NULL

orders

idcreated_atamount
12026-07-03 10:15:00120.50
22026-07-28 18:40:0080.25
32026-08-09 09:05:00199.99
42026-08-21 14:30:0045.00
52026-09-02 11:00:0064.75

These tables already exist in the sandbox — the examples below use them. Want to try this in your own database? Copy the SQL.

The smallest in a list
SELECT LEAST(3, 7, 5) AS smallest;
smallest
3
A coupon covers at most 100
SELECT id, amount,
LEAST(amount, 100) AS covered_by_coupon
FROM orders;
idamountcovered_by_coupon
1120.50100
280.2580.25
3199.99100
445.0045.00
564.7564.75
A bonus of at most 200 — careful with NULL
SELECT name, bonus,
LEAST(bonus, 200) AS capped_bonus
FROM employees;
namebonuscapped_bonus
Emma300200
LiamNULL200
Olivia150150
NoahNULL200
Sophia200200

Liam and Noah had no bonus, and now it's 200: LEAST skips NULL. If a missing bonus should stay missing, check it first: CASE WHEN bonus IS NULL THEN NULL ELSE LEAST(bonus, 200) END.

Limits on both sidesAdvanced
SELECT id, amount,
LEAST(GREATEST(amount, 50), 150) AS clamped
FROM orders;
idamountclamped
1120.50120.50
280.2580.25
3199.99150
445.0050
564.7564.75

First GREATEST raises an amount that's too small to 50, then LEAST lowers one that's too big to 150.

Watch out

PostgreSQL skips NULL: LEAST(5, NULL) gives 5, and only when every value is NULL is the result NULL. In MySQL or Oracle, any NULL would give NULL.

The values must have compatible types: LEAST(5, 'abc') throws invalid input syntax for type integer.

To find the smallest value across table rows, use MIN. LEAST(salary) with a single argument just returns each row's salary.

Related functions