Sub topic 3
How NULL propagates through arithmetic and concatenation
This chapter assumes you can write basic SELECT statements with WHERE and column aliases, and that you know the NUMBER, VARCHAR2, and DATE types. You should also be comfortable with the DUAL table and with COUNT, SUM, and AVG. The previous chapters covered what NULL means and why = never matches it. This chapter covers what NULL does inside expressions.
The propagation rule
NULL means "unknown value". If one input to a calculation is unknown, the result is unknown too. Oracle follows this rule in arithmetic: any operation with a NULL operand returns NULL.
SELECT NULL + 1 AS add_result,
NULL * 0 AS multiply_result,
10 - NULL AS subtract_result,
NULL / 5 AS divide_result
FROM dual;All four columns come back empty. SQL*Plus and most tools show NULL as a blank cell or as (null).
NULL * 0 surprises many people. You might expect 0, because anything times zero is zero. Oracle does not simplify the expression. It sees an unknown operand and returns an unknown result.
The same rule applies to date arithmetic. A date plus an unknown number of days is an unknown date:
SELECT SYSDATE + NULL AS unknown_date FROM dual;Most built-in single-row functions follow the rule as well. UPPER(NULL), LENGTH(NULL), ABS(NULL), ROUND(NULL, 2), and TO_CHAR(NULL) all return NULL. The exceptions are functions designed to handle NULL, such as NVL, NVL2, and COALESCE, which later chapters cover.
A worked example: order lines
The examples below use a small table so you can run everything yourself. Each line has a quantity, a unit price, an optional discount (a fraction such as 0.1 for 10%), and an optional note.
CREATE TABLE order_lines (
line_id NUMBER PRIMARY KEY,
qty NUMBER,
unit_price NUMBER,
discount NUMBER,
note VARCHAR2(50)
);
INSERT INTO order_lines VALUES (1, 2, 10, 0.1, 'gift');
INSERT INTO order_lines VALUES (2, 3, 5, NULL, NULL);
INSERT INTO order_lines VALUES (3, NULL, 8, 0.2, 'rush');
COMMIT;Now compute a gross amount and a net amount after discount:
SELECT line_id,
qty * unit_price AS gross,
qty * unit_price * (1 - discount) AS net,
qty * unit_price * (1 - NVL(discount, 0)) AS net_fixed
FROM order_lines
ORDER BY line_id;| LINE_ID | GROSS | NET | NET_FIXED |
|---|---|---|---|
| 1 | 20 | 18 | 18 |
| 2 | 15 | (null) | 15 |
| 3 | (null) | (null) | (null) |
Line 2 has no discount. The sub-expression (1 - discount) is NULL, so the whole product is NULL. The fix is to treat a missing discount as "no discount", which is a statement about what the data means. NVL(discount, 0) expresses that, and the next chapter covers NVL in detail.
Line 3 has no quantity. NVL(qty, 0) would turn the gross amount into 0, but that would be a business decision, not a harmless default. A missing discount really does mean no discount. A missing quantity might mean the data is incomplete, and a 0 would hide that. Before you replace a NULL, decide what the NULL represents.
One NULL poisons the whole expression
Propagation does not stop at the first operator. In a long expression, a single NULL anywhere makes the entire result NULL:
SELECT 1 + 2 + 3 + NULL + 5 AS total FROM dual;The result is NULL, not 11. This matters when you add up several optional columns, for example base_pay + bonus + overtime + allowance. If any one of them is NULL for a row, that row's total disappears. You must wrap each optional column, not just the one you suspect.
The same thing happens in the HR sample table. The expression salary + salary * commission_pct is NULL for every employee without a commission:
SELECT employee_id,
salary,
commission_pct,
salary + salary * commission_pct AS total_with_null,
salary + salary * NVL(commission_pct, 0) AS total_pay
FROM employees;For employees with no commission, total_with_null is NULL and total_pay equals their salary.
The loop in the diagram shows why the damage spreads. A NULL result becomes an operand of the next operator, which makes that result NULL as well.
Concatenation is the exception
Concatenation with || does not follow the propagation rule. Oracle treats a NULL operand as an empty string and returns the other operand unchanged:
SELECT 'abc' || NULL AS right_null,
NULL || 'abc' AS left_null,
'a' || NULL || 'b' AS middle_null
FROM dual;The results are abc, abc, and ab. Nothing is returned as NULL. Only when every operand is NULL is the result NULL (an empty string, which Oracle treats as NULL; the next chapter explains why).
Here is what that does to a label built from the order lines:
SELECT line_id,
'Line ' || line_id || ': ' || note AS label
FROM order_lines
ORDER BY line_id;| LINE_ID | LABEL |
|---|---|
| 1 | Line 1: gift |
| 2 | Line 2: |
| 3 | Line 3: rush |
Line 2 still produces a string, with a dangling : and nothing after it. The CONCAT function behaves the same way. It treats a NULL argument as an empty string, so CONCAT('abc', NULL) returns abc.
Numbers inside concatenation
When you concatenate a number, Oracle converts it to text first. A NULL number converts to NULL text, which || then treats as empty:
SELECT 'Qty: ' || qty AS qty_label
FROM order_lines
ORDER BY line_id;Line 3 returns Qty: with nothing after it. This looks like a harmless blank, but it differs from arithmetic on the same column, where qty * unit_price returns NULL. The same NULL column can disappear in one expression and produce a half-built string in another.
Arithmetic inside concatenation
Precedence matters when you mix the two. The || operator has the same precedence as + and -, and Oracle evaluates them left to right. Parenthesize arithmetic to make the intent clear:
SELECT line_id,
'Gross: ' || (qty * unit_price) AS gross_label
FROM order_lines
ORDER BY line_id;Line 3 returns Gross: . The multiplication produced NULL, and the concatenation then dropped it.
Propagation in functions and conditions
Functions that take NULL as input and return a value other than NULL are the exception, so it is worth listing the common behaviors side by side.
| Expression | Result | Why | Column | Column |
|---|---|---|---|---|
| `NULL + 1` | NULL | Arithmetic propagates NULL | ||
| `'a' \ | \ | NULL` | `a` | Concatenation treats NULL as empty |
| `UPPER(NULL)` | NULL | Most single-row functions propagate NULL | ||
| `LENGTH(NULL)` | NULL | Not 0 | ||
| `NULL = NULL` | Unknown | Comparison yields unknown, not true | ||
| `NVL(NULL, 0)` | 0 | Designed to replace NULL |
LENGTH(NULL) returning NULL rather than 0 catches people who validate string lengths. A check such as WHERE LENGTH(note) > 0 silently excludes NULL rows. That is probably what you want, but be aware that it is happening.
Comparison operators follow the rule too. qty * unit_price > 10 is unknown when qty is NULL, so WHERE discards the row. A WHERE clause keeps only rows where the condition is true. Rows with NULL inputs are dropped from the result, not flagged.
NULL and aggregates over expressions
Aggregates ignore NULL values in their input, but the expression inside the aggregate is evaluated per row first. This produces a difference that is easy to miss:
SELECT SUM(qty) + SUM(unit_price) AS sum_then_add,
SUM(qty + unit_price) AS add_then_sum
FROM order_lines;The first expression returns 28. SUM(qty) is 5 (the NULL is ignored), SUM(unit_price) is 23, and 5 + 23 is 28.
The second expression returns 20. For line 3, qty + unit_price is NULL, so that row contributes nothing. The sum is (2 + 10) + (3 + 5), which is 20. The 8 from line 3's unit price is lost.
Both queries sum "the same" columns, yet they give different answers whenever NULLs fall in different rows. When you total a calculated expression, decide whether you want rows with any NULL excluded (SUM(a + b)) or each column totaled on its own.
Practical guidelines
- Find out which columns allow NULL before you write arithmetic on them. The data dictionary view
USER_TAB_COLUMNSlists aNULLABLEflag for each column. - Wrap each nullable operand, not the whole expression.
NVL(a + b, 0)turns a row with only one NULL into 0, discarding the known value.NVL(a, 0) + NVL(b, 0)keeps it. - Choose the replacement value deliberately. 0 is neutral for addition and for a discount, but wrong as a default divisor, and for a quantity it may hide missing data.
- In string building, check each nullable part separately if the output format matters, for example to avoid doubled commas.
Exercise
Given the orderlines table above, what do these two expressions return for line 3 (qty is NULL, unitprice is 8, note is 'rush')? (a) qty * unit_price (b) 'Qty: ' || qty || ' units'
Show answer
(a) NULL, because arithmetic with a NULL operand returns NULL. (b) 'Qty: units'. The || operator treats the NULL qty as an empty string, so the text before and after it is kept, leaving two spaces between "Qty:" and "units".
Exercise
A query computes SUM(salary + bonus) over a table where bonus is NULL for some employees. Management says the total is too low. Why, and how would you change the expression so every salary is counted?
Show answer
For each row where bonus is NULL, salary + bonus is NULL, so SUM ignores that row and the employee's salary is missing from the total. Replace the missing bonus with 0 before adding: SUM(salary + NVL(bonus, 0)). Alternatively, SUM(salary) + SUM(bonus) counts every salary, because SUM skips only the NULL bonus values.