NULL Values: Beginner
Three things that look alike
| Value | What it says | Example |
|---|---|---|
| NULL | The value is unknown or was never supplied | A customer skipped the age field |
| 0 | The quantity is known, and it is zero | An account whose balance is 0 dollars |
| '' (empty string) | A text value exists and has 0 characters | A nickname field submitted blank |
- ▸The person or system never supplied anything: NULL
- ▸A quantity was reported and it is zero: 0
- ▸A text field was submitted with nothing typed in it: '' or NULL, and the schema should say which
- ▸A value is unknown but the column is NOT NULL: the schema is wrong, and any placeholder will corrupt every aggregate over it
The customers table
The running example is a small online outdoor-gear store with a customers table. Four customers have signed up. Ann and Cara gave phone numbers, Ben and Dev did not. The store_credit column holds a dollar amount: Ann and Dev have 0, Cara has 25, and Ben's row holds NULL because his account was migrated from an older system that never tracked credit. The referral_code column is text: Ann typed FRIEND10, Ben and Dev submitted the field blank, and Cara's signup predates the field entirely. The query below puts all 3 cases through arithmetic and a string function.
Why store a gap instead of a placeholder
An order arrives through a partner channel whose feed has no phone field at all. Which value should the phone column hold for that order?
What a comparison with NULL returns
Every column comes back NULL, which is how a result grid displays UNKNOWN. NULL = NULL surprises people most. Two unknown values might be equal and might not, so the database declines to say either way. The arithmetic column shows the same rule outside comparisons: 5 plus an unknown amount is an unknown amount, and that NULL flows through any further expression built on top of it.
WHERE keeps only TRUE
A WHERE clause lets a row through when its condition evaluates to TRUE. FALSE drops the row, and UNKNOWN drops it in exactly the same way. That second drop is silent: no error, no warning, the row is absent from the result and nothing says so. The store's orders table has 5 orders, and 2 of them, ORD-002 and ORD-004, have no region recorded because they came through a partner channel that does not send one. Three filters over that table show what the gaps do.
In a two-valued world, the third query would return every row: a value either equals US or it does not. SQL returns 3 of 5. The 2 orders without a region are excluded by = 'US', excluded by <> 'US', and excluded by the combination, because an UNKNOWN joined to an UNKNOWN with OR stays UNKNOWN. Those rows did not fail the test. They were never able to pass it.
The sharpest case is a filter written against NULL itself. A developer who wants the orders with no region reaches for WHERE region = NULL, and the comparison reads as if it should work. Watch what the database does with it on each of the 5 rows.
SELECT*FROM ordersWHERE region = NULL
Combining UNKNOWN with AND and OR
| Expression | Result | Why |
|---|---|---|
| TRUE AND UNKNOWN | UNKNOWN | The outcome depends on the unknown half |
| FALSE AND UNKNOWN | FALSE | One FALSE settles an AND regardless |
| TRUE OR UNKNOWN | TRUE | One TRUE settles an OR regardless |
| FALSE OR UNKNOWN | UNKNOWN | The outcome depends on the unknown half |
| NOT UNKNOWN | UNKNOWN | The opposite of unknown is still unknown |
The table has 1 shape worth memorising: UNKNOWN disappears only when the other operand decides the answer by itself. amount > 1000 AND region = 'US' on ORD-004, amount 300 and region NULL, evaluates FALSE AND UNKNOWN, which is FALSE, and the row is dropped for a reason anyone can see. amount > 100 AND region = 'US' on the same row evaluates TRUE AND UNKNOWN, which is UNKNOWN, and the row is dropped for a reason nothing reports. Both queries lose ORD-004; only the second loses it to the gap.
The orders table holds 5 rows: 2 with region US, 1 with EU and 2 with NULL. How many rows does WHERE region <> 'EU' return?
WHERE clause, ask whether the column can hold NULL. If it can, decide what the query should do with those rows, because the default is to drop them without a trace.IS NULL and IS NOT NULL are the 2 predicates built to test for NULL, and unlike = or <> they never return UNKNOWN. phone IS NULL returns TRUE when the cell is empty and FALSE when it holds anything at all; phone IS NOT NULL returns the reverse. Because the answer is always TRUE or FALSE, a WHERE clause built on them keeps or drops every row deliberately, with no silent third category.
Finding the rows with a gap
Two rows return, Ben and Dev, and the phone column in both is NULL. Compare that with WHERE phone = NULL, which produces UNKNOWN on all 4 rows and returns nothing at all. The animation below plays the correct version row by row, so the difference in evaluation is visible.
SELECTcustomer_id,name,phoneFROM customersWHERE phone IS NULL
Ann's row evaluates IS NULL to FALSE and is dropped; Ben's evaluates TRUE and moves into the result; Cara is dropped and Dev kept. Four decisions, each TRUE or FALSE, none UNKNOWN. That determinism is the whole value of the operator, and it is why IS NULL is a hard rule for testing absence and not a stylistic preference.
Keeping only the known values
IS NOT NULL does the opposite job, and it earns its place most often alongside other conditions. A report of large orders by region has no use for an order whose region is missing, however large the order is. Adding region IS NOT NULL states that choice inside the query, so the next person to read it knows the unknown rows were excluded on purpose.
ORD-003 and ORD-005 return. ORD-002 at 200 and ORD-004, the largest order at 300, are absent, and the IS NOT NULL clause is the reason. Without it, amount > 120 alone evaluates TRUE on both rows, so they would have appeared with a blank region and the report would show an unlabelled bucket worth 500. Which outcome is right depends on the report, and the clause makes the decision visible either way.
IS NULL on an expression
The predicate accepts any expression, not only a bare column. store_credit + 10 IS NULL is TRUE for Ben, because the sum of an unknown amount and 10 is unknown, and FALSE for the other 3 customers. That makes IS NULL the way to check whether a calculation lost a value somewhere along the way, which is how a data quality query finds the rows where a derived column would come out blank before anyone publishes it.
The predicate that looks right and is wrong
- Evaluate to UNKNOWN on every row, including the NULL ones
- Return 0 rows from a WHERE clause, with no error
- Read as a bug only after someone notices rows are missing
- Evaluate to TRUE or FALSE on every row
- Return exactly the rows with, or without, a value
- State the intent in a form the next reader recognises
Operationally, IS NULL is a data quality tool before it is a filter. A query such as WHERE shipping_address IS NULL AND status = 'pending' lists the orders that cannot ship, and the same shape finds employees without a manager, products without a price, and events without a timestamp. The pattern is always the same: name the column, ask whether it is missing, and add whatever other conditions narrow the audience to the rows someone will act on.
WHERE so a reader finds the completeness rule at the top of the filter. The database may check the conditions in any order, but a row passes only when every one of them is TRUE.COUNT is 1 function with 2 behaviours, and the argument decides which one runs. COUNT(*) counts rows: every row in the group, whatever its cells hold. COUNT(column) counts the rows where that column holds a value, and a NULL cell adds nothing to the tally. On a table with no missing values the 2 numbers agree, which is why many engineers never notice the difference until a column with gaps makes them disagree in a report.
The orders table with its gaps
| order_id | amount | discount | region |
|---|---|---|---|
| ORD-001 | 100 | 10 | US |
| ORD-002 | 200 | NULL | NULL |
| ORD-003 | 150 | 15 | EU |
| ORD-004 | 300 | NULL | NULL |
| ORD-005 | 250 | 25 | US |
The result is 5, 3 and 3. COUNT(*) never looks inside a row, so it reports the full 5. COUNT(discount) examines the discount cell on each row and skips the 2 that hold NULL, and COUNT(region) does the same on its own column. The animation plays the first 2 counts side by side as the rows arrive.
SELECTCOUNT(*) AS total_rows,COUNT(discount) AS rows_with_discountFROM orders
Each row ticks total_rows upward. Only ORD-001, ORD-003 and ORD-005 tick rows_with_discount, because ORD-002 and ORD-004 present a NULL discount to the function and it declines to count a value that does not exist. The 2 counters start together and finish 2 apart, and that gap is a measurement.
- ▸COUNT(*): every row in the group
- ▸COUNT(column): rows where the column holds a value
- ▸COUNT(DISTINCT column): distinct values, with NULL excluded
- ▸COUNT(1): every row, because the constant 1 is never NULL
Completeness in one query
The difference between the 2 counts is the number of missing values, and putting both into 1 query turns a table scan into a data quality report. The shape below profiles a users table on 2 optional columns at once, and it extends to as many columns as the table has, in a single pass over the data.
The gap, COUNT(*) - COUNT(email), is the number to report when a team asks how complete a dataset is, because it counts records someone may have to chase. COUNT(DISTINCT email) follows the same rule as COUNT(email) and ignores NULL, so a distinct count never includes a bucket for missing values, and a table with 3 distinct emails and 400 NULLs reports 3.
COUNT(1) and other constants
COUNT(1) returns the same number as COUNT(*), and the reason is the rule above, not a special case: the expression 1 is evaluated once per row, it is never NULL, so every row is counted. The same holds for COUNT('x') or COUNT(0). A constant inside COUNT counts rows; only a column, or an expression that can produce NULL, counts values. Knowing that keeps a reader from assuming COUNT(1) skips anything.
A users table has 1,000 rows. COUNT(email) returns 940. Which statement must be true?
COUNT(*) with COUNT(column) whenever a result will be reported as a count of something: the first says how many records exist and the second how many of them carry the thing being counted.SUM, AVG, MIN and MAX follow the same rule as COUNT(column): a NULL cell contributes nothing. They do not treat NULL as 0. They behave as though the row were absent from the calculation, and that distinction is where reports go wrong, because an average over the 3 known discounts and an average across all 5 orders are 2 different numbers, and the function only ever computes the first.
SUM and AVG over the discount column
The total is 50, from 10 + 15 + 25, and the average is 16.67, from 50 divided by 3. The 3 in that division is COUNT(discount), the number of rows that supplied a value. ORD-002 and ORD-004 appear in neither figure, and the result row gives no hint that 2 orders were left out.
SELECTSUM(discount) AS total_discount,AVG(discount) AS avg_discountFROM orders
Two averages, one question
The margin forecast from the intro failed on exactly this point. Finance asked for the average discount per order, meaning across every order placed. AVG(discount) answers a different question: the average discount among orders that had one. Both are legitimate figures, and the query has to be written for the one that was asked. The switcher runs both over the same 5 orders.
The second form is the one the forecast needed, and it costs nothing extra: the sum is unchanged and the denominator switches to the row count. The choice between 16.67 and 10 is a business definition, so write the definition into the column name, avg_discount_where_present or avg_discount_per_order, and nobody downstream has to guess which question the number answers.
MIN and MAX
MIN(discount) is 10 and MAX(discount) is 25, and both skip the NULL rows without ceremony. For extremes that behaviour is usually what the question wants: the smallest discount applied is 10, and no sensible reading would answer 'unknown'. The caveat is in the wording. MIN(discount) is the minimum among discounts that exist, which is a different figure from the least the store gave on any order, since 2 orders received no discount at all and a report that calls 10 the minimum has quietly redefined the population.
When every value is missing
An aggregate over a column that holds only NULLs has no values to work with, and the functions diverge. COUNT returns 0, because counting nothing yields a number. SUM, AVG, MIN and MAX return NULL, because there is no sum, average or extreme of an empty set. The employees table below has a bonus column that nobody has filled in yet, and the 4 aggregates over it show the split.
The result is 3, 0, NULL, NULL. A downstream calculation that adds total_bonus to a salary total will itself turn NULL, through the same arithmetic rule from the first section, and a dashboard tile goes blank instead of showing 0. The intermediate lesson covers COALESCE, the function that substitutes a default where an aggregate comes back NULL. For now, a NULL total means no values were present, which is different information from a total of 0, and a report should carry that difference rather than paper over it.
NULL as a GROUP BY key
GROUP BY treats NULL differently from a comparison: all rows with a NULL key land in 1 group together, as though NULL equalled NULL for the purpose of grouping. Grouping the orders by region produces 3 groups, US, EU and a NULL group holding ORD-002 and ORD-004, and the aggregates inside each group follow the same skipping rules as before.
| region | order_count | total_amount | total_discount |
|---|---|---|---|
| US | 2 | 350 | 35 |
| EU | 1 | 150 | 15 |
| NULL | 2 | 500 | NULL |
The NULL group's total_discount is NULL, which is the all-missing case from the previous query appearing inside a group. A reader of that row learns that 2 orders worth 500 have no discount information at all, a fact the report should carry forward to whoever owns the partner channel.
- Store NULL where a value was never supplied, and let each query decide what to do with it
- Test for absence with IS NULL and IS NOT NULL, never with = or <>
- Report COUNT(*) alongside COUNT(column) so missing values are visible in every count
- Decide which denominator an average needs before choosing AVG or SUM / COUNT(*)
- Expect NULL, and plan for it, from SUM and AVG over a column with no values
- Don't write 0, '', -1 or 'N/A' into a column to stand for unknown
- Don't assume rows missing from a WHERE result all evaluated FALSE
- Don't read an empty result from = NULL as proof that nothing is missing
- Don't present AVG(column) as an average per row when some rows hold NULL
> A subscription software company's finance team asks for the average discount per customer for the quarter. The subscriptions table holds 12,000 rows, and 3,800 of them have a NULL discount_pct because those customers pay list price and the signup flow writes nothing for them. An analyst runs AVG(discount_pct), gets 18 percent, and finance models margin on it. The true average per customer is 12.3 percent.
How SQL stores missing data, and why filters, counts and averages treat it unlike any other value.
NULL: the billion dollar mistake
- Category
- SQL
- Difficulty
- beginner
- Duration
- 25 minutes
- Challenges
- 11 hands-on challenges
Topics covered: NULL vs zero vs empty string, Three-Valued Logic, IS NULL and IS NOT NULL operators, COUNT(*) vs COUNT(column), Aggregates with NULLs
Lesson Sections
- NULL vs zero vs empty string
NULL marks a cell whose value the database does not have. A customer who skipped the phone field at checkout, a product with no discount price, the CEO's manager: in each case there is nothing true to store, and NULL records that there is nothing. Every other value in a column makes a claim about the world. NULL is the admission that no claim can be made, and a database that can say so holds more information than one forced to write something in every cell. Three things that look alike Zero, the
- Three-Valued Logic (concepts: Three-Valued Logic)
Most programming languages evaluate a comparison to TRUE or FALSE. SQL has a third outcome, UNKNOWN, and every comparison that touches NULL produces it. The rule follows from what NULL means: if the database does not know Ben's store credit, it cannot say whether that credit equals 25, is greater than 0, or even equals another unknown credit. Every yes-or-no question about a missing value gets the only honest answer, which is neither. What a comparison with NULL returns WHERE keeps only TRUE Eve
- IS NULL and IS NOT NULL operators (concepts: Null Handling)
Finding the rows with a gap The store wants to call every customer who has not given a phone number, so the support team can collect one. Of the 4 customers, Ben and Dev left the field empty. The query asks for them directly, naming the column and the condition on it. Keeping only the known values IS NULL on an expression The predicate that looks right and is wrong
- COUNT(*) vs COUNT(column) (concepts: Counting)
The orders table with its gaps Two of the 5 orders carry no discount, and the same 2 carry no region, because the partner channel that placed them sends neither field. Three counts over this table tell the completeness story in a single row of output. Completeness in one query COUNT(1) and other constants
- Aggregates with NULLs (concepts: Sums & Averages)
SUM and AVG over the discount column Watching the sum accumulate shows the skip: 10 after ORD-001, nothing from ORD-002, 25 after ORD-003, nothing again from ORD-004, and 50 after ORD-005. The average divides by the number of values that arrived, and the 2 that never arrived are absent from the denominator as well as the numerator. Two averages, one question MIN and MAX When every value is missing NULL as a GROUP BY key