Sub topic 3
Using CASE in SELECT, WHERE, ORDER BY, GROUP BY and UPDATE
This chapter assumes you can write SELECT with WHERE and ORDER BY, know the Oracle types NUMBER, VARCHAR2 and DATE, understand that NULL means "unknown", and can use GROUP BY with SUM and COUNT. The earlier chapters covered the syntax of simple and searched CASE. This chapter applies CASE across the clauses of a statement.
Using CASE in SELECT, WHERE, ORDER BY, GROUP BY and UPDATE
A CASE expression returns a value, so it can appear anywhere a value is expected. The diagram shows how Oracle walks the branches.
The same expression can sort rows, group rows, or filter rows:
SELECT employee_id, job_id
FROM employees
ORDER BY CASE job_id
WHEN 'AD_PRES' THEN 1
WHEN 'AD_VP' THEN 2
ELSE 3
END,
employee_id;SELECT CASE WHEN salary >= 10000 THEN 'High' ELSE 'Standard' END AS band,
COUNT(*) AS headcount
FROM employees
GROUP BY CASE WHEN salary >= 10000 THEN 'High' ELSE 'Standard' END;In an UPDATE, each row gets the branch that matches it:
UPDATE employees
SET salary = CASE
WHEN salary < 5000 THEN salary * 1.10
WHEN salary < 10000 THEN salary * 1.05
ELSE salary
END
WHERE department_id = 50;Conditional aggregation: SUM(CASE ...) and pivoting rows to columns
Placing CASE inside an aggregate counts or sums only the rows that match a condition. Several categories then become separate columns in one pass.
SELECT department_id,
COUNT(*) AS headcount,
SUM(CASE WHEN salary >= 10000 THEN 1 ELSE 0 END) AS high_earners,
SUM(CASE WHEN salary < 10000 THEN 1 ELSE 0 END) AS other_earners
FROM employees
GROUP BY department_id
ORDER BY department_id;Result data types and ORA-00932 errors
All THEN and ELSE results must have a compatible data type. Mixing a text literal with a number, such as THEN 'None' ELSE salary, can raise ORA-00932 (inconsistent datatypes). Convert the number explicitly with TO_CHAR(salary) so every branch returns text.
Range checks in DECODE with SIGN
DECODE tests only equality. To test a range, wrap the difference in SIGN, which returns -1, 0 or 1:
SELECT employee_id,
salary,
DECODE(SIGN(salary - 10000), 1, 'Above 10000', 'At or below 10000') AS salary_check
FROM employees;NVL, NVL2, COALESCE and NULLIF as shorter alternatives
| Function | Returns |
|---|---|
| NVL(a, b) | a if it is not NULL, otherwise b |
| NVL2(a, b, c) | b if a is not NULL, otherwise c |
| COALESCE(a, b, ...) | the first argument that is not NULL |
| NULLIF(a, b) | NULL if a equals b, otherwise a |
CASE in PL/SQL: statement versus expression
A CASE expression returns a value inside SQL. A CASE statement in PL/SQL runs statements instead and ends with END CASE. If no WHEN matches and there is no ELSE, the statement raises CASENOTFOUND.
Common pitfalls and best practices
WHEN NULLnever matches. UseWHEN x IS NULLin a searched CASE.- Order the WHEN branches from most specific to least specific, because the first true branch wins.
- Add an explicit ELSE so unexpected values do not silently return NULL.
- Keep result types consistent across branches.
Exercise
Exercise
Given the row salary = 12000, what does this expression return? CASE WHEN salary >= 10000 THEN 'High' WHEN salary >= 5000 THEN 'Medium' ELSE 'Low' END
Show answer
'High'. The first WHEN condition is true, so its result is returned and the later branches are not checked.