MySQL | IF( ) Function

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


If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.