15. CASE: a value chosen by conditions
If it's like this, give this; if it's like that, give that. In SELECT, in ORDER BY and inside aggregates.
CASE WHEN: if... then...
Often we want a value that depends on a condition: products put into price bands, cheap, medium and expensive, say. CASE does it in one statement:
SELECT name, price,
CASE
WHEN price < 30 THEN 'cheap'
WHEN price < 100 THEN 'medium'
ELSE 'expensive'
END AS price_band
FROM product
ORDER BY price, id;| name | price | price_band |
|---|---|---|
| Pen | 15.0 | cheap |
| Potato Chips | 25.0 | cheap |
| Highlighter | 25.0 | cheap |
| Notebook | 30.0 | medium |
| Croissant | 35.0 | medium |
| Orange Juice | 40.0 | medium |
| Green Tea | 45.0 | medium |
| Coffee | 55.0 | medium |
| Banana Bread | 60.0 | medium |
| Dried Mango | 90.0 | medium |
| Cheesecake | 120.0 | expensive |
| Cashew Nuts | 150.0 | expensive |
The database reads the WHENs one at a time, top to bottom. The first one that is true gives the value after its THEN, and it stops there. When none is true, you get the value after ELSE. It always ends with END.
So the order of the WHENs matters. Pen, at 15 baht, fits both price < 30 and price < 100, but gets cheap, because the first one is true first.
Without ELSE, a row that fits no WHEN gets NULL:
SELECT name, stock,
CASE
WHEN stock = 0 THEN 'out of stock'
WHEN stock < 20 THEN 'low'
END AS warning
FROM product
ORDER BY stock, id;| name | stock | warning |
|---|---|---|
| Orange Juice | 0 | out of stock |
| Highlighter | 0 | out of stock |
| Cheesecake | 8 | low |
| Banana Bread | 12 | low |
| Cashew Nuts | 20 | NULL |
| Croissant | 30 | NULL |
| Dried Mango | 45 | NULL |
| Notebook | 75 | NULL |
| Green Tea | 80 | NULL |
| Coffee | 120 | NULL |
| Potato Chips | 200 | NULL |
| Pen | 300 | NULL |
2 products are out of stock and 2 are low; the rest get NULL: nothing to warn about.
CASE x WHEN: comparing one value
When every condition compares the same column with different values, there is a short form: the column goes after CASE, and each WHEN takes just a value:
SELECT id, status,
CASE status
WHEN 'paid' THEN 'pack it'
WHEN 'shipped' THEN 'done'
WHEN 'cancelled' THEN 'refund?'
END AS todo
FROM orders
WHERE id <= 6;| id | status | todo |
|---|---|---|
| 1 | paid | pack it |
| 2 | shipped | done |
| 3 | shipped | done |
| 4 | paid | pack it |
| 5 | cancelled | refund? |
| 6 | shipped | done |
CASE status WHEN 'paid' is CASE WHEN status = 'paid'. The short form only compares with "equals"; for <, LIKE, IS NULL or several columns, use the full form.
CASE phone WHEN NULL THEN ... is never true: the short form compares with =, and NULL = NULL is NULL, not true (chapter 5). To test for NULL, use the full form: CASE WHEN phone IS NULL THEN ....
CASE in ORDER BY: an order of your own
Statuses in alphabetical order come out cancelled, paid, shipped, which isn't the order of the work. The shop wants the orders to pack (paid) first, then the shipped ones, and the cancelled ones last. Turn each status into a number with CASE, and sort by that number:
SELECT id, ordered, status
FROM orders
ORDER BY CASE status
WHEN 'paid' THEN 1
WHEN 'shipped' THEN 2
ELSE 3
END,
ordered;| id | ordered | status |
|---|---|---|
| 1 | 2026-09-02 | paid |
| 4 | 2026-09-20 | paid |
| 7 | 2026-10-10 | paid |
| 9 | 2026-10-22 | paid |
| 10 | 2026-10-28 | paid |
| 12 | 2026-11-05 | paid |
| 13 | 2026-11-11 | paid |
| 15 | 2026-11-25 | paid |
| 2 | 2026-09-05 | shipped |
| 3 | 2026-09-12 | shipped |
| 6 | 2026-10-03 | shipped |
| 8 | 2026-10-15 | shipped |
| 11 | 2026-11-02 | shipped |
| 5 | 2026-10-01 | cancelled |
| 14 | 2026-11-18 | cancelled |
The 8 paid orders come first (by date within the group), then the 5 shipped, and the 2 cancelled last. The number itself needn't be in the result.
CASE inside aggregates: counting by condition in one row
Chapter 10 counted the orders of each status with GROUP BY status, one row per status. To see every status as a column in one row, put CASE inside an aggregate:
SELECT count(*) AS all_orders,
sum(CASE WHEN status = 'paid' THEN 1 ELSE 0 END) AS paid,
sum(CASE WHEN status = 'shipped' THEN 1 ELSE 0 END) AS shipped,
sum(CASE WHEN status = 'cancelled' THEN 1 ELSE 0 END) AS cancelled
FROM orders;| all_orders | paid | shipped | cancelled |
|---|---|---|---|
| 15 | 8 | 5 | 2 |
Each row gives 1 when its status matches and 0 when it doesn't, so the sum is the number of matching rows: 15 orders, split into 8, 5 and 2.
With GROUP BY it makes a cross table. For each month, the number of orders by status, and the sales of orders not cancelled:
SELECT strftime('%Y-%m', o.ordered) AS month,
count(DISTINCT CASE WHEN o.status = 'paid' THEN o.id END) AS paid,
count(DISTINCT CASE WHEN o.status = 'shipped' THEN o.id END) AS shipped,
sum(CASE WHEN o.status <> 'cancelled' THEN i.qty * i.price ELSE 0 END) AS sales
FROM orders o
JOIN order_item i ON i.order_id = o.id
GROUP BY month;| month | paid | shipped | sales |
|---|---|---|---|
| 2026-09 | 2 | 2 | 820.0 |
| 2026-10 | 3 | 2 | 1450.0 |
| 2026-11 | 3 | 1 | 940.0 |
A CASE without ELSE gives NULL for rows that don't match, and count skips NULL, so it counts only the orders of that status (DISTINCT because one order has several lines). The sales match chapter 10's: 820, 1,450 and 940 baht.
In SQLite and Phai SQL a condition is worth 1 (true) or 0 (false), so sum(status = 'paid') gives the same as sum(CASE WHEN status = 'paid' THEN 1 ELSE 0 END). The CASE form works in every database, though.
coalesce, nullif and iif
These three are short CASEs used so often that they have names of their own.
coalesce(a, b, ...) gives the first value that isn't NULL. Use it to put something readable where there is nothing:
SELECT name, coalesce(phone, '(no phone)') AS phone
FROM customer
WHERE city IN ('Bangkok', 'Phuket');| name | phone |
|---|---|
| Somchai Jaidee | 081-234-5678 |
| Anan Wongsa | (no phone) |
| Pim Chaiya | 086-555-0101 |
| Wichai Saelim | 083-999-0000 |
| Siriporn Kaewdee | (no phone) |
| Lalita Meesuk | 087-654-3210 |
Anan Wongsa and Siriporn Kaewdee have no number, so they get (no phone) instead of NULL. It is the same as CASE WHEN phone IS NOT NULL THEN phone ELSE '(no phone)' END.
nullif(a, b) is the other way round: NULL when a equals b, otherwise a. Its most common use is avoiding division by zero. How many times its stock left has each product sold? The products out of stock have a stock of 0:
SELECT p.name, p.stock,
coalesce(sum(i.qty), 0) AS sold,
round(1.0 * coalesce(sum(i.qty), 0) / nullif(p.stock, 0), 2) AS sold_per_stock
FROM product p
LEFT JOIN order_item i ON i.product_id = p.id
GROUP BY p.id, p.name, p.stock
ORDER BY p.id;| name | stock | sold | sold_per_stock |
|---|---|---|---|
| Coffee | 120 | 7 | 0.06 |
| Green Tea | 80 | 5 | 0.06 |
| Orange Juice | 0 | 2 | NULL |
| Croissant | 30 | 6 | 0.2 |
| Banana Bread | 12 | 3 | 0.25 |
| Cheesecake | 8 | 3 | 0.38 |
| Potato Chips | 200 | 11 | 0.06 |
| Dried Mango | 45 | 4 | 0.09 |
| Cashew Nuts | 20 | 2 | 0.1 |
| Notebook | 75 | 18 | 0.24 |
| Pen | 300 | 35 | 0.12 |
| Highlighter | 0 | 0 | NULL |
nullif(p.stock, 0) turns 0 into NULL, and dividing by NULL gives NULL instead of breaking: Orange Juice and Highlighter get NULL ("can't be worked out"). coalesce(sum(i.qty), 0) gives Highlighter, never sold, a 0 instead of NULL (as chapter 9 said), and 1.0 * makes the division give decimals.
SQLite and Phai SQL already give NULL for a division by zero, with no error, but many other databases stop with a "division by zero" error. Make nullif a habit: the statement works everywhere, and the reader sees that zero was thought of.
iif(condition, value if true, value if not) is a CASE WHEN ... THEN ... ELSE ... END with one condition, written shorter:
SELECT name, stock, iif(stock > 0, 'in stock', 'sold out') AS shelf
FROM product
WHERE category_id = 1;| name | stock | shelf |
|---|---|---|
| Coffee | 120 | in stock |
| Green Tea | 80 | in stock |
| Orange Juice | 0 | sold out |
iif is in SQLite (since 3.32) and Phai SQL, and SQL Server has one of the same name; some other databases haven't. For SQL that has to move, use CASE.
- Show every customer's name with
'has phone'or'no phone'(withCASEoriif). - For each customer (
customer_id), how many orders are paid and how many cancelled, in one row. - Show every product, the ones out of stock (
stock = 0) first, the rest by name.
Show the answer
SELECT name, iif(phone IS NULL, 'no phone', 'has phone') AS phone FROM customer;
SELECT customer_id, sum(CASE WHEN status = 'paid' THEN 1 ELSE 0 END) AS paid, sum(CASE WHEN status = 'cancelled' THEN 1 ELSE 0 END) AS cancelled FROM orders GROUP BY customer_id;
SELECT name, stock FROM product ORDER BY CASE WHEN stock = 0 THEN 0 ELSE 1 END, name;| name | phone |
|---|---|
| Somchai Jaidee | has phone |
| Malee Srisuk | has phone |
| Anan Wongsa | no phone |
| Pim Chaiya | has phone |
| Narin Thongdee | has phone |
| Kanya Boonmee | has phone |
| Wichai Saelim | has phone |
| Siriporn Kaewdee | no phone |
| Tawan Rungruang | has phone |
| Lalita Meesuk | has phone |
| customer_id | paid | cancelled |
|---|---|---|
| 1 | 2 | 0 |
| 2 | 1 | 0 |
| 3 | 1 | 1 |
| 4 | 1 | 1 |
| 5 | 0 | 0 |
| 6 | 1 | 0 |
| 7 | 2 | 0 |
| 8 | 0 | 0 |
| name | stock |
|---|---|
| Highlighter | 0 |
| Orange Juice | 0 |
| Banana Bread | 12 |
| Cashew Nuts | 20 |
| Cheesecake | 8 |
| Coffee | 120 |
| Croissant | 30 |
| Dried Mango | 45 |
| Green Tea | 80 |
| Notebook | 75 |
| Pen | 300 |
| Potato Chips | 200 |
Phai (bamboo), given as a gift of knowledge: free to use, learn from and pass on · every example on this page was run on Phai SQL and checked against SQLite when the page was made