Checking NULL Rates
& Missing Keys
Learn how to detect, measure, and handle NULL values in your data. NULLs hide silently in databases: finding them is the first step to clean, reliable queries.
What is NULL in SQL?
In SQL, NULL is not zero, not an empty string, and not false. It means "unknown" or "missing": the value simply does not exist in that cell. Any operation involving NULL produces NULL, and any comparison with NULL returns UNKNOWN, not TRUE or FALSE.
Why NULLs Matter
NULLs silently distort your results. Aggregations like AVG, SUM, and COUNT(column) skip NULLs entirely, which can bias calculations. JOINs on NULL keys fail to match, causing lost rows. Understanding NULL rates is the foundation of data quality.
NULL Values vs Missing Keys
A NULL value means a column exists but its content is absent. A missing key means an entire row you expected to find is gone: for example, a customer ID that should appear in the orders table but doesn't.
The IS NULL Operator
You cannot use = NULL to find NULLs because any comparison with NULL returns UNKNOWN. SQL provides IS NULL and IS NOT NULL as the only correct way to test for missing values.
Computing the NULL Rate
The NULL rate for a column is the fraction of rows where that column is NULL. This single metric tells you how much data is missing.
Shortcut: COUNT(*) vs COUNT(col)
You can compute the NULL rate more concisely by exploiting the difference between COUNT(*) and COUNT(column_name).
Key Distinction: Three Patterns
| COUNT(*) | COUNT(col) | SUM(CASE...) | |
|---|---|---|---|
| Counts | All rows | Non-NULL rows | NULL rows |
| Includes NULLs? | Yes | No | Yes (only) |
| Use for | Total denominator | Valid data count | Direct NULL count |
| NULL rate? | Denominator | Numerator (inverted) | Direct numerator |
Finding Missing Keys
A missing key is a value that should exist in one table but doesn't appear in another. For example: a customer in the customers table with no matching rows in orders.
Computing the Missing Key Rate
How to Apply It: Step by Step
Example 1: Single Table NULL Audit
You have an employees table with 500 rows. Find the NULL rate for email and phone columns in one query.
Example 2: Multi-Column NULL Profiling
Profile every column in a table using a single query. This is the standard data profiling pattern used by analysts before building any report.
Example 3: Missing Key Detection
Find products in your inventory that have never been sold (no matching key in sales), plus the missing rate.
Common Mistakes
Summary: Patterns at a Glance
| Goal | SQL Pattern | Key Operator |
|---|---|---|
| Find NULL rows | WHERE col IS NULL | IS NULL |
| Count NULLs | SUM(CASE WHEN ... IS NULL) | CASE |
| NULL rate % | \(1 - \frac{\text{COUNT(col)}}{\text{COUNT(*)}}\) | COUNT |
| Missing keys | LEFT JOIN ... WHERE IS NULL | LEFT JOIN |
| Missing key rate | SUM(CASE)/COUNT(*) on LEFT JOIN | ROUND |
Question 1 of 5
Which SQL expression correctly tests whether a column email contains a NULL value?
Question 2 of 5
A table has 1000 rows. COUNT(phone) returns 750. What is the NULL rate for the phone column?
Question 3 of 5
To find customers who have never placed an order, which JOIN type is most appropriate?
Question 4 of 5
What does COUNT(*) do differently from COUNT(column_name)?
Question 5 of 5
When checking for missing keys with a LEFT JOIN, why is it important to use SELECT DISTINCT on the right-side key before joining?