NULL Handling in SQL: IS NULL vs IS NOT NULL
π‘ Concept Name
NULL Handling in SQL refers to managing unknown or missing values using operators like IS NULL
and IS NOT NULL
, since direct comparisons like = NULL
do not work.
π Quick Intro
NULL represents the absence of a value in SQL. Since NULL is not a value but a placeholder for unknown data, SQL provides specific operators like IS NULL
and IS NOT NULL
to test and filter these rows correctly.
π§ Analogy / Short Story
Imagine a list of attendees at an event. Some entries have a blank for 'Phone Number'. You can't say ""Phone Number = Blank""βbecause blank is not a value. Instead, you check if ""Phone Number is missing"". Thatβs how SQL handles NULLs: not by comparing directly, but by checking their existence with IS NULL
.
π§ Technical Explanation
- π§© NULL indicates unknown or missing dataβnot zero or empty string.
- β οΈ Expressions like
= NULL
or<> NULL
always return false or unknown. - β
Use
IS NULL
to check for NULL values. - β
Use
IS NOT NULL
to exclude NULLs. - π NULLs can affect joins, filters, and aggregations unless handled explicitly.
π― Purpose & Use Case
- β Filtering rows where a column is missing a value.
- β Identifying incomplete or dirty data during audits.
- β Preventing incorrect results in conditional WHERE clauses.
- β Supporting optional fields in forms and user input.
π» Real Code Example
-- Select customers without phone numbers
SELECT Name, Phone
FROM Customers
WHERE Phone IS NULL;
-- Select only customers with phone numbers
SELECT Name, Phone
FROM Customers
WHERE Phone IS NOT NULL;

β Interview Q&A
Q1: What does NULL mean in SQL?
A: NULL represents missing or unknown dataβitβs not the same as 0 or an empty string.
Q2: Can you use = NULL to check for NULLs?
A: No, use IS NULL
instead. = NULL
always returns UNKNOWN.
Q3: How do you exclude NULL values in a query?
A: Use IS NOT NULL
in your WHERE clause.
Q4: Do NULLs affect JOIN results?
A: Yes, especially when matching foreign keysβuse IS NULL
or OUTER JOINs carefully.
Q5: What does COUNT(*) include?
A: All rowsβincluding those with NULLs. But COUNT(column)
excludes NULLs.
Q6: Is NULL equal to NULL in SQL?
A: No, NULL = NULL
is unknown. Use IS NULL
to compare.
Q7: How do NULLs behave in GROUP BY?
A: NULLs are grouped together in a single group like any other value.Q8: Whatβs the output of WHERE column <> NULL
?
Q9: Can you sort columns with NULLs?
A: Yes, NULLs can appear first or last depending on DBMS and ORDER BY settings.
Q10: What is the difference between NULL and an empty string?
A: NULL means unknown or not applicable. An empty string means known, but contains nothing.
π MCQs
Q1. What keyword is used to check for NULLs in SQL?
- == NULL
- NULL
- IS NULL
- = NULL
Q2. Which operator excludes NULL values?
- IS NULL
- = NOT NULL
- != NULL
- IS NOT NULL
Q3. What does NULL mean in SQL?
- Zero
- Empty string
- Missing or unknown value
- Error
Q4. What is the result of NULL = NULL?
- True
- False
- Error
- Unknown
Q5. What does COUNT(column) exclude?
- Zero values
- All rows
- NULL values
- Empty strings
Q6. Which clause helps identify incomplete data?
- GROUP BY
- ORDER BY
- IS NULL
- HAVING
Q7. Is NULL equal to an empty string?
- Yes
- Only in MySQL
- No
- Depends on index
Q8. How to fetch rows where column has value?
- WHERE column != NULL
- WHERE column > NULL
- WHERE column IS NOT NULL
- WHERE NOT NULL column
Q9. Which of these returns false when NULL is involved?
- column = NULL
- column IS NULL
- column IS NOT NULL
- column IS NULL OR NOT NULL
Q10. Which clause affects how NULLs appear in sorted results?
- GROUP BY
- HAVING
- ORDER BY
- WHERE
π‘ Bonus Insight
Use COALESCE()
or IFNULL()
to handle NULLs gracefully by replacing them with fallback values. Also, avoid using = or <> for NULL checksβit leads to logic errors and unexpected results.
π PDF Download
Need a handy summary for your notes? Download this topic as a PDF!