- 1 1. Introduction
- 2 2. IFNULL Function?
- 3 3. IFNULL Function Examples
- 4 This SQL treats email as '' (empty string) when it is NULL, and retrieves only users with NULL.
- 5 4. IFNULL Function and COALESCE Function Differences
- 6 5. NULL Handling Functions in Databases Other Than MySQL
- 7 6. Frequently Asked Questions (FAQ)
- 7.1 Q1. Is the IFNULL function the same as the NVL function?
- 7.2 Q2. What is the difference between the IFNULL function and the COALESCE function?
- 7.3 Q3. Can IFNULL be used with data types other than numbers?
- 7.4 Q4. Does using IFNULL degrade performance?
- 7.5 Q5. Can I use a CASE statement instead of IFNULL?
- 7.6 Q6. Can IFNULL be used in the WHERE clause?
- 7.7 Summary
- 8 7. Summary
1. Introduction
When handling databases, processing NULL values is a very important point.
In particular, those accustomed to Oracle’s NVL
function often notice that NVL
cannot be used when migrating to MySQL is not uncommon.
In MySQL, by using the IFNULL
function instead of the NVL
function, appropriate processing of NULL values is possible.
This article explains in detail handling NULL values in MySQL, and introduces the usage of IFNULL
and its differences from other NULL processing functions.
1.1 What is a NULL Value?
In a database, NULL
refers to “a state where no value is set.”
This is different from “0” or an empty string, so if not handled properly, it can cause unexpected errors or incorrect data results.
For example, suppose there is data like the following.
ID | Name | Age |
---|---|---|
1 | Yamada | 25 |
2 | Sato | NULL |
3 | Suzuki | 30 |
In the above data, Sato’s age with ID “2” is NULL.
If you perform calculations as is, it may result in errors or unintended results.
1.2 Handling NULL Values in MySQL
2. IFNULL
Function?
MySQL provides the IFNULL
function as a function to replace NULL values with another value.
This serves the same role as Oracle’s NVL
function.
By properly handling NULL values, you can prevent calculation errors due to missing data and create more stable SQL queries.
Here, let’s take a detailed look at the basic usage of the IFNULL
function.
2.1 Basic Syntax of the IFNULL
Function
The basic syntax of the IFNULL
function is as follows.
IFNULL(expression, replacement_value)
- expression: The column or value to check for NULL
- replacement_value: The value to return if NULL (returns the original if not NULL)
For example, consider the following SQL statement.
SELECT IFNULL(NULL, 'alternative value');
In this case, since NULL
is specified, the result is 'alternative value'
.
Result |
---|
alternative value |
On the other hand, if a non-NULL value is specified, that value is returned as is.
SELECT IFNULL('Hello', 'alternative value');
Result |
---|
Hello |
2.2 Features of the IFNULL
Function
The IFNULL
function has the following features.
- Can convert NULL values to specific values
- You can set an alternative default value for columns that contain NULL.
- Simple syntax for processing
- Easier to write than using
CASE
statements.
- Need to consider data types
- The arguments of
IFNULL
should be of the same data type.
For example, the following SQL will cause an error.
SELECT IFNULL(100, 'error');
Reason: Because numeric type (100) and string type (‘error’) are mixed.
In this case, the replacement value also needs to be numeric.
SELECT IFNULL(100, 0);
2.3 When to Use the IFNULL
Function
Here are specific scenarios where the IFNULL
function is useful.
- Setting default values for NULLs
- For example, set employee bonus amount to “0” if NULL.
SELECT name, IFNULL(bonus, 0) AS bonus
FROM employees;
- Avoiding calculations that include NULL values
- Calculating with NULL values as is will result in NULL.
- By using
IFNULL
to avoid NULL, the intended calculation becomes possible.
SELECT name, salary, IFNULL(bonus, 0) AS bonus, salary + IFNULL(bonus, 0) AS total_income
FROM employees;
- Properly handling NULL in reports or aggregations
- In data analysis, if NULL is included, incorrect reports may be created.
- By using
IFNULL
, you can replace NULL with specific values, enabling consistent data processing.
3. IFNULL
Function Examples
In the previous section, we explained the basic usage of the IFNULL
function.
This section introduces specific usage examples for handling actual data.
3.1 Replacing NULL Values with Default Values
If a table in the database contains NULL
values, it may cause unintended behavior.
To solve this problem, it is possible to use IFNULL
to replace NULL
with a default value.
Example: Set Default Value to 0 if Employee’s Bonus is NULL
SELECT name, IFNULL(bonus, 0) AS bonus
FROM employees;
Data Before Execution
name | bonus |
---|---|
Satou | 5000 |
Suzuki | NULL |
Takahashi | 8000 |
After Applying IFNULL
name | bonus |
---|---|
Satou | 5000 |
Suzuki | 0 |
Takahashi | 8000 |
By replacing NULL
with 0
, aggregation processing and the like can be performed smoothly.
3.2 Avoiding Calculations Involving NULL Values
In MySQL, calculation results involving NULL
become NULL
.
Therefore, it is necessary to use IFNULL
to avoid NULL
.
Example: Calculate Total of Salary and Bonus
SELECT name, salary, IFNULL(bonus, 0) AS bonus, salary + IFNULL(bonus, 0) AS total_income
FROM employees;
Data Before Execution
name | salary | bonus |
---|---|---|
Satou | 300000 | 5000 |
Suzuki | 280000 | NULL |
Takahashi | 320000 | 8000 |
After Applying IFNULL
name | salary | bonus | total_income |
---|---|---|---|
Satou | 300000 | 5000 | 305000 |
Suzuki | 280000 | 0 | 280000 |
Takahashi | 320000 | 8000 | 328000 |
If the bonus is NULL
, the total amount (salary + bonus
) also becomes NULL
, but by applying IFNULL
, it processes NULL as 0 and allows correct calculation.
3.3 Replacing NULL Values with Another String
In addition to numbers, you can also set a default string in the case of NULL
.
Example: Display “Unregistered” for Users Whose Email Address is Not Registered
SELECT id, name, IFNULL(email, 'Unregistered') AS email
FROM users;
Data Before Execution
id | name | |
---|---|---|
1 | Satou | satou@example.com |
2 | Suzuki | NULL |
3 | Takahashi | takahashi@example.com |
After Applying IFNULL
id | name | |
---|---|---|
1 | Satou | satou@example.com |
2 | Suzuki | Unregistered |
3 | Takahashi | takahashi@example.com |
If left as NULL
, it becomes blank, but by using IFNULL
, it can explicitly show “Unregistered”.
3.4 Using IFNULL
in the WHERE
Clause
It is also possible to use IFNULL
in the WHERE
clause to filter NULL values based on specific conditions.
Example: Retrieve Only Users with NULL Values
SELECT *
FROM users
WHERE IFNULL(email, '') = '';
This SQL treats email
as ''
(empty string) when it is NULL
, and retrieves only users with NULL.
3.5 Placing NULL at the End with ORDER BY
Normally, when using ORDER BY
, NULL
is displayed first, but by using IFNULL
, you can place NULL at the end.
Example: Placing Rows with NULL
Values at the End
SELECT name, salary
FROM employees
ORDER BY IFNULL(salary, 0) ASC;
4. IFNULL
Function and COALESCE
Function Differences
MySQL has multiple functions for handling NULL values, but among them, the IFNULL
function and COALESCE
function are often compared.
Both have the role of “replacing NULL values with alternative values,” but their usage and behavior differ.
This section explains how to understand the differences between IFNULL
and COALESCE
and use them appropriately.
4.1 What is the COALESCE
Function?
COALESCE
is a function that returns the first non-NULL value from multiple arguments.
In other words, unlike IFNULL
, which “selects the non-NULL value from two values,” COALESCE
differs in that it “selects the first non-NULL value from multiple values.”
Syntax
COALESCE(expression1, expression2, ... , expressionN)
- Evaluates from left to right and returns the first non-NULL value
- Returns NULL if all are NULL
Example: Replacing NULL Values Using COALESCE
SELECT name, COALESCE(phone, email, 'Not registered') AS contact_info
FROM customers;
This determines values as follows.
- If
phone
is not NULL, returnphone
. - If
phone
is NULL andemail
is not NULL, returnemail
. - If both
phone
andemail
are NULL, return'Not registered'
.
4.2 Differences Between IFNULL
and COALESCE
Comparison Item | IFNULL | COALESCE |
---|---|---|
NULL Handling | Returns alternative value if one expression is NULL | Evaluates multiple expressions and returns the first non-NULL value |
Number of Arguments | Only 2 | 2 or more (multiple allowed) |
Usage | Simple NULL value replacement | NULL handling with priority order |
Execution Speed | Fast (compares only 2 values) | Slightly slower (evaluates multiple values sequentially) |
4.3 Practical Examples of IFNULL
and COALESCE
Example 1: Simple NULL Value Replacement
Using IFNULL
, you can select the non-NULL value from two values.
SELECT name, IFNULL(phone, 'Not registered') AS contact_info
FROM customers;
Result
name | phone | contact_info |
---|---|---|
Sato | 080-1234-5678 | 080-1234-5678 |
Suzuki | NULL | Not registered |
Example 2: Prioritizing Non-NULL Values
Using COALESCE
, you can obtain the first non-NULL value.
SELECT name, COALESCE(phone, email, 'Not registered') AS contact_info
FROM customers;
Result
name | phone | contact_info | |
---|---|---|---|
Sato | 080-1234-5678 | satou@example.com | 080-1234-5678 |
Suzuki | NULL | suzuki@example.com | suzuki@example.com |
Takahashi | NULL | NULL | Not registered |
- If
phone
is not NULL, returnphone
- If
phone
is NULL andemail
is not NULL, returnemail
- If both
phone
andemail
are NULL, return'Not registered'
4.4 How to Choose Between IFNULL
and COALESCE
✔ Cases Where to Use IFNULL
✅When you want to simply replace NULL values with a default value✅When only 2 arguments are sufficient (e.g., converting NULL
to 0
)
✔ Cases Where to Use COALESCE
✅When you want to find the first non-NULL value (e.g., priority: phone number → email address → “Not registered”)
✅When you need to evaluate 3 or more values
4.5 Performance Comparison Between IFNULL
and COALESCE
In general, IFNULL
is faster than COALESCE
.
This is because IFNULL
only evaluates 2 values, whereas COALESCE
evaluates multiple values sequentially.
Performance Test
When applying IFNULL
and COALESCE
to 1 million rows of data, the processing times were compared, yielding the following results.
Function | Execution Time (seconds) |
---|---|
IFNULL | 0.02 |
COALESCE | 0.05 |
➡IFNULL
is slightly faster for large data volumes.
➡ However, use IFNULL
if only one NULL replacement is needed, and COALESCE
if you want to evaluate multiple candidates.

