A CASE expression in MySQL returns one value out of several, depending on conditions you list in order. The mysql case when form is CASE WHEN condition THEN result ... ELSE default END, and it works anywhere an expression is allowed: in SELECT, WHERE, ORDER BY, GROUP BY and inside aggregate functions. MySQL checks the WHEN branches from top to bottom and returns the result of the first one that is true.
CASE is the SQL way to put "if / else" logic inside a query. This guide uses two small tables, orders(id, customer_id, total, status, created_at) and customers(id, name, city).
MySQL supports a searched CASE and a simple CASE. The searched form evaluates a full condition in every branch:
The simple form compares one expression against a list of values, which is shorter when you only test equality:
Use the searched form when you need ranges, AND / OR, IS NULL or LIKE. The simple form uses = internally, so it can never match NULL.
Branches are tested in the order you write them, and evaluation stops at the first true branch. That is why the >= 500 test must come before >= 100 in the first example: a total of 700 would otherwise be labelled "medium".
If no branch matches and there is no ELSE, the result is NULL. That is easy to miss, so add an ELSE unless NULL is really what you want.
All branches should return compatible types. Mixing a number and a string makes MySQL convert the result to a common type, which can give surprising output, so keep the types the same or cast explicitly.
The most common use is turning codes or numbers into readable labels, as above. You can also compute a value per row:
Always give the CASE expression an alias with AS; otherwise the column name is the full expression text.
Putting CASE inside an aggregate function lets you count or sum only some rows, and produce several totals in one pass. This is how you build a simple pivot report:
COUNT(CASE WHEN status = 'S' THEN 1 END) also works, because COUNT(expr) skips NULL and a CASE without ELSE returns NULL for non-matching rows.
CASE can define a custom sort order. This puts pending orders first, then shipped, then everything else:
It also works in UPDATE, where it replaces several separate statements with one:
Keep ELSE total so rows that match no branch keep their old value instead of becoming NULL.
MySQL also has the IF(condition, a, b) function and IFNULL(x, fallback). IF() is a MySQL-specific shortcut for a single condition. CASE is standard SQL, supports many branches, and runs on other databases too, so prefer it for anything beyond a two-way choice. Do not confuse the CASE expression with the CASE statement used inside stored programs, which ends with END CASE and does not return a value.
| Need | Use |
|---|---|
| One condition, two outcomes | IF() or CASE |
| Many outcomes | CASE |
Replace NULL with a default | IFNULL() or COALESCE() |
| Portable SQL | CASE |
Yes. If no WHEN matches, the result is NULL. Add ELSE when you need a real default value.
Yes, but it is usually clearer to write the conditions directly with AND / OR. CASE in WHERE can stop MySQL from using an index on the tested column.
The simple form compares with =, and NULL = NULL is not true. Use a searched CASE with WHEN col IS NULL THEN ....
Yes. A branch result can itself be a CASE, but deep nesting is hard to read; consider a lookup table instead.
CASE gives SQL branching logic. Use the searched form for conditions and the simple form for equality, order branches from most to least specific, and add an ELSE. Combine it with SUM or COUNT for conditional aggregation, with ORDER BY for custom sorting, and with UPDATE for multi-rule changes.