Example 1
Find the effective (lower) price and the higher reference price for each product
LEAST(list_price, sale_price) picks whichever column holds the smaller number per row. For Doohickey, the sale price (90) is actually higher than the list price (75), so the list price becomes the effective price. Gadget's two prices are equal so both functions return 50. Widget has a sale_price of 80, which is lower than the list_price of 100.
CREATE TABLE products (name VARCHAR(50), list_price INT, sale_price INT);
INSERT INTO
products
VALUES
('Widget', 100, 80),
('Gadget', 50, 50),
('Doohickey', 75, 90);SELECT
name,
LEAST (list_price, sale_price) AS effective_price,
GREATEST (list_price, sale_price) AS higher_price
FROM
products
ORDER BY
name;| name | effective_price | higher_price |
|---|---|---|
| Doohickey | 75 | 90 |
| Gadget | 50 | 50 |
| Widget | 80 | 100 |
The GREATEST and LEAST syntax is identical across all supported engines.