MySQL CASE
CASE is SQL's if-then-else. It evaluates conditions in order and returns the value of the first one that is true, or the ELSE value if none is. Because it is an expression, it can appear anywhere a value can: in the select list, WHERE, ORDER BY, GROUP BY and inside aggregates.
CASE
WHEN condition1 THEN result1
WHEN condition2 THEN result2
ELSE default_result
ENDLabelling values
SELECT name, unit_price,
CASE
WHEN unit_price < 2 THEN 'budget'
WHEN unit_price < 6 THEN 'standard'
ELSE 'premium'
END AS price_band
FROM products
WHERE category_id IN (1, 3)
ORDER BY unit_price;+----------------------+------------+------------+
| name | unit_price | price_band |
+----------------------+------------+------------+
| Whole Milk 1L | 1.45 | budget |
| Salted Butter 250g | 2.85 | standard |
| Orange Juice 1L | 3.20 | standard |
| Greek Yogurt 500g | 3.95 | standard |
| Sparkling Water 6x1L | 4.99 | standard |
| Earl Grey Tea | 6.75 | premium |
| Aged Gruyere 250g | 7.95 | premium |
| Espresso Beans 1kg | 24.50 | premium |
+----------------------+------------+------------+
The order of the WHENs matters: 1.45 is also below 6, but the first true condition wins. Without ELSE, rows that match nothing get NULL.
The simple form
When every condition compares the same expression with a value, the short simple CASE reads better:
SELECT order_id,
CASE status
WHEN 'delivered' THEN 'Delivered'
WHEN 'shipped' THEN 'On its way'
WHEN 'processing' THEN 'Being packed'
ELSE 'Cancelled'
END AS customer_message
FROM orders
WHERE order_id IN (8, 17, 18, 21);+----------+------------------+
| order_id | customer_message |
+----------+------------------+
| 8 | Cancelled |
| 17 | Delivered |
| 18 | On its way |
| 21 | Being packed |
+----------+------------------+
The simple form compares with =, so CASE x WHEN NULL THEN ... never matches: use the searched form WHEN x IS NULL for that.
Conditional counting
CASE inside an aggregate counts or sums only some rows. This "pivot" turns statuses into columns, one row per employee:
SELECT e.first_name AS employee,
SUM(CASE WHEN o.status = 'delivered' THEN 1 ELSE 0 END) AS delivered,
SUM(CASE WHEN o.status IN ('shipped', 'processing') THEN 1 ELSE 0 END) AS open,
SUM(CASE WHEN o.status = 'cancelled' THEN 1 ELSE 0 END) AS cancelled
FROM orders AS o
JOIN employees AS e ON e.employee_id = o.employee_id
GROUP BY e.employee_id, e.first_name
ORDER BY e.employee_id;+----------+-----------+------+-----------+
| employee | delivered | open | cancelled |
+----------+-----------+------+-----------+
| Linus | 5 | 2 | 1 |
| Maya | 6 | 2 | 0 |
| Omar | 4 | 1 | 1 |
+----------+-----------+------+-----------+
In MySQL a condition already is 0 or 1, so SUM(o.status = 'delivered') gives the same result. The CASE form is portable to every database.
Custom sort order
CASE in ORDER BY sorts by any rule you like, here open orders first, then the rest by date:
SELECT order_id, status, order_date
FROM orders
WHERE order_date >= '2025-04-20'
ORDER BY CASE status WHEN 'processing' THEN 1 WHEN 'shipped' THEN 2 ELSE 3 END,
order_date;+----------+------------+------------+
| order_id | status | order_date |
+----------+------------+------------+
| 20 | processing | 2025-05-28 |
| 21 | processing | 2025-06-02 |
| 22 | processing | 2025-06-03 |
| 18 | shipped | 2025-05-12 |
| 19 | shipped | 2025-05-20 |
| 16 | cancelled | 2025-04-25 |
| 17 | delivered | 2025-05-03 |
+----------+------------+------------+
IF(): the two-way shortcut
MySQL's IF(condition, then, else) function is a compact CASE with one WHEN:
SELECT name, units_in_stock,
IF(units_in_stock = 0, 'reorder!', 'ok') AS stock_check
FROM products
WHERE category_id = 2;+------------------+----------------+-------------+
| name | units_in_stock | stock_check |
+------------------+----------------+-------------+
| Sourdough Loaf | 25 | ok |
| Butter Croissant | 120 | ok |
| Chocolate Cake | 8 | ok |
| Rye Crackers | 0 | reorder! |
+------------------+----------------+-------------+
IF() is MySQL-specific; CASE works everywhere.
Test yourself
Which value does CASE return when several WHEN conditions are true?
- The last one
- The first one
- All of them
- NULL
What does CASE return when no WHEN matches and there is no ELSE?
- 0
- An empty string
- NULL
- An error
What is the MySQL function IF(a > b, 'x', 'y') equivalent to?
- CASE WHEN a > b THEN 'x' ELSE 'y' END
- COALESCE(a, b)
- NULLIF(a, b)
- a > b AND 'x' OR 'y'