This tutorial will teach us how to use the IF function in MySQL. This function allows us to introduce conditional querying in the MySQL database.

If you have basic programming knowledge, you are probably familiar with the IF…ELSE statements in other programming languages. These statements enable you to evaluate multiple conditions and take the necessary actions if either is true.

Let us learn how we can work with the IF function in MySQL.

## MySQL IF Function

Unlike the traditional if…else statements, the IF function in MySQL returns a value based on the specified condition.

The function syntax is shown below:

If the value of the *expression *evaluates to TRUE, the function returns the value of the *if_true_expression. *Otherwise, the function will return the value of the *if_false_expression. *

**NOTE**: Keep in mind that NULL and 0 are treated as FALSE.

## Example 1: Usage

Let us illustrate how the IF function works with several practical examples.

if(100 >= 10,

'max: 100',

'max: 10') as res;

We check if the expression 100 > 10 is true in this case. If yes, we print “max: 100”; otherwise, print “max: 10”.

Since the expression is true, the function should return, as shown below:

--------+

max: 100|

## Example 2: Using IF Function With NULL

The following example shows the usage of the IF function when working with NULL values:

if(NULL,

'true',

'false') as res;

The resulting output is shown below:

-----+

false|

This is because NULL is treated as a false value.

## Conclusion

This short tutorial described the basics and usage of the IF() function in MySQL. Several examples provided its basic functions and usage with the NULL values. Feel free to explore the other articles for more information.