Sub topic 9
Simple CASE: Comparing One Value to Many
Assume you know basic SELECT, WHERE, GROUP BY, and aggregate functions such as COUNT and SUM. This chapter focuses on the simple form of CASE, which compares one expression with several possible values.
A simple CASE expression maps stored values to clearer labels. It has this shape:
CASE expression
WHEN value_1 THEN result_1
WHEN value_2 THEN result_2
ELSE default_result
ENDSQL evaluates the expression once, then compares it with each WHEN value using equality. It returns the result from the first matching WHEN branch.
Mapping status codes
Suppose an orders table stores short status codes:
| status | Meaning |
|---|---|
| `P` | Pending |
| `S` | Shipped |
| `D` | Delivered |
You can show readable labels without changing the stored data:
SELECT order_id,
status,
CASE status
WHEN 'P' THEN 'Pending'
WHEN 'S' THEN 'Shipped'
WHEN 'D' THEN 'Delivered'
ELSE 'Unknown'
END AS status_label
FROM orders;For a row where status is 'S', SQL compares 'S' with 'P', then 'S', and returns 'Shipped'. The alias status_label names the computed result.
The ELSE branch handles values that do not match any listed value. This includes unexpected codes such as 'C', as well as values introduced later by an application change.
How matching works
The following diagram shows the evaluation path for one row.
SQL stops at the first match. Therefore, the order of WHEN branches matters when comparisons involve values that can overlap. With a simple CASE, each comparison is equality-based, so this issue is less common than with searched CASE, but branch order still determines which result is selected if equivalent conditions are introduced by a dialect or expression.
Simple CASE versus searched CASE
A simple CASE compares one expression with fixed values:
CASE status
WHEN 'P' THEN 'Pending'
WHEN 'S' THEN 'Shipped'
ELSE 'Other'
ENDA searched CASE tests a separate condition in every WHEN clause:
CASE
WHEN salary >= 100000 THEN 'high'
WHEN salary >= 50000 THEN 'medium'
ELSE 'low'
ENDChoose simple CASE for direct value-to-label mappings. Choose searched CASE for ranges, compound conditions, and tests such as IS NULL.
NULL is not an ordinary value
This simple CASE does not label a missing status:
CASE status
WHEN NULL THEN 'Missing'
ELSE 'Known'
ENDThe comparison acts like status = NULL, and equality with NULL is not true. Use a searched CASE with IS NULL instead:
CASE
WHEN status IS NULL THEN 'Missing'
WHEN status = 'P' THEN 'Pending'
WHEN status = 'S' THEN 'Shipped'
ELSE 'Other'
END AS status_labelAll result branches should produce compatible data types. Returning text from one branch and a numeric value from another may cause an error or an implicit conversion, depending on the database system. Keep the result type consistent.
Exercise
Write a simple CASE expression that maps priority values H, M, and L to High, Medium, and Low, and maps every other value to Unknown.
Show answer
CASE priority WHEN 'H' THEN 'High' WHEN 'M' THEN 'Medium' WHEN 'L' THEN 'Low' ELSE 'Unknown' END AS priority_label