01 / 21
42 ? 7 ?

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.

SQL

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.

NULL
Unknown or absent value. Not stored, not comparable with =.
Empty String ''
An actual value: a string with zero length. Can be compared normally.
Zero 0
A real numeric value. Participates in math, aggregations, and filters.

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.

id score 1 85 2 NULL 3 92 4 NULL 5 78 6 90 AVG(score) 86.25 Only 4 rows counted! 2 NULLs silently skipped

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.

NULL Value
Row exists but a column has no data. Detected with IS NULL.
Missing Key
Expected row is entirely absent. Detected with LEFT JOIN ... WHERE key IS NULL.

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.

-- ✕ Wrong: never matches anything SELECT * FROM students WHERE score = NULL; -- ✓ Correct: finds all NULL scores SELECT * FROM students WHERE score IS NULL;

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.

SELECT COUNT(*) AS total_rows, SUM(CASE WHEN score IS NULL THEN 1 ELSE 0 END) AS null_count, ROUND( 100.0 * SUM(CASE WHEN score IS NULL THEN 1 ELSE 0 END) / COUNT(*), 2 ) AS null_rate_pct FROM students;

Shortcut: COUNT(*) vs COUNT(col)

You can compute the NULL rate more concisely by exploiting the difference between COUNT(*) and COUNT(column_name).

SELECT ROUND( 100.0 * (1 - COUNT(score) * 1.0 / COUNT(*) ), 2 ) AS null_rate_pct FROM students;
COUNT(*) 6 All rows, including NULLs COUNT(score) 4 Only non-NULL values

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.

-- Find customers with no orders SELECT c.customer_id, c.name FROM customers c LEFT JOIN orders o ON c.customer_id = o.customer_id WHERE o.customer_id IS NULL;

Computing the Missing Key Rate

SELECT COUNT(*) AS total_customers, SUM(CASE WHEN o.customer_id IS NULL THEN 1 ELSE 0 END) AS missing, ROUND( 100.0 * SUM(CASE WHEN o.customer_id IS NULL THEN 1 ELSE 0 END) / COUNT(*), 2 ) AS missing_rate_pct FROM customers c LEFT JOIN orders o ON c.customer_id = o.customer_id;

How to Apply It: Step by Step

1
Identify target columns. List the columns that should never (or rarely) be NULL.
2
Count total rows. Use COUNT(*) to get your denominator.
3
Count NULLs per column. Use SUM(CASE WHEN col IS NULL THEN 1 ELSE 0 END) or the COUNT shortcut.
4
Compute rate. Divide NULL count by total rows and multiply by 100.
5
Check keys across tables. Use LEFT JOIN + IS NULL to find orphaned or missing keys.

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.

SELECT COUNT(*) AS total, ROUND(100.0 * (1 - COUNT(email) * 1.0 / COUNT(*)), 2) AS email_null_pct, ROUND(100.0 * (1 - COUNT(phone) * 1.0 / COUNT(*)), 2) AS phone_null_pct FROM employees;

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.

SELECT 'name' AS col, ROUND(100.0*SUM(CASE WHEN name IS NULL THEN 1 ELSE 0 END)/COUNT(*),2) AS null_pct FROM employees UNION ALL SELECT 'email', ROUND(100.0*SUM(CASE WHEN email IS NULL THEN 1 ELSE 0 END)/COUNT(*),2) FROM employees UNION ALL SELECT 'phone', ROUND(100.0*SUM(CASE WHEN phone IS NULL THEN 1 ELSE 0 END)/COUNT(*),2) FROM employees;

Example 3: Missing Key Detection

Find products in your inventory that have never been sold (no matching key in sales), plus the missing rate.

SELECT COUNT(*) AS total_products, SUM(CASE WHEN s.product_id IS NULL THEN 1 ELSE 0 END) AS never_sold, ROUND( 100.0 * SUM(CASE WHEN s.product_id IS NULL THEN 1 ELSE 0 END) / COUNT(*), 2 ) AS missing_pct FROM inventory i LEFT JOIN (SELECT DISTINCT product_id FROM sales) s ON i.product_id = s.product_id;

Common Mistakes

✕ Using = NULL
Writing WHERE col = NULL always returns zero rows. Use IS NULL instead.
✕ COUNT(col) as total
Using COUNT(col) as denominator excludes NULLs, making the rate appear as zero.
✕ Forgetting DISTINCT
Without DISTINCT in key checks, duplicate matches inflate counts and hide missing keys.

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?