MySQL IFNULL() function usage in SELECT queries

Josh Otwell
4 min readJul 29, 2020

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…

Photo by vardan harutyunyan on Unsplash

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’:

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)

--

--

Josh Otwell

SQL | PHP | Photography. Sign-up for my free developer newsletter, OpenLampTech, here: openlamptech.substack.com