Curriculum
Course: SQL
Login

Curriculum

SQL

SQL References

0/80

MySQL Functions

0/139

SQL Server Functions

0/84

SQL Quick Ref

0/1
Text lesson

IF

Example

Returns “YES” if the condition is true; otherwise, returns “NO”.

SELECT IF(500<1000“YES”“NO”);

Definition and Usage

The IF() function outputs a value if a condition is true, or an alternative value if the condition is false.

Syntax

IF(conditionvalue_if_truevalue_if_false)

Parameter Values

Parameter

Description

condition

Required: The value that will be evaluated

value_if_true

Required: The value that will be returned if the condition is true

value_if_false

Required: The value that will be returned if the condition is false

Technical Details

Works in:

 From MySQL version 4.0

More Examples

Example

Returns 5 if the condition is true, otherwise returns 10.

SELECT IF(500<1000510);

Example

Check if two strings are identical and return “YES” if they are, otherwise return “NO”.

SELECT IF(STRCMP(“hello”,“bye”) = 0“YES”“NO”);

Example

Returns “MORE” if the condition is true; otherwise, returns “LESS”.

SELECT OrderID, Quantity, IF(Quantity>10“MORE”“LESS”)
FROM OrderDetails;