IF

The three argument function that evaluates its first argument as a boolean, and then returns either its second value or its third.

ParameterTypeDescription
test_value
boolean
the boolean value to check
if_value
any
the value of the expression if the test_value is true
else_value
any
the value of the expression if the test_value is false
Returns: any

Example

query:

SELECT sales_agent,
       IF(status = "Former", NULL, "Active")
  FROM sales_teams

results:

sales_agent if
Anna Snelling Active
Cecily Lampkin Active
Mei-Mei Johns Active
Violet Mclelland Active
Corliss Cosme Active
Rosie Papadopoulos Active
Garret Kinder
Wilburn Farren Active
Elizabeth Anderson Active
Daniell Hammack Active

Calling with a test_value that is NULL (or undefined) will not return the if_value or the else_value, but NULL. This often crops up when doing a LEFT JOIN and the joined table doesn’t include a match row.

If you need NULL values to evaluate as FALSE, please use COALESCE:

IF(COALESCE(potentially_undefined_column, FALSE), "true", "false/null/undefined")