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.
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.
Why NULLs Cause Problems
SUM, AVG silently skip NULL rows, giving misleading results.WHERE price = NULL returns no rows because nothing "equals" NULL.NULL Propagation: The Chain Reaction
The COALESCE Function
Returns the first non-NULL value in the list, reading left to right. If every argument is NULL, the result is NULL.
The IFNULL Function
If expression is NULL, return replacement. Otherwise, return expression unchanged. It accepts exactly two arguments.
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
0. For strings, 'Unknown' or 'N/A'.column_name with COALESCE(column_name, default).COALESCE(a, b, c) tries each in order.Example 1: Simple Default
A students table has a nullable nickname column. Display the nickname if it exists, otherwise show the first_name.
Example 2: Fallback Chain
A contacts table has three phone columns. Display the best available phone number.
Example 3: IFNULL in MySQL
Calculate the final price after applying a nullable discount. If no discount exists, treat it as zero.
Nesting IFNULL vs Using COALESCE
When you need multiple fallbacks with IFNULL, you must nest calls. COALESCE handles this naturally.
Common Mistakes
WHERE x = NULL instead of WHERE x IS NULL. The comparison always returns unknown.COALESCE(price, 'none') may cause a type error.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?