Sub topic 15
Common Pitfalls and Best Practices
Assume you know basic SELECT, WHERE, ORDER BY, GROUP BY, aggregate functions, and Oracle types such as NUMBER, VARCHAR2, and DATE. This chapter focuses on mistakes that produce wrong results or make SQL harder to maintain.
Make branch order explicit
Oracle evaluates CASE branches from top to bottom and returns the result from the first true branch. Put more specific conditions before broader conditions.
SELECT employee_id,
salary,
CASE
WHEN salary < 10000 THEN 'Below 10000'
WHEN salary < 20000 THEN 'Below 20000'
ELSE '20000 or more'
END AS salary_group
FROM employees;The second condition includes salaries below 10,000, but those rows already match the first condition. The query is valid, yet its intent is unclear. Use non-overlapping ranges when possible.
A missing ELSE returns NULL when no branch matches. That may be correct, but it can cause missing labels or null totals later.
Handle NULL explicitly
NULL means an unknown or absent value. It does not equal another value, including another NULL. Therefore, this simple expression never matches a null department:
CASE department_id
WHEN NULL THEN 'Unknown'
ELSE 'Known'
ENDUse a searched CASE with IS NULL instead:
SELECT employee_id,
CASE
WHEN commission_pct IS NULL THEN 'No commission'
ELSE 'Has commission'
END AS commission_status
FROM employees;DECODE is different. Oracle treats two nulls as equal inside DECODE. This behavior can make a conversion from DECODE to CASE subtly wrong unless you add an explicit IS NULL condition.
Keep result data types consistent
All result expressions in a CASE should represent the same logical type. Mixing numbers and text can cause implicit conversion, an ORA-00932 inconsistent datatypes error, or a conversion error at runtime.
Prefer explicit conversion when different source types are involved:
SELECT employee_id,
CASE
WHEN salary IS NULL THEN 'Not supplied'
ELSE TO_CHAR(salary, 'FM9999990.00')
END AS salary_text
FROM employees;Do not use a formatted text result for later arithmetic. Keep values numeric until presentation.
Choose the right construct
| Need | Preferred construct |
|---|---|
| Test arbitrary conditions or ranges | Searched `CASE` |
| Match one expression to known values | Simple `CASE` |
| Preserve legacy Oracle code | `DECODE`, with care |
| Replace one null value | `NVL` or `COALESCE` |
| Test whether a value is null | `NVL2` or searched `CASE` |
DECODE only performs equality matching. For ranges, use CASE directly instead of encoding comparisons with SIGN, unless you are maintaining existing code.
Watch expressions in predicates and aggregates
A CASE around an indexed column in WHERE can make index use harder. Compare the column directly where possible, and inspect the execution plan for important queries.
For conditional aggregation, include ELSE 0 when you need a numeric zero instead of null:
SELECT department_id,
SUM(CASE WHEN salary >= 10000 THEN 1 ELSE 0 END) AS high_earners
FROM employees
GROUP BY department_id;Exercise
What is wrong with CASE commission_pct WHEN NULL THEN 'None' ELSE 'Some' END, and how should it be written?
Show answer
WHEN NULL never matches in a simple CASE. Use CASE WHEN commission_pct IS NULL THEN 'None' ELSE 'Some' END.