IS NULL & IS NOT NULL
Find missing values correctly with IS NULL / IS NOT NULL and understand why = NULL never works.
Data is often incomplete. In player_profiles some players have no nickname, and David Warner has no team. In innings, one of MS Dhoni's scores is missing. NULL marks these unknown values — and it needs its own operator.
Definition
NULL means "no value / unknown". It is not zero, not an empty string
'', and not the text'NULL'.IS NULL returns TRUE when a value is NULL; IS NOT NULL returns TRUE when a value is present. Unlike every other comparison, they never return UNKNOWN — they always give a clear TRUE or FALSE.
Why a special operator? Any normal comparison with NULL (=, <>, >, IN…) asks "is unknown equal to X?" — and the honest answer is UNKNOWN. WHERE drops UNKNOWN rows, so WHERE team = NULL returns nothing, ever.
Three-valued logic
| Expression | Result |
|---|---|
NULL = NULL | UNKNOWN |
NULL <> 'MI' | UNKNOWN |
NULL > 50 | UNKNOWN |
NULL IS NULL | TRUE |
'MI' IS NULL | FALSE |
NULL IS NOT NULL | FALSE |
Syntax
sql
SELECT column_list
FROM table_name
WHERE column_name IS NULL;
SELECT column_list
FROM table_name
WHERE column_name IS NOT NULL;Example — players with no team
sql
SELECT name, team
FROM player_profiles
WHERE team IS NULL;| name | team |
|---|---|
| David Warner | NULL |
Compare with WHERE team = NULL, which returns 0 rows.
Example — players who do have a team
sql
SELECT name, team
FROM player_profiles
WHERE team IS NOT NULL;| name | team |
|---|---|
| Virat Kohli | RCB |
| Rohit Sharma | MI |
| Jos Buttler | RR |
| MS Dhoni | CSK |
| Jasprit Bumrah | MI |
Example — NULL hides from other filters
The innings table has 12 rows. MS Dhoni's 31 March score is NULL.
sql
SELECT player, runs
FROM innings
WHERE runs <> 0;This returns 10 rows, not 11: Rohit's 0 is removed (correct), but Dhoni's NULL innings is also removed, because NULL <> 0 is UNKNOWN. To keep it:
sql
SELECT player, runs
FROM innings
WHERE runs <> 0
OR runs IS NULL; -- 11 rowsCommon mistakes
- ❌
WHERE team = NULL/WHERE team <> NULL→ always 0 rows. ✅IS NULL/IS NOT NULL. - ❌
WHERE team = 'NULL'→ searches for the 4-letter text "NULL". ✅IS NULL. - ❌ Treating NULL as 0 or
''. An empty string is a real (known) value; NULL is unknown. - ❌
WHERE team IS NOT 'MI'→ invalid; IS only works with NULL. ✅team <> 'MI'.
Note: SQL Server has a legacy setting
SET ANSI_NULLS OFFthat makes= NULLwork. It is deprecated — always writeIS NULL. To replace NULLs in the output, useISNULL(col, value)orCOALESCE(col, value)(covered in the functions chapter).
Interview tip
Q: Why doesn't WHERE col = NULL work?
A: NULL means unknown, so comparing anything to it with = yields UNKNOWN, not TRUE. WHERE keeps only TRUE rows. IS NULL is the only correct test.
Key points to remember
- NULL means unknown / missing — it is not 0, not '' and not the text 'NULL'.
- Any comparison with NULL using =, <>, >, < or IN gives UNKNOWN, so the row is dropped.
- IS NULL finds missing values; IS NOT NULL finds present values.
- IS NULL / IS NOT NULL always return TRUE or FALSE, never UNKNOWN.
- WHERE col = NULL always returns 0 rows — a classic beginner bug.
- Filters like runs <> 0 silently drop NULL rows; add OR col IS NULL if you need them.
Try it yourself
IS NULL vs = NULL
Runs on a fresh IPL practice database in your browser · edit the query and press Ctrl + Enter
IS NOT NULL
Runs on a fresh IPL practice database in your browser · edit the query and press Ctrl + Enter
NULL hides from <>
Runs on a fresh IPL practice database in your browser · edit the query and press Ctrl + Enter
Practice: missing scores
Runs on a fresh IPL practice database in your browser · edit the query and press Ctrl + Enter
Revision Flashcards
Tap a card to flip it and see the answer.
