Lesson 9 / 37 · Computing values
CASE picks the first branch that is TRUE
CASE is evaluated in SELECT, per row, top to bottom. A NULL amount makes every comparison unknown, so none of the WHEN branches fires and the ELSE wins.
The query
SELECT id, amount,
CASE
WHEN amount >= 100 THEN 'big'
WHEN amount >= 20 THEN 'medium'
ELSE 'small'
END AS size
FROM orders
Try adding WHEN amount IS NULL THEN 'unknown' as the first branch: order 108 gets a label of its own.
Next: Casting and date functions run in SELECT and in WHERE · All lessons