Sub topic 1
What NULL Means: Unknown, Missing, or Not Applicable
Prerequisites
You should know how to write basic SELECT, WHERE, and ORDER BY queries. You should also be familiar with tables, rows, columns, and simple joins.
NULL Is a Marker, Not an Ordinary Value
NULL is a special SQL marker meaning that a value is absent from a row. The absence can have different causes:
- The value is unknown. An employee's birth date has not been recorded.
- The value is missing. A customer skipped an optional form field.
- The value is not applicable. A contractor does not have a company employee number.
- The value has not been decided yet. An order has not received a shipping date.
SQL stores these cases with NULL, but the database does not know which meaning you intended. If those meanings matter, record them separately or document them in your data model.
NULL is different from every ordinary value:
| Stored value | Meaning |
|---|---|
| `0` | A known numeric value equal to zero |
| `''` | A known empty string, where the database supports it |
| `FALSE` | A known Boolean value |
| `NULL` | No value is present or known |
For example, a commission of 0 means the employee has a known zero commission. A NULL commission means the commission is unknown, missing, or not applicable.
A Small Example
CREATE TABLE employees (
id INTEGER PRIMARY KEY,
name VARCHAR(50) NOT NULL,
manager_id INTEGER,
commission NUMERIC(8, 2)
);
INSERT INTO employees (id, name, manager_id, commission) VALUES
(1, 'Ada', NULL, NULL),
(2, 'Ben', 1, 100.00),
(3, 'Cy', 1, NULL),
(4, 'Di', 2, 300.00);
SELECT id, name, manager_id, commission
FROM employees
ORDER BY id;Ada has no manager, so manager_id is NULL. Ada and Cy have no recorded commission, so their commission values are also NULL. These two columns use NULL for different business reasons.
A NULL value does not mean that Ada has manager number zero, an empty manager name, or a false manager relationship. It means no manager value is stored.
Why the Distinction Matters
SQL expressions operate differently when one of their inputs is NULL. You cannot safely treat NULL as zero, an empty string, or false without making an explicit choice.
For example, adding a known salary to an unknown bonus produces an unknown result:
SELECT 50000 + NULL AS total_pay;The result is NULL, because the database cannot determine the total. Replacing the missing bonus with zero is a business decision, not an automatic interpretation:
SELECT 50000 + COALESCE(NULL, 0) AS total_pay;This query returns 50000, but it assumes that a missing bonus should count as zero.
A Useful Mental Model
Treat NULL as “the database does not have a usable value here,” rather than as a value that can be compared or calculated normally. This model prepares you for SQL's three-valued logic: conditions involving NULL can be TRUE, FALSE, or UNKNOWN.
Exercise
In the employees example, what is the difference between a commission of 0 and a commission of NULL?
Show answer
0 is a known commission amount equal to zero. NULL means no commission value is available, whether because it is unknown, missing, or not applicable.