Sub topic 9
How the arguments are evaluated
Assume you know basic SELECT statements, Oracle data types, DUAL, and the main behavior of NVL, NVL2, and COALESCE. This chapter focuses on when Oracle evaluates each argument, because an unused argument can still cause an error or perform expensive work.
Short-circuit evaluation with COALESCE
COALESCE checks its arguments from left to right. It returns the first non-NULL value and does not evaluate later arguments once it has found one.
The following function raises an error whenever Oracle calls it:
CREATE OR REPLACE FUNCTION fail_if_called
RETURN NUMBER
IS
BEGIN
RAISE_APPLICATION_ERROR(-20001, 'fail_if_called was evaluated');
END;
/The first query returns 1. Because the first argument is already non-NULL, Oracle does not call the function.
SELECT COALESCE(1, fail_if_called()) AS result
FROM dual;The next query raises the application error because NVL does not provide the same short-circuit guarantee:
SELECT NVL(1, fail_if_called()) AS result
FROM dual;This difference matters when a fallback expression contains a function, a calculation that can fail, or an expensive query.
NVL and NVL2 evaluation
NVL evaluates the value expression and the replacement expression as part of processing the call. Do not use it to protect an unsafe expression.
For example, this is unsafe as a general pattern:
SELECT NVL(commission_pct, TO_NUMBER('not a number')) AS commission
FROM employees;Even if every row has a non-NULL commission_pct, the invalid conversion can still be evaluated. The exact error can also depend on implicit conversion and query optimization.
NVL2 chooses between its second and third arguments based on its first argument:
SELECT employee_id,
NVL2(commission_pct,
salary + salary * commission_pct,
salary) AS total_pay
FROM employees;For safe SQL, assume that expressions in both result arguments of NVL2 may be evaluated. If either branch can fail, use a searched CASE expression instead:
SELECT employee_id,
CASE
WHEN commission_pct IS NOT NULL
THEN salary + salary * commission_pct
ELSE salary
END AS total_pay
FROM employees;CASE uses short-circuit evaluation for its conditions and selected result expressions. It is therefore the clearest choice when each branch has different safety or performance requirements.
Choosing the function
| Function | Argument pattern | Evaluation guidance |
|---|---|---|
| `NVL` | One value and one replacement | Do not rely on short-circuiting |
| `NVL2` | One test value and two results | Do not rely on unused result branches being harmless |
| `COALESCE` | Two or more fallback values | Evaluates from left to right and stops at the first non-`NULL` value |
| `CASE` | Explicit conditions and results | Use when branch logic or safety must be obvious |
This diagram shows the practical evaluation order:
Practical decision rule
Use COALESCE for a list of fallback columns or expressions:
SELECT COALESCE(mobile_phone, work_phone, 'no phone') AS phone
FROM contacts;Use NVL when you need a simple two-value replacement and both expressions are safe. Use NVL2 when one test value selects between two safe results. Use CASE when branches can raise errors, have different costs, or need explicit conditions.
Exercise
Which construct should you prefer when the fallback expression might raise an error but should be ignored if the first value is non-NULL?
Show answer
Prefer COALESCE, or use CASE when you need explicit conditions and branches.