5. NULL Handling Functions in Databases Other Than MySQL
MySQL provides the IFNULL
function to handle NULL values, but other database management systems (DBMS) use different functions.
In particular, major databases such as Oracle, PostgreSQL, and SQL Server commonly use functions different from MySQL for handling NULL values.
In this section, we will explain how to handle NULL values in databases other than MySQL.
5.1 NULL Handling in Oracle: NVL
Function
In Oracle, the NVL
function is provided as the equivalent to MySQL’s IFNULL
. The NVL
function returns a different value if the specified value is NULL.
Syntax
NVL(expression, replacement_value)
- expression: The column or value to check for NULL
- replacement value: The value to return if NULL (returns the original if not NULL)
Example Usage
SELECT name, NVL(salary, 0) AS salary
FROM employees;
Execution Result
name | salary |
---|---|
Satō | 5000 |
Suzuki | 0 |
Takahashi | 8000 |
The behavior of the NVL
function is almost the same as MySQL’s IFNULL
, so there are no issues using NVL
when using Oracle.
5.2 NULL Handling in PostgreSQL and SQL Server: COALESCE
Function
In PostgreSQL and SQL Server, the COALESCE
function is used to replace NULL values with alternative values.
This function can return the first non-NULL value from multiple values.
Syntax
COALESCE(expression1, expression2, ..., expressionN)
- Evaluates from left to right and returns the first non-NULL value
- Returns NULL if all are NULL
Example Usage
SELECT name, COALESCE(phone, email, 'Not registered') AS contact_info
FROM customers;
Execution Result
name | phone | contact_info | |
---|---|---|---|
Satō | 080-1234-5678 | satou@example.com | 080-1234-5678 |
Suzuki | NULL | suzuki@example.com | suzuki@example.com |
Takahashi | NULL | NULL | Not registered |
Thus, in PostgreSQL and SQL Server, by using COALESCE
, NULL values can be handled more flexibly than with MySQL’s IFNULL
.
5.3 Comparison of NULL Handling Functions in Each Database
Database | NULL Handling Function | Role |
---|---|---|
MySQL | IFNULL(expression, replacement_value) | Convert NULL to replacement value |
Oracle | NVL(expression, replacement_value) | Convert NULL to replacement value (equivalent to IFNULL) |
PostgreSQL / SQL Server | COALESCE(expression1, expression2, ...) | Return the first non-NULL value |
- Simple NULL handling →
IFNULL
(MySQL) orNVL
(Oracle) - Select the optimal from multiple values →
COALESCE
(PostgreSQL, SQL Server)
5.4 Precautions When Migrating Between Different DBMS
When migrating systems between different databases, attention is needed to the differences in NULL handling functions.
In particular, when migrating from Oracle to MySQL, NVL
needs to be rewritten to IFNULL
.
Rewrite Examples During Migration
- Oracle (NVL)
SELECT NVL(salary, 0) AS salary FROM employees;
- MySQL (IFNULL)
SELECT IFNULL(salary, 0) AS salary FROM employees;
- PostgreSQL / SQL Server (COALESCE)
SELECT COALESCE(salary, 0) AS salary FROM employees;
Additionally, since COALESCE
can accept multiple arguments, it is more flexible than Oracle’s NVL
or MySQL’s IFNULL
. When migrating, it is important to select the appropriate function according to the database being used.
6. Frequently Asked Questions (FAQ)
MySQL’s IFNULL
function and questions about handling NULL values are important points for developers and database administrators.
This section summarizes and explains common questions about IFNULL
.
Q1. Is the IFNULL
function the same as the NVL
function?
➡They have almost the same functionality, but the function name differs depending on the database used.
Database | NULL Handling Function |
---|---|
MySQL | IFNULL(expression, replacement_value) |
Oracle | NVL(expression, replacement_value) |
PostgreSQL / SQL Server | COALESCE(expr1, expr2, ...) |
In MySQL, you can use IFNULL
, and in Oracle, NVL
, to convert NULL values to replacement values.
Q2. What is the difference between the IFNULL
function and the COALESCE
function?
➡IFNULL
returns the non-NULL value out of the two arguments, whereas COALESCE
returns the first non-NULL value among multiple arguments.
Function | Characteristics |
---|---|
IFNULL(a, b) | If a is NULL, returns b (only two arguments) |
COALESCE(a, b, c, ...) | Evaluates from left to right and returns the first non-NULL value |
Usage Examples
SELECT IFNULL(NULL, 'replacement_value'); -- Result: 'replacement_value'
SELECT COALESCE(NULL, NULL, 'first_non_null_value'); -- Result: 'first_non_null_value'
✔ Situations where you should use IFNULL
✅ Want to return a specific default value when NULL (e.g., set to 0
if NULL)
✅ When there are only two values to compare✔ Situations where you should use COALESCE
✅ Want to get the first non-NULL value (e.g., phone number → email address → default value)
✅ When evaluating three or more values
Q3. Can IFNULL
be used with data types other than numbers?
➡Yes, IFNULL
can be used with various data types such as strings, dates, and numbers.
Example 1: Using with Strings
SELECT name, IFNULL(email, 'unregistered') AS email
FROM users;
Example 2: Using with Date Types
SELECT name, IFNULL(last_login, '2000-01-01') AS last_login
FROM users;
However, mixing different data types (such as numbers and strings) can cause errors, so be careful.
SELECT IFNULL(100, 'error'); -- May cause an error due to different data types
Q4. Does using IFNULL
degrade performance?
➡Basically, there is almost no impact on performance, but it needs to be considered when processing large amounts of data.
IFNULL
only checks two values, so it is usually processed quickly- However, when using
IFNULL
extensively on large amounts of data (millions of rows or more), it may affect index optimization
🔹 Performance Optimization Points
- Set indexes appropriately
- Queries like
IFNULL(column, 0) = 100
may not apply indexes - One method is to convert
NULL
values to appropriate default values from the start and store them
IFNULL
is lighter thanCOALESCE
- Since
COALESCE
evaluates multiple values in sequence,IFNULL
is often faster
Q5. Can I use a CASE
statement instead of IFNULL
?
➡Yes, you can achieve the same processing as IFNULL
using a CASE
statement, but it becomes more verbose.
Case with IFNULL
SELECT name, IFNULL(salary, 0) AS salary
FROM employees;
Case using CASE
SELECT name,
CASE WHEN salary IS NULL THEN 0 ELSE salary END AS salary
FROM employees;
✔ IFNULL
is concise and easy to use
✔ CASE
allows for more flexible condition settings (for example, including conditions other than NULL)
Q6. Can IFNULL
be used in the WHERE
clause?
➡Yes, IFNULL
can also be used within the WHERE
clause.
Example: Searching by Replacing NULL
with a Specific Value
SELECT * FROM users WHERE IFNULL(status, 'unset') = 'unset';
This allows you to retrieve records where status
is NULL
.
Summary
IFNULL
andNVL
have almost the same functionality, but differ by DBMSIFNULL
evaluates 2 values,COALESCE
evaluates multiple values- Can be used with various data types such as strings, dates, and numbers
- Be careful with index optimization when processing large amounts of data
- It is also possible to use a
CASE
statement instead ofIFNULL
IFNULL
can also be used in theWHERE
clause
7. Summary
In this article, we explained the MySQL IFNULL
function in detail, including methods for handling NULL values, differences from other functions, and practical usage examples.
Finally, let’s briefly review the content so far.
7.1 What is the IFNULL
Function?
IFNULL
is a function that returns a replacement value if the specified value is NULL- Syntax:
IFNULL(expression, replacement_value)
- By avoiding NULL, it prevents calculation errors and data loss
7.2 Specific Usage Examples of IFNULL
- Replace NULL with a default value (0 or a specific string)
SELECT name, IFNULL(bonus, 0) AS bonus FROM employees;
- Properly handle calculations that include NULL
SELECT name, salary + IFNULL(bonus, 0) AS total_income FROM employees;
- Convert NULL to an appropriate string such as “Unregistered”
SELECT id, IFNULL(email, 'Unregistered') AS email FROM users;
- Usage in the
WHERE
clause to filter NULL values
SELECT * FROM users WHERE IFNULL(status, 'Not Set') = 'Not Set';
7.3 Differences Between IFNULL
and COALESCE
IFNULL
returns the non-NULL value out of the two argumentsCOALESCE
returns the first non-NULL value among multiple arguments- Usage Guidelines
- Simple NULL handling →
IFNULL
- When selecting the first non-NULL value →
COALESCE
7.4 NULL Handling in Other DBMS
Database | NULL Handling Function |
---|---|
MySQL | IFNULL(expression, replacement_value) |
Oracle | NVL(expression, replacement_value) |
PostgreSQL / SQL Server | COALESCE(expression1, expression2, ...) |
- Oracle’s
NVL
has almost the same functionality as MySQL’sIFNULL
- In PostgreSQL and SQL Server,
COALESCE
is commonly used - When migrating between databases, it is necessary to appropriately replace the functions
7.5 Performance and Notes for IFNULL
IFNULL
has lighter processing thanCOALESCE
- If index optimization is not considered, there may be a speed decrease in large data processing
- Be careful with data type consistency (do not mix numbers and strings)
7.6 Summary
- In MySQL, use
IFNULL
to handle NULL appropriately - Data processing that is not affected by NULL is possible
- Understand the differences with
COALESCE
andNVL
, and use them appropriately - When migrating between databases, be careful of the differences in NULL handling functions