Learn SQL
-- filtering.null_checks

IS NULL / IS NOT NULL in SQL

Detect missing values — the = NULL trap

--concept

NULL means “unknown / absent”, and it is never equal to anything — not even another NULL. So WHERE bonus = NULL matches nothing and WHERE bonus != NULL silently drops rows. The only correct tests are IS NULL (keep missing) and IS NOT NULL (keep filled). Functions like COALESCE(bonus, 0) substitute a default for display or math.

--how it works
1

NULL means unknown

Not zero, not empty string — the absence of a value. Three employees have no bonus recorded; that is information, not corruption.

2

NULL is never equal to anything

= NULL and != NULL both evaluate to UNKNOWN, even NULL = NULL. WHERE keeps TRUE only — so both tests match nothing.

3

Use IS NULL / IS NOT NULL

The only correct tests: WHERE bonus IS NULL or WHERE bonus IS NOT NULL.

4

Substitute for display: COALESCE

COALESCE(bonus, 0) renders missing bonuses as 0 without changing stored data.

5

Aggregates skip NULLs

COUNT(*) = 6 but COUNT(bonus) = 3 — NULLs are ignored by COUNT(col), SUM, AVG, MIN, MAX.

--visualization
COUNT(*) counts all rows · COUNT(bonus) skips NULLs
idnamedepartmentbonus1AliceEngineering5,0002BobSalesNULL3CynthiaEngineeringNULL4DavidMarketing3,0005ElenaSales2,5006FaridEngineeringNULLNULL = unknown — never use bonus = NULL (it matches zero rows, always)
matches predicate filtered out NULL cells
Stage 1 / 8
bonus can be NULL (unknown). WHERE bonus IS NULL — remember: = NULL and != NULL match NOTHING.
--example
query.sql
1SELECT name, bonus
2FROM employees
3WHERE bonus IS NULL;
Result
namebonus
BobNULL
CynthiaNULL
FaridNULL

Three employees have no bonus recorded. Flip the toggle to IS NOT NULL and the opposite trio appears — Alice, David and Elena.

--variants

IS NULL

Find the missing values.

query.sql
1SELECT name, bonus FROM employees
2WHERE bonus IS NULL;
Result
namebonus
BobNULL
CynthiaNULL
FaridNULL
1Three rows have no bonus recorded.
2bonus = NULL would have returned nothing — the classic bug.
3NULL prints literally in the result table.

IS NOT NULL

Find the filled values.

query.sql
1SELECT name, bonus FROM employees
2WHERE bonus IS NOT NULL
3ORDER BY bonus DESC;
Result
namebonus
Alice5000
David3000
Elena2500
1The complement of IS NULL — three employees with bonuses.
2ORDER BY sorts them high to low.
3NULL rows never leak into either side by accident.

COALESCE default

Print a stand-in for NULL.

query.sql
1SELECT name, COALESCE(bonus, 0) AS bonus_or_zero
2FROM employees;
Result
namebonus_or_zero
Alice5000
Bob0
Cynthia0
David3000
Elena2500
Farid0
1COALESCE returns its first non-NULL argument.
2Filled bonuses pass through; missing ones become 0 in the output.
3The stored table is unchanged — this is display-layer only.

COUNT(*) vs COUNT(col)

Aggregates ignore NULLs.

query.sql
1SELECT COUNT(*) AS all_rows,
2 COUNT(bonus) AS with_bonus
3FROM employees;
Result
all_rowswith_bonus
63
1COUNT(*) counts rows: 6.
2COUNT(bonus) counts non-NULL bonuses: 3.
3AVG(bonus) divides by 3, not 6 — NULLs never dilute averages.
--key facts
Three-valued logicTRUE / FALSE / UNKNOWN — WHERE keeps TRUE only
Count differenceCOUNT(*) = 6 but COUNT(bonus) = 3 on our table
DefaultsCOALESCE(bonus, 0) renders missing bonuses as 0
--use cases
Optional fields: bonus, middle_name, deleted_at — everywhere data is genuinely absent
Reports: COALESCE defaults so tables show 0 instead of blanks
Data-quality checks: WHERE email IS NULL OR email = ''
Soft deletes: WHERE deleted_at IS NULL = "not deleted"
--under the hood
NULL needs an extra bit per row to mark presence — some engines hide it in a null bitmap.
Indexes typically skip NULL entries (or include them, engine-specific) — know your engine before relying on IS NULL lookups.

Recommended resourcesAffiliate links · may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Pro code editor & AI assistantWrite, debug and explain code faster while you practice the patterns here.Try it free Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books

Practice what you learned

Take the queries you just visualized and see the data structures that power real databases.