Skip to content
Lesson 7 of 10 · SQL for Beginners

NULLs and CASE

Handle missing values correctly and compute labels with CASE and COALESCE.

17 min

NULL means unknown or missing. It is not zero and not an empty string. Our customers table has NULL in email for Chen, Elif and Hugo: we do not have it.

Any comparison with NULL is neither true nor false: it is unknown, and WHERE keeps only true rows. So:

sql4 lines
WHERE email = NULL     -- returns nothing, always
WHERE email <> 'x'     -- also drops the NULL rows
WHERE email IS NULL    -- the right way
WHERE email IS NOT NULL

5 + NULL is NULL. COUNT(email), SUM, AVG, MIN, MAX skip NULLs, while COUNT(*) counts rows. With 8 customers and 3 missing emails: COUNT(*) is 8, COUNT(email) is 5.

COALESCE(a, b, c) returns the first value that is not NULL:

sql1 line
SELECT name, COALESCE(email, 'no email') AS contact FROM customers;

sql7 lines
SELECT title,
  CASE
    WHEN price < 12 THEN 'budget'
    WHEN price < 20 THEN 'standard'
    ELSE 'premium'
  END AS band
FROM books;

CASE checks the WHENs top to bottom and uses the first that is true, like if / elif / else. Without ELSE, no match gives NULL.

Reference

Comparisons with NULL

NULL = NULL

Result
NULL (unknown), not true

NULL IS NULL

Result
true

5 + NULL

Result
NULL

COUNT(col) where col is NULL

Result
not counted

COALESCE(NULL, 0)

Result
0

NULL AND false

Result
false

NULL OR true

Result
true

A WHERE clause keeps a row only when the condition is true. Unknown is dropped.

Exercise1 check
sql

Show the name of every customer who has no email address on file.

Tables you can query

  • customers (id, name, city, joined, email)
starter code2 lines
SELECT name FROM customers
WHERE
Exercise1 check
sql

Show each customer's name and a column contact holding their email, or the text no email when it is missing.

Tables you can query

  • customers (id, name, city, joined, email)
starter code2 lines
SELECT name,
FROM customers
Exercise1 check
sql

Show each book's title and a band: 'budget' if the price is under 12, 'standard' if under 20, otherwise 'premium'.

Tables you can query

  • books (id, title, author, genre, price, year, stock)
starter code2 lines
SELECT title,
FROM books
Common misconception

“`WHERE email <> '[email protected]'` returns every customer except Ben.”

In everyday logic, anyone who is not Ben is "not equal to Ben".

What is actually true

A row with a NULL email gives "unknown", not true, so Chen, Elif and Hugo are dropped too. To include them: WHERE email <> '[email protected]' OR email IS NULL.

How to catch yourself

Whenever a column may hold NULL, ask yourself what should happen to those rows.

  • NULL means unknown: use IS NULL and IS NOT NULL, never = NULL.
  • A WHERE keeps only true rows, so unknown comparisons drop the row.
  • Aggregates skip NULLs; COUNT(*) counts rows and COUNT(col) counts non-NULL values.
  • COALESCE supplies a default, and CASE computes a value by rules.
Check yourself

NULL checks.

1Multiple choice

Which finds rows where email is missing?

  • A`WHERE email = NULL`
  • B`WHERE email IS NULL`
  • C`WHERE email = ''`
  • D`WHERE NOT email`
2Numeric answer

With 8 customers and 3 NULL emails, what does COUNT(email) return?

3Numeric answer

What does COUNT(*) return for those same 8 customers?

4Multiple choice

What is 5 + NULL?

  • A5
  • B0
  • CNULL
  • DAn error
5Multiple choice

What does COALESCE(NULL, NULL, 7, 9) return?

  • ANULL
  • B7
  • C9
  • D16
For you · never graded

Think of a form you have filled in with optional fields. What would "missing" mean for each field, and what would you want a report to do with those rows?

Practise this lesson, not just read it

In BiliMind the questions above are marked when you answer, the flashcards come back on a schedule, and the rest of SQL for Beginners is planned around your week. Free.