Sub topic 10
How CASE Evaluates: Order, ELSE, and Result Type
Assume you know basic SELECT, WHERE, GROUP BY, and aggregate functions. This chapter focuses on how SQL chooses a CASE result and how that choice affects data types and missing values.
Conditions are checked in order
A searched CASE tests each WHEN condition from top to bottom. It returns the result for the first condition that is true. SQL does not continue to later branches after finding a match.
SELECT employee_id,
salary,
CASE
WHEN salary >= 100000 THEN 'high'
WHEN salary >= 50000 THEN 'medium'
ELSE 'low'
END AS salary_band
FROM employees;An employee with a salary of 125000 matches both salary conditions, but the result is high because the first matching condition wins. Therefore, put more specific or more important conditions before broader conditions.
ELSE and the default NULL
The ELSE clause defines the result when no WHEN condition is true. If you omit ELSE, SQL uses NULL as the default result.
SELECT order_id,
CASE
WHEN status = 'P' THEN 'Pending'
WHEN status = 'S' THEN 'Shipped'
END AS status_label
FROM orders;An order with another status, including a NULL status, receives a NULL label. Add an explicit ELSE when an unmatched row should have a visible or safe value.
SELECT order_id,
CASE
WHEN status = 'P' THEN 'Pending'
WHEN status = 'S' THEN 'Shipped'
ELSE 'Other'
END AS status_label
FROM orders;A NULL condition is not true. For example, status = 'P' is neither true nor false when status is NULL under SQL's three-valued logic. The row therefore continues to the next branch or to ELSE.
>callout note > Use IS NULL and IS NOT NULL when you need to test for missing values. Do not use = NULL.
Result type must be compatible
Every result expression in a CASE should have compatible data types. SQL chooses one result type for the whole expression, using rules that vary by database system.
| Branch results | Typical outcome |
|---|---|
| `'yes'` and `'no'` | Text |
| `1` and `0` | Integer |
| `salary` and `0` | Numeric |
| A date and a text value | Often an error or dialect-specific conversion |
For predictable queries, return the same kind of value from every branch. Do not mix numeric labels and text labels unless your database explicitly supports the conversion you want.
SELECT employee_id,
CASE
WHEN salary >= 100000 THEN 1
WHEN salary IS NULL THEN 0
ELSE 0
END AS qualifies
FROM employees;The expression above returns numeric values for every branch. The ELSE also makes the handling of salaries below the threshold explicit.
Branch evaluation and errors
A CASE returns only one selected result, but you should not use it as the only protection against every possible error. Query optimizers can evaluate some subexpressions earlier than expected, especially constants or aggregate expressions.
Use NULLIF for a division whose denominator may be zero:
SELECT total_revenue / NULLIF(order_count, 0) AS average_order_value
FROM sales_summary;NULLIF(order_count, 0) returns NULL when the count is zero, so the division produces NULL instead of dividing by zero.
Exercise
What result does this expression return when salary is 120000, and why? CASE WHEN salary >= 50000 THEN 'medium' WHEN salary >= 100000 THEN 'high' ELSE 'low' END
Show answer
It returns medium. Both conditions are true, but CASE returns the result from the first true WHEN branch.