Sub topic 6
NULL Propagation in Arithmetic and String Expressions
Assume you know basic SELECT, WHERE, ORDER BY, and table structure. This chapter focuses on how NULL moves through calculations and text expressions.
NULL in arithmetic
NULL means that a value is missing, unknown, or not applicable. It is not the same as zero. In an arithmetic expression, an unknown input usually makes the whole result unknown:
CREATE TABLE employees (
name VARCHAR(50) NOT NULL,
salary NUMERIC(10, 2),
commission NUMERIC(10, 2)
);
INSERT INTO employees (name, salary, commission) VALUES
('Ada', 50000.00, NULL),
('Ben', 60000.00, 1000.00),
('Cy', NULL, 500.00);
SELECT name,
salary + commission AS total_pay,
salary * 1.10 AS salary_with_raise
FROM employees;For Ada, salary + commission is NULL because the commission is unknown. For Cy, both calculations produce NULL because the salary is unknown. Ben receives numeric results.
This behavior applies to addition, subtraction, multiplication, and division. It also applies when NULL appears inside a more complex expression:
SELECT name,
(salary + commission) * 1.10 AS adjusted_pay
FROM employees;If any required input is NULL, the expression cannot produce a known numeric answer.
| Expression | Result when one input is NULL |
|---|---|
| `10 + NULL` | `NULL` |
| `10 * NULL` | `NULL` |
| `NULL / 2` | `NULL` |
| `10 / NULL` | `NULL` |
NULL does not protect every expression from errors. For example, dividing by zero can still raise an error when the divisor is known to be zero. Use NULLIF when you want a zero divisor to become NULL:
SELECT revenue / NULLIF(order_count, 0) AS average_order_value
FROM daily_sales;If order_count is zero, NULLIF returns NULL, so the division result becomes NULL instead of causing a division-by-zero error.
NULL in string expressions
String concatenation usually propagates NULL too. In systems that use || for concatenation:
SELECT 'Hello, ' || NULL AS greeting;The result is NULL, not Hello, . A nullable middle name can therefore make a complete name disappear:
SELECT first_name || ' ' || middle_name || ' ' || last_name AS full_name
FROM people;If middle_name is NULL, the entire expression may become NULL.
Use COALESCE to substitute a known value before combining strings:
SELECT first_name || ' ' ||
COALESCE(middle_name || ' ', '') ||
last_name AS full_name
FROM people;COALESCE returns its first non-NULL argument. Here, a missing middle name contributes an empty string. Concatenation functions such as CONCAT may treat NULL as an empty string instead, but behavior differs between database systems. Check your system's documentation before relying on that rule.
Predicting propagation
Think of each expression as needing known inputs. If an operation cannot determine its result because an input is NULL, its result is usually NULL. Then inspect each function separately, because functions can define exceptions.
Exercise
What does 10 + NULL return, and how can you safely calculate salary plus an optional commission?
Show answer
It returns NULL. Use salary + COALESCE(commission, 0) when a missing commission should count as zero.