Handling NULL Values
IS NULL, IS NOT NULL, COALESCE, ISNULL
✅ What You Will Learn
NULL in SQL means "no value" or "unknown". It is not zero, not an empty string — it represents the complete absence of data. NULLs are common in real-world datasets and handling them correctly is a core analyst skill.
Comparing NULL with = will not work — you must use IS NULL. And NULLs in calculations produce NULL results, which can silently break reports.
📋 customers table — some rows have NULL in the discount column
| customer_id | customer_name | city | discount |
|---|---|---|---|
| 1 | Rahul Sharma | Delhi | 500 |
| 2 | Priya Verma | Noida | NULL |
| 3 | Amit Kumar | Gurgaon | 200 |
| 4 | Sneha Kapoor | Delhi | NULL |
| 5 | Vikram Singh | Noida | 150 |
Syntax
Examples
📌 Key Points to Remember
- ✓NULL is not zero and not empty string — it means no value
- ✓Use IS NULL and IS NOT NULL — never = NULL
- ✓NULLs propagate in calculations: NULL + 5 = NULL
- ✓COALESCE works across all databases — prefer it over ISNULL or IFNULL
- ✓COUNT(*) includes NULLs; COUNT(column) does not
🏢 Real-World Application
NULL handling is one of the most practical skills for a data analyst working with real-world data. Databases always have missing data — a customer who did not provide their phone number, an order with no discount, a survey response left blank. Knowing how to handle NULLs prevents silent errors in reports. COALESCE(discount, 0) ensures a SUM of discounts does not return NULL when one customer has no discount. IS NULL is used to find incomplete records: WHERE email IS NULL finds all customers missing email addresses — essential for data quality checks.
⚠️ Common Mistakes to Avoid
❓ Frequently Asked Questions
What is NULL in SQL?
NULL represents a missing or unknown value. It is not zero, not empty string, not false — it is the absence of any value. A column is NULL when no data was entered for that field.
How do I check for NULL in SQL?
Use IS NULL or IS NOT NULL. WHERE phone IS NULL finds rows with no phone number. WHERE phone IS NOT NULL finds rows that have a phone number. Never use = NULL or != NULL — they always return no results.
What does COALESCE do in SQL?
COALESCE returns the first non-NULL value in a list. COALESCE(discount, 0) returns the discount if it exists, or 0 if it is NULL. COALESCE(col1, col2, col3, 'default') returns the first non-NULL among the three columns, or 'default' if all are NULL.
What is NULLIF in SQL?
NULLIF(a, b) returns NULL if a equals b, otherwise returns a. It is useful for avoiding division-by-zero errors: amount / NULLIF(quantity, 0) returns NULL instead of an error when quantity is 0.
How do NULLs affect aggregate functions?
COUNT(*) counts NULLs. COUNT(col) ignores NULLs. SUM, AVG, MIN, MAX all ignore NULLs. So AVG of [10, 20, NULL] is 15 (not 10). Be aware of this when NULLs represent zero rather than missing data.
✏️ Practice Exercise
Write a query that shows each customer's name and discount amount — replace any NULL discount with 0 and label that column "applied_discount".
Learn SQL with Live Trainer Guidance
These tutorials give you the theory. Our live SQL course at EVIKA Academy, Noida teaches you to apply SQL on real company datasets — with a trainer who uses it daily at MakeMyTrip.