IFNULL() in MySQL — How It Works

What the MySQL IFNULL() function does, with an example.

Published October 21, 2026

IFNULL(a, b) returns a if it is not NULL, otherwise b. It is MySQL's two-argument NULL replacement.

How it works

Use COALESCE for portability.

Try it in MySQL

SELECT IFNULL(NULL, 'default')
default
SELECT IFNULL(phone, 'n/a') FROM users
phone or n/a
SELECT IFNULL(1, 2)
1

Practical examples

Example 1

SELECT IFNULL(NULL, 'default');
-- default

Example 2

SELECT IFNULL(phone, 'n/a') FROM users;
-- phone or n/a

Example 3

SELECT IFNULL(1, 2);
-- 1

Use it in a real query

SELECT id, IFNULL(NULL, 'default') AS result
FROM your_table
LIMIT 10;

Common mistakes

  • Using it where COALESCE would be more portable.
  • Replacing NULL with a value that changes meaning.
  • Mismatched types.

Key takeaways

  • IFNULL() in one line: IFNULL(a, b) returns a if it is not NULL, otherwise b.
  • Try queries in a test database first.
  • Check NULL handling and data types.

FAQ

IFNULL vs NULLIF?

IFNULL replaces NULL; NULLIF makes values NULL.

Does IFNULL() work in other databases?

Most have an equivalent, but names and syntax vary between MySQL, PostgreSQL, SQL Server and SQLite.

More SQL articles