COALESCE() in MySQL — How It Works
What the MySQL COALESCE() function does, with an example.
Published October 21, 2026
COALESCE(a, b, c, ...) returns the first non-NULL argument. It is the standard way to provide defaults for NULL values.
How it works
It is portable across databases, unlike IFNULL.
Try it in MySQL
SELECT COALESCE(NULL, NULL, 'fallback')
fallback
SELECT COALESCE(nickname, name, 'Guest') FROM users
display name
SELECT COALESCE(SUM(total), 0) FROM orders
0 if none
Practical examples
Example 1
SELECT COALESCE(NULL, NULL, 'fallback');
-- fallbackExample 2
SELECT COALESCE(nickname, name, 'Guest') FROM users;
-- display nameExample 3
SELECT COALESCE(SUM(total), 0) FROM orders;
-- 0 if noneUse it in a real query
SELECT id, COALESCE(NULL, NULL, 'fallback') AS result
FROM your_table
LIMIT 10;Common mistakes
- Mixing data types in arguments.
- Using it to hide missing data issues.
- Confusing with IFNULL (two args only).
Key takeaways
- COALESCE() in one line: COALESCE(a, b, c, ...) returns the first non-NULL argument.
- Try queries in a test database first.
- Check NULL handling and data types.
FAQ
COALESCE vs IFNULL?
COALESCE takes any number of arguments; IFNULL takes two.
Does COALESCE() work in other databases?
Most have an equivalent, but names and syntax vary between MySQL, PostgreSQL, SQL Server and SQLite.