Why does checking for 'nothing' in your data never work the way you expect?
Why equals NULL fails in SQL - The Real Reasons
Imagine you have a list of friends and you want to find those whose phone number you don't know yet. You try to check if their phone number is equal to nothing, but it doesn't work as expected.
Using the usual equals sign (=) to check for unknown or missing values (NULL) doesn't work because NULL means 'unknown'. Comparing anything to unknown with = always fails or returns no results, making your search frustrating and incomplete.
SQL provides a special way to check for NULL values using IS NULL or IS NOT NULL. This lets you correctly find records where data is missing or unknown, solving the problem elegantly.
SELECT * FROM friends WHERE phone = NULL;
SELECT * FROM friends WHERE phone IS NULL;
This concept allows you to accurately find and handle missing or unknown data in your database queries.
When managing customer records, you can find all customers who haven't provided their email address yet by checking if the email field IS NULL.
NULL means unknown, not just empty or zero.
Using = NULL doesn't work because comparisons with unknown always fail.
Use IS NULL or IS NOT NULL to check for missing data correctly.
Practice
column = NULL always fail to find rows with NULL values?Solution
Step 1: Understand NULL meaning in SQL
NULL represents an unknown or missing value, not a specific value.Step 2: Explain comparison behavior with NULL
Comparing anything to NULL with = or != always returns false because NULL is unknown.Final Answer:
Because NULL means unknown and cannot be compared with = or != operators -> Option AQuick Check:
NULL comparison with = fails [OK]
- Using = NULL to find NULL values
- Assuming NULL equals zero
- Trying to quote NULL as a string
age column?Solution
Step 1: Review correct syntax to check NULL
SQL uses IS NULL to test if a column has NULL values.Step 2: Analyze each option
WHERE age = NULL and C use = or != with NULL which always fail; WHERE age = 'NULL' treats NULL as a string which is incorrect.Final Answer:
WHERE age IS NULL -> Option CQuick Check:
Use IS NULL for NULL checks [OK]
- Using = NULL or != NULL in WHERE clause
- Quoting NULL as a string
- Confusing NULL with empty string
users with a column email that contains some NULL values, what will this query return?SELECT * FROM users WHERE email = NULL;
Solution
Step 1: Understand the effect of = NULL in WHERE clause
Comparing with = NULL always yields unknown (false), so no rows match.Step 2: Predict query result
The query returns zero rows because no row satisfies email = NULL.Final Answer:
No rows at all -> Option DQuick Check:
email = NULL returns no rows [OK]
- Expecting rows with NULL to be returned
- Thinking = NULL is valid syntax
- Confusing NULL with empty string
salary:SELECT * FROM employees WHERE salary = NULL;
But it returns no rows. What is the fix?
Solution
Step 1: Identify why salary = NULL fails
Comparing with = NULL always returns false, so no rows match.Step 2: Use correct syntax to check NULL
Use IS NULL to find rows where salary is NULL.Final Answer:
Change to WHERE salary IS NULL -> Option BQuick Check:
Use IS NULL, not = NULL [OK]
- Using = NULL instead of IS NULL
- Comparing NULL to zero
- Quoting NULL as string
orders where shipped_date is either NULL or equals '2024-01-01'. Which WHERE clause is correct?Solution
Step 1: Understand NULL comparison rules
Use IS NULL to check for NULL values, not = NULL or != NULL.Step 2: Analyze the WHERE clause options
WHERE shipped_date IS NULL OR shipped_date = '2024-01-01' correctly uses IS NULL and equals comparison; others misuse NULL comparisons or treat NULL as string.Final Answer:
WHERE shipped_date IS NULL OR shipped_date = '2024-01-01' -> Option AQuick Check:
Combine IS NULL with = for dates [OK]
- Using = NULL or != NULL in OR conditions
- Quoting NULL as string
- Mixing NULL checks with wrong operators
Ready to prove it?
Pass the test and this pattern moves unknown
