CASE example
SELECT name,
stock,
CASE
WHEN stock = 0 THEN 'out_of_stock'
WHEN stock < 10 THEN 'few units'
ELSE 'available'
END AS availability
FROM products
ORDER BY stock, name
LIMIT 6;The condition stock = 0 must appear before stock < 10. Otherwise, out-of-stock products would fall into the broader category.
What to look for: With stock = 0 you will see «out_of_stock»; with stock = 5, «few units»; and with stock = 20, «available». CASE selects a calculated label without changing the stock.
Check the result with small data
Some items are out of stock, some have low stock and some have enough.
WITH products(name, stock) AS (
VALUES ('Mouse',0),('Keyboard',4),('Monitor',20)
)
SELECT name, CASE
WHEN stock = 0 THEN 'out_of_stock'
WHEN stock < 10 THEN 'few units'
ELSE 'available' END AS status
FROM products ORDER BY name;| name | status |
|---|---|
| Keyboard | few units |
| Monitor | available |
| Mouse | out_of_stock |
CASE tests each condition in order and uses the first result that applies. An item never gets two labels, even if it meets more than one condition.
Check your understanding: How would an item with exactly 10 units be classified?
Show answer
As "available". stock = 0 is false and so is stock < 10, because 10 is not less than 10; that is why ELSE applies.
What changes from case to case
CASE
WHEN condition_1 THEN result_1
WHEN condition_2 THEN result_2
ELSE alternative_result
ENDThe searched form lets you write different conditions. The simple form compares one expression with several values: CASE status WHEN 'paid' THEN ....
Common mix-ups with CASE
Ordering the conditions from general to specific
CASE
WHEN stock < 10 THEN 'few units'
WHEN stock = 0 THEN 'out_of_stock'
ENDThe second branch never classifies a zero, because the first already accepts it. Also avoid mixing incompatible results without checking how the engine decides the expression's final type.
Turning conditions into categories without leaving gaps
The first WHEN that is true wins. To classify amounts as high, medium and low, write the limits in an order that does not hide a later category. If you test «amount greater than 10» first, a row of 100 goes there and never reaches a later «amount greater than 80» branch. The order of the rules is part of what the expression means.
Include cases at each boundary: a value right at the threshold, just below, just above, and null. Without ELSE, a row with no true condition returns null; that may be what you expect, or a category you forgot. A null does not match a WHEN NULL branch in the simple form either: to detect a missing value, use an explicit condition IS NULL.
Keep the branches within a compatible domain: mixing quantities and text can cause different conversions or errors depending on the engine. A calculated category does not change the original column. If you later use it to group or filter, keep a single definition in a suitable query layer, to avoid diverging versions of the same rule.
Technical questions: PostgreSQL: conditional expressions (opens in a new tab).