MySQL If

April 12, 20191 min readUpdated 1/19/2021

 

IF function

The IF function is used to evaluate an expression and return a value.

The IF function consists of three parts.
1. condition to evaluate.
2. expression(can be another IF function) or value if the condition is evaluated to true.
3. expression(can be another IF function) or value if the condition is evaluated to false.

IF(condition, if_true_expr_or_value, if_false_expr_or_value)

Our example above shows ‘no sales rep’ if the condition(sales_rep_id IS NULL) is true and ‘sales rep’ if false.

IF function with aggregate functions

Here we are summing up the number of customers with and without reps.