Sub topic 8
Searched CASE: WHEN condition THEN result
This chapter assumes that you can write basic SELECT ... FROM ... WHERE queries and understand common data types. You should also know the purpose of GROUP BY and aggregate functions such as COUNT and SUM.
A searched CASE expression chooses a result by testing conditions in order. Each condition follows WHEN, and its result follows THEN:
CASE
WHEN condition_1 THEN result_1
WHEN condition_2 THEN result_2
ELSE default_result
ENDSQL returns the result for the first condition that is true. If no condition is true, SQL returns the ELSE result. If you omit ELSE, the default result is NULL.
Classifying rows with conditions
Suppose an employees table has a numeric salary column. You can create a salary band without changing the stored data:
SELECT first_name,
salary,
CASE
WHEN salary >= 100000 THEN 'high'
WHEN salary >= 50000 THEN 'medium'
ELSE 'low'
END AS salary_band
FROM employees;The order matters. A salary of 120000 satisfies both conditions, but SQL returns high because that condition appears first. SQL stops checking after it finds a true condition.
Conditions can use comparison operators, logical operators, and other expressions:
SELECT order_id,
total_amount,
CASE
WHEN status = 'cancelled' THEN 'not counted'
WHEN total_amount >= 1000 THEN 'large'
WHEN total_amount >= 100 THEN 'regular'
ELSE 'small'
END AS order_size
FROM orders;The result expressions should be compatible types. In this example, every branch returns text. Mixing numbers and text can cause an error or an implicit conversion, depending on the database system.
Evaluation order and NULL
A searched CASE tests conditions from top to bottom. It does not treat NULL as an ordinary value. A comparison such as commission = 0 is not true when commission is NULL; it evaluates to an unknown result.
Use IS NULL or IS NOT NULL when you need to test for missing data:
SELECT first_name,
CASE
WHEN commission IS NULL THEN 'no commission'
WHEN commission = 0 THEN 'zero commission'
ELSE 'has commission'
END AS commission_status
FROM employees;Place more specific conditions before broader conditions. Otherwise, a broad condition can prevent later branches from being reached.
| Condition order | Salary | Returned result |
|---|---|---|
| `salary >= 50000` before `salary >= 100000` | 120000 | The first branch |
| `salary >= 100000` before `salary >= 50000` | 120000 | The high-salary branch |
CASE in other query clauses
A CASE expression can appear anywhere an expression is allowed. For example, it can create a custom sort order:
SELECT order_id,
status
FROM orders
ORDER BY CASE
WHEN status = 'urgent' THEN 1
WHEN status = 'normal' THEN 2
WHEN status = 'low' THEN 3
ELSE 4
END,
order_id;This sorts by business priority instead of alphabetically. You can also use CASE in GROUP BY, but repeat the expression or place it in a subquery when your database does not allow a select-list alias there.
Exercise
What does this expression return for a row where salary is 75000? CASE WHEN salary >= 100000 THEN 'high' WHEN salary >= 50000 THEN 'medium' ELSE 'low' END
Show answer
It returns 'medium'. The first condition is false, and the second condition is true.