NULL vs zero vs empty string
Every cell in a table holds one of 2 things: a value, or a marker saying that no value was recorded. That marker is NULL. It is how a database writes "we don't know" or "this doesn't apply" inside a cell.
Zero and the empty string both look like nothing, yet both are values. 0 is a number that someone measured or typed. '' is a piece of text that happens to contain no characters. NULL marks the absence of any value, which is why a column of any type can hold it: an INTEGER column can, and so can a VARCHAR or BOOLEAN one.
Databases need that marker because the alternative is to invent a value. A made-up birth year or a made-up price looks exactly like a real one, and every report built on the column would treat it as a measurement.
One table, 3 kinds of nothing
A shop keeps a products table. stock is an INTEGER counting units on the shelf, and description is a VARCHAR that the seller types into a form.
| product_id | name | stock | description |
|---|---|---|---|
| 1 | Desk Lamp | 12 | 'Brass, 40cm tall' |
| 2 | Notebook | 0 | 'A5, 80 pages' |
| 3 | Mug | NULL | '' |
| 4 | Backpack | 7 | NULL |
What each cell says
- Notebook, stock 0: someone counted the shelf and found none. Sold out is a fact you can act on.
- Mug, stock NULL: no count was recorded. The shelf could hold 0 mugs or 500.
- Mug, description '': the seller saved the form with the box blank. An answer exists, and it is zero characters long.
- Backpack, description NULL: no description was ever saved for this product.
Why the difference changes answers
A sold-out product and an uncounted product call for different actions. One needs a reorder, the other needs someone to walk to the shelf. If the table stored 0 for both, the question "which products are sold out?" would return products nobody has counted. The query below asks that question, and a second tab asks which descriptions were saved blank.
| product_id | name | stock | description |
|---|---|---|---|
| 1 | Desk Lamp | 12 | Brass, 40cm tall |
| 2 | Notebook | 0 | A5, 80 pages |
| 3 | Mug | NULL | |
| 4 | Backpack | 7 | NULL |
Zero
stock = 0 matches the Notebook only. The Mug's stock is NULL, and a missing count is a different thing from a count of 0.
SELECT
product_id,
name,
stock
FROM products
WHERE stock = 0| product_id | name | stock |
|---|---|---|
| 2 | Notebook | 0 |
Empty string
description = '' matches the Mug only. The Backpack's description is NULL, so it has no text to compare with the empty string.
SELECT
product_id,
name,
description
FROM products
WHERE description = ''| product_id | name | description |
|---|---|---|
| 3 | Mug |
Where NULLs come from
NULL enters a table for 4 common reasons, and each one is a different story about the same empty cell.
- An optional field was skipped: a customer checked out without entering a phone number.
- The value does not exist yet: an order placed today has no delivery date.
- The question does not apply: a house has no apartment number.
- The column is newer than the row: a loyalty_tier column added this year is NULL for every customer who signed up before it existed.
The cell looks identical in all 4 cases, so the meaning has to come from knowing how the column gets filled.
| In the cell | A value? | Reads as |
|---|---|---|
| 0 | Yes, a number | Measured, and the amount is zero |
| '' | Yes, text | Saved, with no characters in it |
| NULL | No | Nothing was recorded |
Deciding what to store is a modelling choice that someone makes for every column. Try it on a tip column in a food delivery app's orders table.
An orders table has a tip column. For which order is NULL the accurate thing to store?
- The customer tapped the "No tip" button on the checkout screen
- The order is still cooking and the tip screen has not appeared
- The customer typed 0.00 into the custom tip box at checkout
The rest of this lesson keeps coming back to one test: was anything recorded? If something was, the cell holds a value, even when that value is 0 or ''. If nothing was, the cell is NULL, and SQL will refuse to say whether it equals anything at all.
About this lesson
This section is part of the NULL Values: Beginner lesson on DataDriven, the home of data engineering. Lessons cover SQL, Python, Spark, data modeling and pipeline architecture, and each section pairs its explanation with worked examples and practice you run as you go.