Why does comparing to NULL in SQL never work the way you expect?
IS NULL vs equals NULL in SQL - When to Use Which
Imagine you have a list of customer records in a spreadsheet. Some customers have missing phone numbers, but you want to find all those missing entries. You try to filter by typing "phone = NULL" in the search box, but nothing shows up.
Using "equals NULL" in SQL doesn't work because NULL means 'unknown' or 'missing' data, not a value you can compare directly. This makes queries return no results or wrong results, causing confusion and wasted time.
The IS NULL condition in SQL is designed to check for missing or unknown values properly. It tells the database to look for entries where the data is absent, making your queries accurate and reliable.
SELECT * FROM customers WHERE phone = NULL;
SELECT * FROM customers WHERE phone IS NULL;
Using IS NULL lets you correctly find and handle missing data, making your database queries trustworthy and effective.
A company wants to send promotional texts but only to customers with phone numbers. Using IS NULL, they can easily exclude customers without phone numbers and avoid sending messages to empty contacts.
NULL means missing or unknown data, not a value.
Using = NULL in SQL does not work as expected.
IS NULL is the correct way to check for missing data.
Practice
age has missing (NULL) values?Solution
Step 1: Understand NULL in SQL
NULL means unknown or missing data, and it cannot be compared with = or != operators.Step 2: Use IS NULL to check for missing values
The correct way to check for NULL is usingIS NULL, which returns true for missing values.Final Answer:
age IS NULL-> Option AQuick Check:
Use IS NULL to check missing values [OK]
- Using = NULL returns no rows
- Confusing IS NULL with equality operator
- Using == NULL which is invalid in SQL
salary is NULL?Solution
Step 1: Review SQL syntax for NULL checks
SQL usesIS NULLto check for NULL values; equality operators (=, ==) are invalid here.Step 2: Identify the correct syntax
OnlyWHERE salary IS NULLis valid and will run without error.Final Answer:
WHERE salary IS NULL -> Option AQuick Check:
IS NULL is correct syntax for NULL checks [OK]
- Using = NULL causes syntax errors or no matches
- Using == NULL is invalid SQL
- Using != NULL does not find NULLs
employees with column manager_id that can be NULL, what will this query return?SELECT * FROM employees WHERE manager_id = NULL;
Solution
Step 1: Understand comparison with NULL using = operator
In SQL,= NULLdoes not match any rows because NULL is unknown and cannot be compared with =.Step 2: Result of the query
The query returns zero rows because no value equals NULL using = operator.Final Answer:
No rows -> Option CQuick Check:
= NULL returns no rows [OK]
- Expecting = NULL to find NULL values
- Confusing NULL with empty string or zero
- Assuming syntax error instead of empty result
email:SELECT * FROM users WHERE email = NULL;
But it returns no rows. What is the fix?
Solution
Step 1: Identify incorrect NULL comparison
Using= NULLdoes not find NULL values because NULL is not equal to anything.Step 2: Correct NULL check syntax
UseIS NULLto properly find rows whereemailis missing.Final Answer:
Change to email IS NULL -> Option DQuick Check:
Use IS NULL to find missing values [OK]
- Using != NULL expecting to find NULLs
- Using string 'NULL' instead of NULL keyword
- Using IS NOT NULL which finds non-missing values
SELECT * FROM customers WHERE ???
Solution
Step 1: Understand NULL and empty string difference
NULL means missing data, empty string means data is present but empty.Step 2: Combine conditions correctly
Usephone IS NULLto find missing phone numbers andphone = ''to find empty strings, combined with OR to get either case.Final Answer:
phone IS NULL OR phone = '' -> Option BQuick Check:
Use IS NULL for missing, = '' for empty [OK]
- Using = NULL which never matches
- Using AND instead of OR excludes all rows
- Confusing empty string with NULL
Ready to prove it?
Pass the test and this pattern moves unknown
