MySQL LEAST() Function
MySQL LEAST() Function: Picking the Smallest Value Across Columns
The LEAST() function compares two or more expressions within a single row and returns the smallest value. While MIN() aggregates down a column across rows, LEAST() compares sideways across arguments.
A classic use case is capping values—for instance, LEAST(discount_pct, 50) ensures a promotional discount never exceeds 50%. It handles integers, decimals, dates, and strings cleanly when data types align.
Syntax
LEAST(value1, value2, ...)
value1, value2, ...: Two or more values to compare.
Practical Example
Suppose demo_products stores standard retail prices alongside seasonal promo prices (promo_price) and partner prices (partner_price). You want to display the lowest eligible price per product while handling missing NULL promo prices.
SELECT
product_id,
product_name,
retail_price,
promo_price,
partner_price,
LEAST(
retail_price,
COALESCE(promo_price, retail_price),
COALESCE(partner_price, retail_price)
) AS lowest_price
FROM demo_products
WHERE product_id IN (21, 22, 23);
| product_id | product_name | retail_price | promo_price | partner_price | lowest_price |
|---|---|---|---|---|---|
| 21 | SSD 1TB | 110.00 | 89.00 | 95.00 | 89.00 |
| 22 | RAM 32GB | 140.00 | NULL | 125.00 | 125.00 |
| 23 | Cooling Pad | 35.00 | NULL | NULL | 35.00 |
Because COALESCE() falls back to retail_price, product 22 still finds 125.00 instead of returning NULL.
Common Mistakes & NULL Behavior
- Strict NULL short-circuiting: In MySQL, if any argument passed to
LEAST()isNULL, the entire function returnsNULL. For example,LEAST(5, 10, NULL)returnsNULL, NOT5. Always sanitize nullable inputs withCOALESCE(). - Passing a single argument:
LEAST(10)triggers a syntax/argument-count error because at least two values are required. - Unexpected string-to-number coercion: Mixing integers and strings like
LEAST(9, '100')triggers numeric comparison, whileLEAST('9', '100')compares alphabetically and returns'100'. Match types explicitly.
Frequently Asked Questions
Why did LEAST(promo_price, retail_price) return NULL for some rows?
Whenever promo_price is NULL, MySQL returns NULL for the whole LEAST() expression. Wrap promo_price with COALESCE(promo_price, retail_price) to skip missing promos.
When should I use LEAST() instead of MIN()?
Use LEAST() to find the smallest among multiple columns in the same row. Use MIN() to find the smallest value across many rows in one column.
Unlock Your
Full Potential.
Sign in to track your learning journey, earn industry-recognized certificates, and join our elite developer community.
Quick Access With
Try it Yourself
Experiment with the code from this lesson in our interactive playground.