7. NULL Values: Missing Data Done Right
7.3 Why = NULL Never Works (Common Beginner Trap)
Why. = NULL is a famous trap in interviews and real bugs.
What. In SQL, NULL = NULL is not TRUE. Comparisons with NULL yield unknown. Use IS NULL.
How. Wrong: WHERE note = NULL. Right: WHERE note IS NULL.
Steps
- Run the wrong form — see empty or odd results.
- Run IS NULL — see real missing rows.
- Remember the interview one-liner.
Examples
Example 1 · dataset: raja_coffee
SELECT COUNT(*) AS wrong_equals_null
FROM orders
WHERE note = NULL;Output
| wrong_equals_null |
|---|
| 0 |
Meaning. Usually 0 — = NULL does not find NULLs.
Example 2 · dataset: raja_coffee
SELECT COUNT(*) AS right_is_null
FROM orders
WHERE note IS NULL;Output
| right_is_null |
|---|
| 22 |
Meaning. Real count of missing notes.
Ravindra Bagale's Tip
Interview line: “I use IS NULL because NULL means unknown, not a value.”
Ravindra Bagale's Tip – मराठी
Interview ओळ: “मी IS NULL वापरतो कारण NULL म्हणजे अज्ञात, मूल्य नाही.”
Ravindra Bagale's Tip – हिंदी
इंटरव्यू लाइन: “मैं IS NULL इस्तेमाल करता/करती हूँ क्योंकि NULL अज्ञात है, मान नहीं।”