Learn why NULL represents missing/unknown data (not zero or empty string), why `= NULL` fails silently, how `NULL` affects `AVG` and `NOT IN`, and how `COALESCE` and `NULLIF` protect production pipelines.
Suppose you hold a sealed envelope labeled "Monthly Bonus". You do not know what number is inside.
$0? No — you might have received $500 or $0, it is simply *unknown*.TRUE or FALSE — you have to answer `UNKNOWN`.That is exactly how SQL treats `NULL`. NULL is not zero (`0`), and NULL is not a blank text string (`''`). NULL means *missing or unknown*.
NULL means *unknown*, writing WHERE email = NULL or WHERE email != NULL ALWAYS evaluates to UNKNOWN and returns zero rows. Always use IS NULL and IS NOT NULL.| Concept / Function | Plain-English Meaning | Example |
|---|---|---|
| `NULL` | Absence of a known value (not 0, not ''). | Phone number was never collected at signup. |
| Three-Valued Logic (3VL) | SQL expressions evaluate to TRUE, FALSE, or UNKNOWN. | NULL = NULL returns UNKNOWN, not TRUE! |
| `IS NULL` / `IS NOT NULL` | The ONLY standard operators that check whether a field is missing. | WHERE discount_code IS NULL |
| `COALESCE(a, b, c)` | Returns the first non-NULL value from left to right. | COALESCE(mobile_phone, home_phone, 'None') |
| `NULLIF(a, b)` | Returns NULL if a = b, otherwise returns a. Prevents / 0 crashes! | revenue / NULLIF(orders, 0) |
COUNT(*) vs COUNT(col) | COUNT(*) counts every row; COUNT(col) skips rows where col is NULL. | Used carefully to avoid understated denominators. |
Consider a miniature `sales_rep_bonuses` table:
| rep_id | rep_name | base_salary | commission | deals_closed |
|---|---|---|---|---|
| 1 | Maya | 60000 | 5000 | 10 |
| 2 | Liam | 60000 | 0 | 2 |
| 3 | Noor | 60000 | NULL | 0 |
60000 + 5000 = 6500060000 + 0 = 60000Any math operation touching a NULL turns the entire answer into NULL! Wrap nullable numbers with `COALESCE(commission, 0)`:
SELECT
rep_name,
base_salary + COALESCE(commission, 0) AS total_pay
FROM sales_rep_bonuses;Fix it permanently with NULLIF(deals_closed, 0):
SELECT
rep_name,
ROUND(COALESCE(commission, 0) * 1.0 / NULLIF(deals_closed, 0), 2) AS avg_commission_per_deal
FROM sales_rep_bonuses;Look how clean that works:
- For Maya (10): NULLIF(10, 0) returns 10 -> 5000.0 / 10 = 500.00.
- For Noor (0): NULLIF(0, 0) returns NULL, and 0 / NULL = NULL (no crash!). Combine with outer COALESCE(..., 0) if dashboards require 0.
Both numbers are mathematically valid, depending on whether Noor truly earned $0 or had unrecorded data — which is why analysts MUST inspect NULL behavior before writing KPIs.
This is one of the deadliest SQL bugs in production pipelines:
-- Suppose blocked_users contains: (101, 102, NULL)
SELECT user_id
FROM active_users
WHERE user_id NOT IN (SELECT user_id FROM blocked_users);What happens? It returns 0 rows! Why? Because user_id NOT IN (101, 102, NULL) expands to:
user_id != 101 AND user_id != 102 AND user_id != NULL -> since user_id != NULL is UNKNOWN, the whole AND expression becomes UNKNOWN and filters out every user!
Two Safe Fixes: 1. Use `NOT EXISTS` (immune to NULLs!):
SELECT a.user_id
FROM active_users a
WHERE NOT EXISTS (
SELECT 1 FROM blocked_users b WHERE b.user_id = a.user_id
);WHERE user_id IS NOT NULL), or use a LEFT JOIN ... WHERE b.user_id IS NULL.| Broken Pattern | Why It Fails | Clean Production Pattern |
|---|---|---|
WHERE status != 'CANCELLED' | Silently drops rows where status IS NULL! | WHERE COALESCE(status, 'ACTIVE') != 'CANCELLED' or WHERE status IS DISTINCT FROM 'CANCELLED' |
a / b | Crashes query when b = 0. | a / NULLIF(b, 0) |
COALESCE(col, 'N/A') inside numeric column | Mixing text 'N/A' and numbers breaks schema/types. | Keep numeric columns numeric (COALESCE(col, 0)), format 'N/A' only in UI layer. |
WHERE col = NULL | Always returns 0 rows. | WHERE col IS NULL |
= or !=.COUNT(*) vs COUNT(col)**: aggregates ignore NULL!NOT IN (subquery) if the subquery might return a single NULL — prefer `NOT EXISTS`.Test your understanding of the concepts covered in this lesson.
Query bronze.contacts and return contact_id, full_name, and a cleaned column contact_email. If email is NULL or blank/spaces only, replace it with 'no-email@on-file.invalid'. Otherwise, lowercase and trim the email. Order by contact_id ASC.