MySQL IFNULL() function usage in SELECT queries

We all know as SQL professionals that the NULL marker is a special case. Oftentimes, you have NULL‘s stored in your tables, and that’s fine. Suppose you want to present an alternative value in query results where some of the columns have NULL? This is a perfect opportunity (but not the only) to use the IFNULL() function. I find IFNULL() quite useful when exporting query results to a CSV file or other type of flat file, providing something more meaningful than the NULL word itself. However, you only have one substitute for the NULL value when using IFNULL(), so keep that in mind. Continue reading and see examples using IFNULL() in SELECT queries…

OS, Software, and DB used:

  • OpenSuse Leap 15.1
  • MySQL 8.0.20


Self-Promotion:

If you enjoy the content written here, by all means, share this blog and your favorite post(s) with others who may benefit from or like it as well. Since coffee is my favorite drink, you can even buy me one if you would like!


There are a couple of NULL‘s in table ‘ages_1’:

1
2
3
4
5
6
7
8
9
10
11
12
 MySQL  localhost:33060+ ssl  practice  SQL > SELECT * FROM ages_1;
+--------+
| an_age |
+--------+
|     12 |
|     44 |
|   NULL |
|   NULL |
|     22 |
|     17 |
+--------+
6 rows in set (0.0010 sec)

IFNULL() syntax is as follows:

1
IFNULL(expression_1, expression_2)

IFNULL() returns ‘expression_2’ only if ‘expression_1’ is NULL. Otherwise, ‘expression_1’ is returned.

In the following query, the call to IFNULL() has both of the required parameters and will return the number 0 (zero) anywhere there is a NULL in the ‘an_age’ column:

1
2
3
4
5
6
7
8
9
10
11
12
 MySQL  localhost:33060+ ssl  practice  SQL > SELECT IFNULL(an_age, 0) FROM ages_1;
+-------------------+
| IFNULL(an_age, 0) |
+-------------------+
|                12 |
|                44 |
|                 0 |
|                 0 |
|                22 |
|                17 |
+-------------------+
6 rows in set (0.0008 sec)

The previous query results are also achievable using CASE:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
 MySQL  localhost:33060+ ssl  practice  SQL > SELECT
                                           -> CASE WHEN an_age IS NULL THEN 0
                                           -> ELSE an_age
                                           -> END AS an_age
                                           -> FROM ages_1;
+--------+
| an_age |
+--------+
|     12 |
|     44 |
|      0 |
|      0 |
|     22 |
|     17 |
+--------+
6 rows in set (0.0016 sec)

However, I find IFNULL() less verbose to type when used in this type of context. CASE can mimic IFNULL() behavior as shown in the previous example query. But, the opposite is not true. IFNULL() cannot mimic CASE. They both serve different purposes. Just so happens CASE can work like IFNULL() as used in this specific query. Read more on CASE from the official documentation for in-depth coverage.

Although the ‘an_age’ column is of datatype INTEGER, storing whole numbers, IFNULL() can substitute a string value in place of NULL for the returned query results as shown in the next query:

1
2
3
4
5
6
7
8
9
10
11
12
 MySQL  localhost:33060+ ssl  practice  SQL > SELECT IFNULL(an_age, 'MISSING') FROM ages_1;
+---------------------------+
| IFNULL(an_age, 'MISSING') |
+---------------------------+
| 12                        |
| 44                        |
| MISSING                   |
| MISSING                   |
| 22                        |
| 17                        |
+---------------------------+
6 rows in set (0.0008 sec)

To be clear, IFNULL() is not limited to only SELECT query results. I tend to use IFNULL() most often in SELECT statements as I have shown in this post. But, IFNULL() can be used in other MySQL statements as well.

If you have not already, give IFNULL() a try for those one-liners where you need to provide something more meaningful in query results containing NULL.

Visit the official IFNULL() online documentation for more information.

Like what you have read? See anything incorrect? Please comment below and thanks for reading!!!

A Call To Action!

Thank you for taking the time to read this post. I truly hope you discovered something interesting and enlightening. Please share your findings here, with someone else you know who would get the same value out of it as well.

Visit the Portfolio-Projects page to see blog post/technical writing I have completed for clients.

Have I mentioned how much I love a cup of coffee?!?!

To receive email notifications (Never Spam) from this blog (“Digital Owl’s Prose”) for the latest blog posts as they are published, please subscribe (of your own volition) by clicking the β€˜Click To Subscribe!’ button in the sidebar on the homepage! (Feel free at any time to review the Digital Owl’s Prose Privacy Policy Page for any questions you may have about: email updates, opt-in, opt-out, contact forms, etc…)

Be sure and visit the “Best Of” page for a collection of my best blog posts.


Josh Otwell has a passion to study and grow as a SQL Developer and blogger. Other favorite activities find him with his nose buried in a good book, article, or the Linux command line. Among those, he shares a love of tabletop RPG games, reading fantasy novels, and spending time with his wife and two daughters.

Disclaimer: The examples presented in this post are hypothetical ideas of how to achieve similar types of results. They are not the utmost best solution(s). The majority, if not all, of the examples provided, is performed on a personal development/learning workstation-environment and should not be considered production quality or ready. Your particular goals and needs may vary. Use those practices that best benefit your needs and goals. Opinions are my own.

One thought on “MySQL IFNULL() function usage in SELECT queries

Hey thanks for commenting! Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.