Skip to content

IF

SET @input = 5;
SELECT IF(@input>3, 'Greater', 'Smaller') AS Result;
+---------+
| Result |
+---------+
| Greater |
+---------+

In this example, the IF statement in SQL is used to compare a variable (@input) to a specific value. The IF statement checks if the value of @input is greater than 3. If the condition is true, it returns ‘Greater’. If the condition is false, it returns ‘Smaller’. The returned value is displayed as ‘Result’. In this case, since the value of @input is 5 which is indeed greater than 3, the output is ‘Greater’.