2.2 Mastering NULL, Three-Valued Logic & COALESCE
Master the #1 source of silent SQL bugs: NULL vs 0 vs empty string, Three-Valued Logic truth tables, COALESCE vs IFNULL, NULLIF division-by-zero protection, and how NULL affects COUNT and NOT IN.
1. What NULL Actually Represents
Beginners often confuse NULL, 0, and '' (empty string). They are completely different things in a database:
| Value | Meaning | Real-World Example (discount or phone) |
|---|---|---|
0 | Known numeric value of zero | Customer explicitly received ₹0 discount |
'' | Known string of length 0 | User explicitly saved an empty middle name |
NULL | Unknown / Missing / Not Applicable | Customer hasn't entered a phone number yet |
Because you cannot say whether one unknown value equals another unknown value, SQL uses Three-Valued Logic: every expression evaluates to TRUE, FALSE, or UNKNOWN.
2. Three-Valued Logic Truth Table (Memorize This!)
| Expression | Result | Why? |
|---|---|---|
NULL = NULL | UNKNOWN | Two unknown values aren't guaranteed to be equal! |
NULL != 5 | UNKNOWN | An unknown value might be 5! |
TRUE AND UNKNOWN | UNKNOWN | Still depends on the unknown side |
FALSE AND UNKNOWN | FALSE | FALSE AND anything is always FALSE |
TRUE OR UNKNOWN | TRUE | TRUE OR anything is always TRUE |
NULL IS NULL | TRUE | IS NULL specifically checks for the absence of a value |
-- ❌ WRONG: Returns 0 rows every single time!
SELECT name FROM customers WHERE phone = NULL;
-- ✅ RIGHT: Always use IS NULL / IS NOT NULL
SELECT name FROM customers WHERE phone IS NULL;
SELECT name FROM customers WHERE phone IS NOT NULL;
-- ✅ PRO (PostgreSQL / SQL Standard): NULL-safe equality!
-- Treats NULL as equal to NULL (returns TRUE when both are NULL)
SELECT * FROM customers WHERE referral_code IS NOT DISTINCT FROM NULL;
3. The 4 Classic NULL Traps in Interviews & Production
Trap 1: Arithmetic & String Concatenation with NULL
100 + NULL → NULL!
'Hello ' || NULL (PostgreSQL) → NULL!
Fix: Wrap nullable columns in COALESCE(a, b, ...), which returns the first non-NULL value from left to right:
SELECT
product_name,
price,
COALESCE(discount, 0) AS safe_discount,
price - COALESCE(discount, 0) AS final_price
FROM products;
COALESCE vs IFNULL / NVL (Important Difference!)
| Function | Supported In | Max Arguments | Recommendation |
|---|---|---|---|
COALESCE(a, b, c, ...) | ALL SQL databases (ANSI Standard) | Unlimited (checks left → right) | ✅ Always prefer COALESCE! |
IFNULL(a, b) | MySQL & SQLite only | Exactly 2 | Avoid — breaks in PostgreSQL/SQL Server |
NVL(a, b) | Oracle only | Exactly 2 | Avoid — Oracle-specific |
Trap 2: COUNT(*) vs COUNT(column_name)
| Query | Counts NULLs? | What It Actually Returns |
|---|---|---|
COUNT(*) | ✅ Yes | Total number of rows in the table |
COUNT(phone) | ❌ No! | Number of rows where phone IS NOT NULL |
COUNT(DISTINCT city) | ❌ No! | Number of unique non-NULL cities |
Trap 3: NOT IN with a NULL Returns ZERO Rows!
Why? Because x NOT IN (1, 2, NULL) expands to x != 1 AND x != 2 AND x != NULL → TRUE AND TRUE AND UNKNOWN → UNKNOWN → 0 rows returned!
-- 🚨 SILENT BUG if banned_users.user_id ever contains a NULL!
SELECT * FROM users
WHERE user_id NOT IN (SELECT user_id FROM banned_users);
-- ✅ SAFE FIX #1: Filter NULLs out of the subquery
SELECT * FROM users
WHERE user_id NOT IN (
SELECT user_id FROM banned_users WHERE user_id IS NOT NULL
);
-- ✅ SAFE FIX #2 (Best practice): Use NOT EXISTS (immune to NULLs!)
SELECT u.*
FROM users u
WHERE NOT EXISTS (
SELECT 1 FROM banned_users b WHERE b.user_id = u.user_id
);
Trap 4: Division by Zero → Solved with NULLIF(a, b)
NULLIF(a, b) returns NULL if a = b, otherwise returns a. Wrapping a denominator in NULLIF(col, 0) prevents division-by-zero crashes cleanly:
SELECT
campaign,
COALESCE(ROUND(revenue / NULLIF(clicks, 0), 2), 0) AS revenue_per_click
FROM ad_campaigns;
4. Watch IS NOT NULL & COALESCE Transform a Table Live
| name | phone | |
|---|---|---|
| Aarav | aarav@mail.com | 9876543210 |
| Meera | meera@mail.com | NULL |
| Kabir | NULL | 9123456789 |
5. Quick Check Your Understanding
Module 2.2 Quick Quiz
Keep Exploring: Related Lessons & Tools
All Courses →Test regular expressions live with color-coded match highlights, capture group inspection, plain-English token breakdown, find-and-replace, and Python/JS snippets.
Paste any Python, JS, Java, or C++ snippet to estimate Time & Space complexity, or enter input size N to see which algorithms pass in 1s.