Handling NULL Values (IS NULL)
Handling NULL Values: IS NULL, IS NOT NULL, and Three-Valued Logic
No concept in relational databases trips up beginners and seasoned programmers more consistently than NULL. Because NULL represents missing or unknown information, standard equality operators (= and !=) do not work as expected. To inspect, filter, and handle missing values properly, you must use IS NULL and IS NOT NULL.
1. What is NULL? (And What It Isn't)
The Fundamental Rule:
Because NULL represents an unknown, nothing can equal NULL—not even another NULL!
2. The Correct Syntax: IS NULL and IS NOT NULL
Because standard comparison operators fail on NULL, SQL provides dedicated unary operators:
3. The Dangerous Trap: WHERE column = NULL
Consider this common junior developer error:
What happens when MySQL executes this?
- 1For every row, MySQL evaluates
shipped_date = NULL. - 2Regardless of whether
shipped_datecontains'2026-03-10'orNULL, the expression evaluates toUNKNOWN! - 3Because
WHEREonly returns rows that evaluate strictly toTRUE, this query will return ZERO rows 100% of the time!
4. Sorting Behavior of NULL in MySQL
How does ORDER BY handle rows with NULL values?
- In MySQL,
NULLvalues are treated as the lowest possible values. - In
ORDER BY col ASC, rows containingNULLappear at the very beginning. - In
ORDER BY col DESC, rows containingNULLappear at the very end.
5. Replacing NULLs in Output: IFNULL() and COALESCE()
When presenting query results to users or frontend applications, displaying raw NULL values looks unpolished. You can replace NULL with friendly fallback values:
IFNULL(expression, fallback_value) (MySQL Specific):
COALESCE(val1, val2, ..., valN) (ANSI SQL Standard):
Returns the first non-null value in its parameter list:
6. Best Practices & Common Pitfalls
- Avoid Nullable Foreign Keys Where Possible: Making foreign keys nullable allows orphaned child rows with no parent entity. Only make foreign keys nullable when the relationship is genuinely optional.
- *Count Gotcha (
COUNT(col)vs `COUNT()`):** COUNT(*)counts all rows regardless of nullability.COUNT(column_name)counts only rows where that column is NOT NULL!
Multiple Choice Questions
1. Which SQL operator correctly checks if a column contains a missing or unrecorded value?
A. = NULL B. == NULL C. IS NULL D. IS EMPTY Answer: C Explanation: IS NULL is the dedicated SQL operator designed to test for the presence of NULL values.
2. What will the query SELECT * FROM employees WHERE bonus = NULL; return?
A. All employees with a bonus of zero B. All employees whose bonus is NULL C. Zero rows, because comparing any value to NULL using '=' yields UNKNOWN D. An error message Answer: C Explanation: In SQL, bonus = NULL evaluates to UNKNOWN for every row, causing the WHERE clause to discard all records.
3. How does MySQL sort NULL values when executing an ORDER BY column ASC statement?
A. NULL values are placed at the very top (treated as the lowest values) B. NULL values are placed at the very bottom C. NULL values are randomly scattered D. NULL values are omitted from the output Answer: A Explanation: In MySQL, NULL values are treated as lower than any non-NULL value, causing them to appear first in ascending (ASC) sorts.
4. Which function returns the first non-NULL value from a comma-separated list of arguments?
A. IFNULL() B. COALESCE() C. NULLIF() D. ISNULL() Answer: B Explanation: COALESCE(val1, val2, ...) is the standard SQL function that evaluates arguments sequentially and returns the first non-NULL entry.
5. What is the difference between COUNT(*) and COUNT(commission_pct) on a table with 100 rows where 20 rows have NULL commissions?
A. Both return 100 B. COUNT() returns 100, while COUNT(commission_pct) returns 80 C. Both return 80 D. COUNT(commission_pct) throws an error Answer: B Explanation: `COUNT() tallies total physical rows (100), whereas COUNT(column_name)` tallies only rows where the specified column contains a non-NULL value (80).
Sorting Data with ORDER BY
Continue learning with hands-on practice, examples, and exercises in the upcoming topic.
Related Lessons
| Previous Lesson | Next Lesson |
|---|---|
| Pattern Matching with LIKE & Wildcards | Sorting Data with ORDER BY |
Practice Quiz
Test your understanding of this lesson with 5 questions. Each question has one correct answer.