01 / 20
? ✓

COALESCE & IFNULL
Handling NULLs in SQL

Learn to replace missing values gracefully using SQL's built-in NULL-handling functions, and understand when to use each across different database dialects.

SQL

What is NULL in SQL?

NULL is not a value: it represents the absence of data. It is not zero, not an empty string, and not the word "null." It means the data is unknown or missing.

'Alice' Has a value 0 or '' Known, but empty NULL Unknown / Missing These three are completely different in SQL

Why NULLs Cause Problems

✕ Broken Aggregates
SUM, AVG silently skip NULL rows, giving misleading results.
✕ Failed Comparisons
WHERE price = NULL returns no rows because nothing "equals" NULL.
✕ Ugly Output
Reports display blank cells or the word "null" instead of a friendly default.

NULL Propagation: The Chain Reaction

price = 50 + discount = NULL = total = NULL Apply COALESCE price = 50 + COALESCE(disc, 0) = 0 = total = 50 COALESCE replaces NULL with 0 before the math happens

The COALESCE Function

COALESCE(value1, value2, ..., valueN)

Returns the first non-NULL value in the list, reading left to right. If every argument is NULL, the result is NULL.

1
SQL evaluates value1. If it is not NULL, return it immediately.
2
Otherwise, evaluate value2. If not NULL, return it.
3
Continue until a non-NULL is found or the list is exhausted.

The IFNULL Function

IFNULL(expression, replacement)

If expression is NULL, return replacement. Otherwise, return expression unchanged. It accepts exactly two arguments.

expression IS NULL? (check) YES replacement NO expression IFNULL is a simple two-branch decision

COALESCE vs IFNULL

COALESCE IFNULL
SQL Standard ✓ ANSI SQL ✕ Non-standard
Arguments 2 or more Exactly 2
Portability All major databases MySQL, SQLite only
Fallback chain ✓ Built-in ✕ Requires nesting
Readability Clear with many fallbacks Simpler for one fallback

NULL Functions Across Dialects

Database 2-Argument N-Argument
MySQL IFNULL COALESCE
PostgreSQL (use COALESCE) COALESCE
SQL Server ISNULL COALESCE
Oracle NVL COALESCE
SQLite IFNULL COALESCE

How to Apply NULL Handling

1
Identify nullable columns. Check the schema: which columns allow NULL? Which have no DEFAULT?
2
Choose a sensible default. For numbers, often 0. For strings, 'Unknown' or 'N/A'.
3
Wrap the column. Replace column_name with COALESCE(column_name, default).
4
Chain fallbacks if needed. Use multiple arguments: COALESCE(a, b, c) tries each in order.
5
Test with NULL data. Always verify your query returns the expected default, not NULL or an error.

Example 1: Simple Default

A students table has a nullable nickname column. Display the nickname if it exists, otherwise show the first_name.

SELECT first_name, COALESCE(nickname, first_name) AS display_name FROM students;
first_name nickname display_name 'Maria' 'Mia' 'Mia' 'James' NULL 'James' 'Robert' 'Bobby' 'Bobby' NULL replaced by first_name automatically

Example 2: Fallback Chain

A contacts table has three phone columns. Display the best available phone number.

SELECT name, COALESCE(mobile, home, work, 'No phone') AS best_phone FROM contacts;
mobile NULL home NULL work NULL 'No phone' not NULL Return! Return! Return! Fallback COALESCE checks each argument left to right The first non-NULL value wins

Example 3: IFNULL in MySQL

Calculate the final price after applying a nullable discount. If no discount exists, treat it as zero.

-- MySQL / SQLite syntax SELECT product, price, price - IFNULL(discount, 0) AS final_price FROM products;
-- Equivalent standard SQL SELECT product, price, price - COALESCE(discount, 0) AS final_price FROM products;

Nesting IFNULL vs Using COALESCE

When you need multiple fallbacks with IFNULL, you must nest calls. COALESCE handles this naturally.

-- Ugly: nested IFNULL (MySQL) IFNULL(mobile, IFNULL(home, IFNULL(work, 'N/A')))
-- Clean: COALESCE (any database) COALESCE(mobile, home, work, 'N/A')

Common Mistakes

✕ Using = NULL
Writing WHERE x = NULL instead of WHERE x IS NULL. The comparison always returns unknown.
✕ Mismatched Types
Passing a string fallback for a numeric column: COALESCE(price, 'none') may cause a type error.
✕ Forgetting Portability
Using IFNULL in PostgreSQL or Oracle: it does not exist there. Use COALESCE instead.

Summary Cheat Sheet

Scenario Best Function Example
One fallback, any DB COALESCE COALESCE(x, 0)
One fallback, MySQL IFNULL IFNULL(x, 0)
Multiple fallbacks COALESCE COALESCE(a, b, c)
One fallback, SQL Server ISNULL ISNULL(x, 0)
One fallback, Oracle NVL NVL(x, 0)

Question 1 of 5

What does COALESCE(NULL, NULL, 'hello', 'world') return?

Question 2 of 5

Which statement about IFNULL is true?

Question 3 of 5

You want to display a user's preferred_name if available, otherwise their first_name, otherwise 'Guest'. Which query is correct?

Question 4 of 5

What is the SQL Server equivalent of MySQL's IFNULL?

Question 5 of 5

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