Skip to main content
Evaluates a condition and returns different results based on whether the condition is true or false. The IF function is a simplified alternative to the CASE expression for handling conditional logic.

Syntax

IF(<condition>, <then>, <else>)

Parameters

ParameterDescriptionSupported input types
<condition>Condition that the function evaluates.BOOLEAN
<then>Value returned when <condition> evaluates to true.Any
<else>Value returned when <condition> evaluates to false or NULL. Must match the data type of <then>.Any

Return type

The IF function returns the same data type as the <then> and <else> parameters.

Example

The following example classifies each level in the levels table as 'Hard' when its maxpoints score threshold is 100 or above, and 'Easy' otherwise:
SELECT level, name, IF(maxpoints >= 100, 'Hard', 'Easy') AS difficulty
FROM levels
ORDER BY level;
level int nullname text nulldifficulty text
1Thunderbolt CircuitEasy
2Velocity ValeEasy
3Raceway RidgeEasy
4Nitro NarrowsHard
5Thunder RoadHard
6Burnout BoulevardEasy
7Speed StreetEasy
8Racing RavineHard
9Drift DistrictHard
10Acceleration AlleyHard

Rows: 10Execution time: 6.00ms