What is IF Null Function in SQL?

IFNULL function is one of the MySQL control flow functions that accepts two arguments and returns the first argument if it is not NULL. Otherwise, the IFNULL function returns the second argument.

The two arguments can be literal values or expressions.

The following illustrates the syntax of the IFNULL function:

IFNULL(expression_1,expression_2);

Example:

  1. SELECT IFNULL(1,0); -- returns 1

  2. SELECT IFNULL('',1); -- returns ''

  3. SELECT IFNULL(NULL,'IFNULL function'); -- returns IFNULL function

How it works.

  • IFNULL(1,0) returns 1 because 1 is not NULL .
  • IFNULL(' ',1) returns ' ' because the ' ' string is not NULL .
  • IFNULL(NULL,'IFNULL function') returns IFNULL function string because the first argument is NULL.