All 40 lessons
Lesson 7 of 40 · SQL Basics
NULL in SQL
By Skillancy Editorial Team
Every example runs on the ShopKart dataset, already loaded in the free SQL Compiler. Click Try it in the SQL Compiler under a query to open it there, then press Run.
NULL means "no value" or "unknown". A customer with no phone number on file has NULL in the phone column. NULL is not zero and not an empty string, and it behaves differently from every other value, which is why it causes so many subtle bugs.
Syntax
WHERE column IS NULL
WHERE column IS NOT NULL
COALESCE(value1, value2, ...) -- first non-NULL value
NULLIF(a, b) -- NULL if a = b, otherwise aWhy = NULL doesn't work
Any comparison with NULL gives unknown, not true. So this returns no rows, even though some customers have no phone:
SELECT customer_name
FROM customers
WHERE phone = NULL;Try it in the SQL CompilerResult:
(0 rows)
Examples
1. Find missing values with IS NULL
SELECT customer_name, phone
FROM customers
WHERE phone IS NULL;Try it in the SQL CompilerResult:
| customer_name | phone |
|---|---|
| Rohan Gupta | NULL |
| Karan Mehta | NULL |
(2 rows)
2. Find present values with IS NOT NULL
Orders that have been delivered (have a delivery date):
SELECT order_id, order_date, delivery_date
FROM orders
WHERE delivery_date IS NOT NULL
LIMIT 5;Try it in the SQL CompilerResult:
| order_id | order_date | delivery_date |
|---|---|---|
| 1001 | 2025-01-05 | 2025-01-08 |
| 1002 | 2025-01-18 | 2025-01-23 |
| 1003 | 2025-02-02 | 2025-02-05 |
| 1005 | 2025-03-11 | 2025-03-14 |
| 1006 | 2025-03-29 | 2025-04-02 |
(5 rows)
3. Replace NULL with COALESCE
COALESCE returns the first value that isn't NULL. Show a friendly label instead of an empty cell:
SELECT customer_name,
COALESCE(phone, 'Not provided') AS phone
FROM customers
LIMIT 4;Try it in the SQL CompilerResult:
| customer_name | phone |
|---|---|
| Aarav Sharma | 9876543210 |
| Priya Nair | 9845012345 |
| Rohan Gupta | Not provided |
| Sneha Reddy | 9988776655 |
(4 rows)
With more arguments, it works as a fallback chain: COALESCE(phone, email, 'No contact').
SELECT customer_name,
COALESCE(phone, email, 'No contact') AS best_contact
FROM customers
WHERE phone IS NULL OR email IS NULL;Try it in the SQL CompilerResult:
| customer_name | best_contact |
|---|---|
| Rohan Gupta | rohan.g@outlook.com |
| Karan Mehta | karan.mehta@rediffmail.com |
| Divya Menon | 9447001122 |
(3 rows)
4. NULL in arithmetic
Anything combined with NULL becomes NULL. Here, days to deliver is NULL for orders without a delivery date:
SELECT order_id, status,
delivery_date - order_date AS days_to_deliver
FROM orders
WHERE order_id BETWEEN 1018 AND 1022;Try it in the SQL CompilerResult:
| order_id | status | days_to_deliver |
|---|---|---|
| 1018 | Delivered | 5 |
| 1019 | Delivered | 2 |
| 1020 | Shipped | NULL |
| 1021 | Delivered | 3 |
| 1022 | Shipped | NULL |
(5 rows)
5. NULL in aggregates
COUNT(*) counts rows; COUNT(column) counts non-NULL values:
SELECT COUNT(*) AS customers,
COUNT(phone) AS with_phone,
COUNT(email) AS with_email
FROM customers;Try it in the SQL CompilerResult:
| customers | with_phone | with_email |
|---|---|---|
| 12 | 10 | 11 |
(1 row)
6. Avoid divide-by-zero with NULLIF
NULLIF(a, b) returns NULL when a = b. Dividing by NULL gives NULL instead of an error, which makes it the standard guard against division by zero:
SELECT 100 / NULLIF(0, 0) AS safe_division;Try it in the SQL CompilerResult:
| safe_division |
|---|
| NULL |
(1 row)
Without NULLIF, 100 / 0 stops the query with a "division by zero" error.
Common mistakes
= NULLor<> NULLinstead ofIS NULL/IS NOT NULL.- Forgetting NULLs in
<>filters.WHERE brand <> 'Nalli'silently drops products with a NULL brand. AddOR brand IS NULLif you need them. - Averaging with missing values.
AVGskips NULLs. If NULL should mean zero, useAVG(COALESCE(col, 0)).
Practice question
List every product's name and brand, showing "Unbranded" when the brand is missing.
Show solution
SELECT product_name,
COALESCE(brand, 'Unbranded') AS brand
FROM products
WHERE product_id >= 112;Try it in the SQL CompilerResult:
| product_name | brand |
|---|---|
| The Psychology of Money | Jaico |
| Wings of Fire | Universities Press |
| Yoga Mat 6mm | Boldfit |
| Cricket Bat (Kashmir Willow) | Unbranded |
(4 rows)
Key takeaways
- NULL means unknown; it is not 0 or
''. - Test for it with
IS NULL/IS NOT NULL, never=. COALESCEreplaces NULL with a fallback value.NULLIFturns a value into NULL, which is handy for avoiding division by zero.
Frequently asked questions
Is NULL the same as zero or an empty string?
No. NULL means the value is unknown or missing. Zero is a known number and '' is a known (empty) piece of text. In PostgreSQL, NULL, 0 and '' are three different things.
Do aggregate functions ignore NULL?
Yes. SUM, AVG, MIN, MAX and COUNT(column) skip NULLs. COUNT(*) counts every row regardless.
What is the difference between COALESCE and IFNULL?
COALESCE is standard SQL and accepts any number of arguments. IFNULL (MySQL) and ISNULL (SQL Server) accept exactly two. Prefer COALESCE for portable code.
Keep learning SQL
One email a week with SQL and analytics guides, new practice questions and cohort dates. No spam, unsubscribe anytime.
Prefer a one-page summary? Download the free SQL cheat sheet (PDF)