Skip to content
Related Articles

Related Articles

Improve Article

MySQL | IF( ) Function

  • Last Updated : 21 Nov, 2019

The MySQL IF() function is used for validating a condition. The IF() function returns a value if the condition is TRUE and another value if the condition is FALSE. The MySQL IF() function can return values that can be either numeric or strings depending upon the context in which the function is used.
The IF() function accepts one parameter which is the condition to be evaluated.

Syntax:

IF(condition, true_value, false_value)

Parameters Used:

  • condition – It is used to specify the condition to be evaluated.
  • true_value – It is an optional parameter which is used to specify the value to be returned if the condition evaluates to be true.
  • false_value – It is an optional parameter which is used to specify the value to be returned if the condition evaluates to be false.

Return Value:
The MySQL IF() function returns a value if the condition is TRUE or a different value if the condition is FALSE.

Supported Versions of MySQL:



  • MySQL 5.7
  • MySQL 5.6
  • MySQL 5.5
  • MySQL 5.1
  • MySQL 5.0
  • MySQL 4.1
  • MySQL 4.0
  • MySQL 3.23

Example-1: Implementing IF() function on a numeric condition and returning a string value.

SELECT IF(5<12, 'TRUE', 'FALSE'); 

Output:

TRUE 

Example-2: Implementing IF() function to compare two strings.

SELECT IF(STRCMP('geeksforgeeks', 'gfg')=0, 'TRUE', 'FALSE'); 

Output:

FALSE 

Example-3: Implementing IF() function on a numeric condition and returning a numeric value.

SELECT IF(5<12, '1', '0'); 

Output:

1 
My Personal Notes arrow_drop_up
Recommended Articles
Page :