CodeBlanks Logo Icon CodeBlanks

Summary - NULL handling

Write NULL-safe queries with COALESCE and NULLIF.

Summary - NULL handling

In this group you learned NULL, IS NULL, COALESCE, NULLIF and IFNULL. Handling NULL correctly is key to robust queries.

SELECT COALESCE(NULLIF(total, 0), 1) AS duzelti FROM orders;

If total is 0 returns NULL then 1.

COALESCE(NULLIF(total, 0), 1) AS duzelti is selected from orders.

Practice Summary - NULL handling

Select names from customers where email is null.

Starter code

query.sql
-- summary null
SELECT name
FROM customers
WHERE

Sample solution

query.sql
SELECT name
FROM customers
WHERE email IS NULL

Expected output

name
Mehmet
Zeynep

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 Which gives the first non-NULL value?

  • NULLIF
  • IFNULL
  • COALESCE Correct answer
  • IS NULL

Explanation: COALESCE returns the first non-NULL value.

2 Summary - NULL handling: match the terms

Term Meaning
IS NULL empty
COALESCE fallback
NULLIF null if equal
IFNULL two fallbacks

All lessons in Beginner