Nishaglobal Education logo

NULL: working with missing values

Understand SQL NULL values, find them with IS NULL and IS NOT NULL instead of =, and replace them in results with COALESCE.

Lesson 5 of 1220 minQuiz: 4 questions

🎯 By the end of this lesson you will be able to:

  • Explain what NULL means and why `= NULL` never matches
  • Find missing values with IS NULL and IS NOT NULL
  • Show a replacement for missing values with COALESCE
Outline
  1. What you'll learn and why it matters
  2. What NULL means
  3. Finding NULLs with IS NULL
  4. Replacing NULL in results with COALESCE
  5. Where NULL surprises people
  6. Practice
  7. Common mistakes
  8. Recap
  9. Quiz
  10. Mark lesson complete

What you'll learn and why it matters

Real data always has gaps: a customer skips the email field, a delivery date is not known yet. Databases mark a missing value as NULL. NULL behaves differently from every other value, and misunderstanding it is one of the most common causes of wrong answers in SQL, so it deserves its own lesson.

What NULL means

NULL means unknown or missing. It is not zero, and it is not empty text (''). In our shop, Meera and Sara have no email, and Kofi's city is unknown.

Finding NULLs with IS NULL

Because NULL is unknown, comparing anything with = to NULL gives “unknown” rather than true, so this finds nothing:

SQL
SELECT name, email
FROM customers
WHERE email = NULL;

Result: 0 rows

Query result
nameemail
(no rows)

Always use IS NULL instead:

SQL
SELECT name, email
FROM customers
WHERE email IS NULL;

Result: 2 rows

Query result
nameemail
Meera NairNULL
Sara LopezNULL

And IS NOT NULL for rows that do have a value:

SQL
SELECT name, city
FROM customers
WHERE city IS NOT NULL AND country = 'India'
ORDER BY name;

Result: 4 rows

Query result
namecity
Arjun SinghDelhi
Asha PatelMumbai
Meera NairKochi
Ravi KumarBengaluru

Replacing NULL in results with COALESCE

COALESCE(a, b) gives a if it is not NULL, otherwise b. Use it to show something friendlier than a blank:

SQL
SELECT name, COALESCE(email, 'no email') AS contact
FROM customers
WHERE country = 'India';

Result: 4 rows

Query result
namecontact
Asha Patelasha@example.com
Ravi Kumarravi@example.com
Meera Nairno email
Arjun Singharjun@example.com

This only changes the result, not the table. You can give COALESCE more than two values; it returns the first one that is not NULL.

Where NULL surprises people

SituationWhat happensWhy
WHERE email = NULLNo rowsComparisons with NULL are unknown, never true
WHERE city <> 'Mumbai'Kofi (city NULL) is not includedNULL <> 'Mumbai' is unknown, so the row is dropped
price + NULLNULLAny arithmetic with an unknown value is unknown
COUNT(email)Counts only non-NULL emailsCovered in the next lesson

Practice

Guided exercise

Customers without a city

Show the name and country of customers whose city is missing.

  1. Select name, country from customers.
  2. Filter with city IS NULL, not = NULL.
Show the solution
Solution
SELECT name, country
FROM customers
WHERE city IS NULL;

Result: 1 row

Query result
namecountry
Kofi MensahGhana

How to get there: Only Kofi has no city. city = NULL would have returned no rows at all.

Your turn

Friendly city list

Show the name and city of customers from Ghana and the UK, with Unknown instead of a blank city. Call the column city.

Show the solution
Solution
SELECT name, COALESCE(city, 'Unknown') AS city
FROM customers
WHERE country IN ('Ghana', 'UK');

Result: 2 rows

Query result
namecity
John SmithLondon
Kofi MensahUnknown

How to get there: COALESCE returns London for John, because it is not NULL, and the fallback text for Kofi.

Common mistakes

Writing = NULL or <> NULL

You see: Empty results, no error

Fix: Use IS NULL and IS NOT NULL.

Treating NULL as zero or empty text

You see: Totals or filters that are slightly wrong

Fix: Remember NULL means unknown. Use COALESCE(column, 0) if you really want zero.

Forgetting NULL rows in a not-equal filter

You see: Rows with missing values silently disappear

Fix: Add OR column IS NULL when those rows should count.

Recap

  • NULL means a value is missing or unknown. It is not 0 and not ''.
  • Find it with IS NULL / IS NOT NULL; = NULL never matches.
  • COALESCE(column, fallback) shows a replacement for NULL in results.
  • NULL makes comparisons and arithmetic unknown, so check how your filters treat it.

Key terms

IS NULL
The test for a missing value.
COALESCE
A function that returns the first of its values that is not NULL.

Check your understanding

Answer all 4 questions, then check your answers to see an explanation for each one.

  1. 1. Which filter finds customers with no email?

  2. 2. What is `COALESCE(NULL, 'Pune', 'Delhi')`?

  3. 3. Kofi's city is NULL. Is Kofi returned by `WHERE city <> 'Mumbai'`?

  4. 4. What does NULL mean in a price column?

Answer all 4 questions to check.

Mark this lesson complete

Finished this lesson?

When you have worked through the examples and exercises, mark it complete to save your progress.