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 a

Why = 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 Compiler

Result:

(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 Compiler

Result:

customer_namephone
Rohan GuptaNULL
Karan MehtaNULL

(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 Compiler

Result:

order_idorder_datedelivery_date
10012025-01-052025-01-08
10022025-01-182025-01-23
10032025-02-022025-02-05
10052025-03-112025-03-14
10062025-03-292025-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 Compiler

Result:

customer_namephone
Aarav Sharma9876543210
Priya Nair9845012345
Rohan GuptaNot provided
Sneha Reddy9988776655

(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 Compiler

Result:

customer_namebest_contact
Rohan Guptarohan.g@outlook.com
Karan Mehtakaran.mehta@rediffmail.com
Divya Menon9447001122

(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 Compiler

Result:

order_idstatusdays_to_deliver
1018Delivered5
1019Delivered2
1020ShippedNULL
1021Delivered3
1022ShippedNULL

(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 Compiler

Result:

customerswith_phonewith_email
121011

(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 Compiler

Result:

safe_division
NULL

(1 row)

Without NULLIF, 100 / 0 stops the query with a "division by zero" error.

Common mistakes

  • = NULL or <> NULL instead of IS NULL / IS NOT NULL.
  • Forgetting NULLs in <> filters. WHERE brand <> 'Nalli' silently drops products with a NULL brand. Add OR brand IS NULL if you need them.
  • Averaging with missing values. AVG skips NULLs. If NULL should mean zero, use AVG(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 Compiler

Result:

product_namebrand
The Psychology of MoneyJaico
Wings of FireUniversities Press
Yoga Mat 6mmBoldfit
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 =.
  • COALESCE replaces NULL with a fallback value.
  • NULLIF turns 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.

Newsletter

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)