SQLPathLab
Conditions and NULL · Basic

IFNULL

replacing NULL with a default value.

Overview

MySQL IFNULL(value, fallback) uses the fallback only when the first expression is NULL; empty strings, zero, and FALSE are not replaced. Keep the fallback type compatible with the source column to avoid implicit conversion in sorting or arithmetic. COALESCE is usually preferable for portable SQL.

Basic syntax

SELECT IFNULL(phone, 'N/A') AS phone FROM customers;

Examples

IFNULL example

The example displays N/A for NULL customer phone values while preserving existing numbers; it does not modify the source table.

Sample table

Table:customers
customers
phone
138-0000-0001
NULL
138-0000-0003

Run result

result
phone
138-0000-0001
N/A
138-0000-0003

Common issues

Does IFNULL also replace empty strings?

No. IFNULL uses the fallback only for NULL; an empty string, zero, and FALSE are not NULL.

How do I choose between IFNULL and COALESCE?

MySQL IFNULL accepts two arguments. COALESCE checks multiple candidates and is more portable across databases.

Why should the fallback type be compatible?

The database must derive one result type. Mixing numbers and text may cause implicit conversion that affects comparisons, sorting, or application reads.

Tags

ConditionsCommon

Was this explanation clear?