Sub topic 1
What NULL Means: Unknown, Missing, or Not Applicable
NULL is the most misunderstood thing in SQL. Most of the surprises in this tutorial come from one mistake: treating NULL as a value. This chapter builds the correct mental model first. The later chapters then cover the rules that follow from it, such as three-valued logic, comparisons, aggregates, and joins.
Prerequisites
This chapter assumes you can:
- Write basic
SELECT,WHERE, andORDER BYqueries. - Describe a table in terms of rows, columns, and data types.
- Read simple
JOINandGROUP BYqueries. These appear only briefly here.
All examples use standard SQL and run on PostgreSQL. Where other systems behave differently, the text says so.
NULL Is a Marker, Not a Value
A NULL is a marker that says "no value is stored here." It is not a number, a string, or a boolean. It has no type of its own and no content to compare. A column of type INTEGER can hold integers or NULL. The NULL is not one of the integers.
This is the source of NULL's odd behavior. Ordinary values can be compared, added, and sorted. A marker meaning "there is nothing here" cannot. The question "is nothing greater than 5?" has no yes-or-no answer.
Here is the sample table used throughout this tutorial:
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);Ada has no manager, so her manager_id is NULL. Ada and Cy have no commission recorded, so their commission is NULL. Ben and Di have values.
Three Meanings Behind One Marker
SQL has a single NULL, but people use it for at least three different situations.
| Meaning | Example in the sample table | What the NULL tells you |
|---|---|---|
| Unknown | Cy may earn commission, but nobody has entered the amount yet | A value exists in the real world. We do not know it. |
| Missing | Ben's record was imported from a system with no commission field | The data was never collected or was lost. |
| Not applicable | Ada's `manager_id`, because she is at the top of the hierarchy | No value could exist for this row. |
The three cases look the same to the database. You can see this in Ada's row, where commission could be unknown and manager_id is not applicable, yet both are NULL. The database cannot tell you which meaning applies.
This has a practical consequence. When you read a NULL, you must decide from the schema and the business rules what it means. If the distinction matters to your application, you have to record it yourself. A common approach is an extra status column or a separate table, covered in the chapter on design choices.
NULL Is Not Zero, an Empty String, or False
The most common beginner error is swapping NULL for a "neutral" value. Each of the following differs from NULL:
- Zero is a number. A commission of
0says "this person earns nothing extra." A NULL commission says "we have no commission figure." Averages, totals, and filters treat the two differently. - An empty string (
'') is a string with zero characters. It is a known value: "the text is blank." A NULL string means no text was stored. - False is a boolean value. It answers a yes-or-no question with "no." NULL, in a boolean context, means the answer is not known.
You can check the empty-string distinction directly:
CREATE TABLE notes (
id INTEGER PRIMARY KEY,
body VARCHAR(100)
);
INSERT INTO notes (id, body) VALUES
(1, NULL),
(2, ''),
(3, 'hello');
SELECT id,
CASE
WHEN body IS NULL THEN 'NULL'
WHEN body = '' THEN 'empty string'
ELSE 'text'
END AS kind
FROM notes
ORDER BY id;On PostgreSQL, SQLite, MySQL, and SQL Server, the result is NULL, empty string, text for ids 1, 2, and 3.
Why the Distinction Changes Query Results
Replacing NULL with zero or an empty string is not a cosmetic choice, because aggregates and filters respond differently. Here is a preview using the sample table. The details are explained in the chapter on aggregates.
SELECT AVG(commission) AS avg_non_null, -- 200.00
AVG(COALESCE(commission, 0)) AS avg_with_zeros -- 100.00
FROM employees;The first average ignores Ada and Cy and divides 400 by 2. The second pretends they earn zero and divides 400 by 4. Neither answer is wrong in general. They answer different questions: "what is the average among people with a recorded commission?" versus "what is the average if unrecorded means zero?" Only you know which one the business wants. That is why the meaning of a NULL matters before you decide how to handle it.
Where NULLs Come From
NULLs enter your results in more ways than an explicit INSERT:
- Omitted columns. If you insert a row without naming a nullable column and the column has no default, the column gets NULL.
- Explicit assignment. You write
NULLin anINSERTorUPDATE. - Outer joins. A
LEFT JOINfills the right-hand columns with NULL for rows that have no match. These NULLs mean "no matching row," which is a fourth meaning that exists only in query results. - Expressions.
NULLIF(a, b)returns NULL when its arguments are equal. ACASEexpression with noELSEreturns NULL when no branch matches. - Propagation. Operations on NULL usually return NULL, so one NULL can spread through a calculation.
The fourth point matters when debugging. A NULL in a query result does not always come from a NULL stored in a table. Before you decide how to treat it, find out which of these sources produced it.
NULL Has a Type, Even Though It Has No Value
A bare NULL literal has no obvious type. Most of the time the database infers one from context, for example from the column it is inserted into. Sometimes it cannot, and you must say what you mean with CAST:
SELECT CAST(NULL AS INTEGER) AS no_number,
CAST(NULL AS VARCHAR(10)) AS no_text;This matters in places like UNION, where both sides need compatible column types, and in some systems when you select a bare NULL into a temporary table. PostgreSQL, for example, may treat an untyped NULL as text in some contexts. If you see an error about "unknown" or mismatched types around a NULL, add a cast.
Common Mistakes at This Stage
- Assuming NULL means zero or blank. This leads to wrong totals, wrong averages, and rows silently dropped from reports.
- Reading one NULL as one meaning. A NULL in
manager_idand a NULL incommissioncan mean different things in the same row. - Using NULL as a flag. Storing NULL to mean "false" or "default" makes queries harder to read and harder to get right. A
NOT NULLcolumn with an explicit default is clearer. - Forgetting that outer joins create NULLs. A NULL in a joined column may mean only that no matching row exists.
Exercise
In the sample employees table, Ada's manager_id is NULL and Cy's commission is NULL. Which of the three meanings (unknown, missing, not applicable) most plausibly applies to each, and why does the database not record the difference? Then say what would be wrong with storing 0 in Cy's commission to avoid the NULL.
Show answer
Ada's manager_id is most plausibly "not applicable," because she is at the top of the hierarchy and no manager can exist. Cy's commission is most plausibly "unknown" or "missing," because a commission could exist but was not recorded. The database stores a single NULL marker for all cases, so the meaning comes only from the schema and business rules. Storing 0 for Cy would claim that Cy is known to earn no commission. That changes results: AVG(commission) would drop from 200.00 to about 133.33 (400 divided by 3 rows with values), and a filter for people with no commission data would no longer find Cy.