Sub topic 4
How NULL Behaves in Expressions
This chapter assumes you can write basic SELECT ... FROM ... WHERE queries, recognize common numeric and text types, and use GROUP BY with aggregates such as COUNT and SUM.
NULL represents a missing, unknown, or not-applicable value. It is not the same as zero, an empty string, or the text 'NULL'. SQL expressions follow special rules when NULL is involved.
NULL propagates through expressions
Most arithmetic with NULL produces NULL. SQL cannot determine the result when one required input is unknown.
SELECT order_id,
quantity,
unit_price,
quantity * unit_price AS line_total
FROM order_items;If quantity is NULL, line_total is also NULL. The same rule applies to addition, subtraction, and division.
Text concatenation also needs care. In many SQL systems, concatenating a NULL value produces NULL, although the exact concatenation operator differs by dialect.
| Expression | Result | Column | Column | Column | Column |
|---|---|---|---|---|---|
| `10 + NULL` | `NULL` | ||||
| `'Paid' | NULL` | `NULL` in dialects using ` | ` | ||
| `NULL = NULL` | Unknown | ||||
| `NULL IS NULL` | True |
A NULL result does not necessarily mean an error. It means the expression has no known value.
Comparisons use three-valued logic
SQL comparisons can produce three outcomes: true, false, or unknown. A comparison with NULL normally produces unknown because SQL cannot compare a known value with an unknown value.
SELECT employee_id, commission
FROM employees
WHERE commission = NULL;This query does not find employees whose commission is missing. The condition is unknown for every row, so the WHERE clause keeps none of them.
Use IS NULL and IS NOT NULL for null checks:
SELECT employee_id, commission
FROM employees
WHERE commission IS NULL;SELECT employee_id, commission
FROM employees
WHERE commission IS NOT NULL;The WHERE clause returns only rows where its condition is true. Both false and unknown conditions are excluded.
Logical operators and NULL
Unknown values also affect AND, OR, and NOT.
TRUE AND UNKNOWNproduces unknown.FALSE AND UNKNOWNproduces false.TRUE OR UNKNOWNproduces true.FALSE OR UNKNOWNproduces unknown.NOT UNKNOWNremains unknown.
This explains a common NOT IN problem. If a subquery used by NOT IN returns even one NULL, the comparison can become unknown for every candidate row. Use NOT EXISTS or remove NULL values from the subquery when that situation is possible.
Aggregates treat NULL differently
Aggregate functions usually ignore NULL inputs.
SELECT department_id,
COUNT(*) AS rows_in_department,
COUNT(commission) AS known_commissions,
SUM(commission) AS commission_total
FROM employees
GROUP BY department_id;COUNT(*) counts every row. COUNT(commission) counts only rows with a non-NULL commission. SUM(commission) ignores missing commissions. If every input to SUM is NULL, the result is usually NULL, not zero.
Use COALESCE when a fallback value is appropriate:
SELECT employee_id,
salary + COALESCE(commission, 0) AS total_pay
FROM employees;COALESCE returns the first non-NULL argument. Here, a missing commission is treated as zero for this calculation. That choice is meaningful only if missing commission really means no commission.
NULLIF can deliberately produce NULL, which is useful for avoiding division by zero:
SELECT total_revenue / NULLIF(order_count, 0) AS average_order_value
FROM sales_summary;When order_count is zero, NULLIF returns NULL, so the result is unknown rather than an invalid division.
Practical checklist
- Use
IS NULL, not= NULL. - Expect most arithmetic involving
NULLto returnNULL. - Remember that
WHEREkeeps only true conditions. - Distinguish
COUNT(*)fromCOUNT(column). - Use
COALESCEonly when its replacement value matches the business meaning. - Check subqueries for
NULLbefore usingNOT IN.
Exercise
What rows does WHERE discount = NULL return, and what condition should replace it when you want rows with missing discounts?
Show answer
It returns no rows because the comparison is unknown. Use WHERE discount IS NULL.