©SQL :: Manager
HomeProgram InformationTechnical detailsFree downloadDonate for a cup of coffee
myway SQL Manager for MySQL and MariaDB, Download on Uptodown
SQL functionMySQLMariaDB

The IF Function - Conditional value selection in SQL queries

IFSyntax:IF(Expression, Value1, Value2)
Return value:Depending on the data types used
Function type:Comparison function
Function Description

The IF() function returns "Value1" if "Expression" is TRUE, or "Value2" if the condition FALSE.

If the value of "Expression" is NULL, the condition is FALSE.

SQL Examples for the IF function


select if(true'value true''value false');
select if(false'value true''value false');
select if(3 > 4, 'value true''value false');
select if(3 < 4, 'value true''value false');
select if(null'value true''value false');
select if(3 = 4, 'ok'null);
select if(3 <> 4, 'ok'null);

if(true, 'value true', 'value false')
varchar(11) BINARY
value true
if(false, 'value true', 'value false')
varchar(11) BINARY
value false
if(3 > 4, 'value true', 'value false')
varchar(11) BINARY
value false
if(3 < 4, 'value true', 'value false')
varchar(11) BINARY
value true
if(null, 'value true', 'value false')
varchar(11) BINARY
value false
if(3 = 4, 'ok', null)
varchar(2) BINARY
if(3 <> 4, 'ok', null)
varchar(2) BINARY
The examples were created with the MyWAY SQL manager: Download

How to use the IF() function in MySQL and MariaDB databases

In both MySQL and MariaDB the IF() function is used to perform conditional logic within SQL querys and allows to return different values ​​based on a specified condition. The function can also be used in combination with other functions or expressions, for example to adjust the returned values ​​based on certain conditions.

The true and false expressions can be literals, table column values, or subqueries, but their data types should be compatible. The IF() function in MySQL and MariaDB provides a good way to get values ​​based on specified conditions to return conditionally, which gives much more flexibility to SQL queries against databases.

Further MySQL und MariaDB SQL Comparison functions

More about COALESCE Function

More about DECODE_ORACLE Function

More about GREATEST Function

More about INTERVAL Function

More about LEAST Function

STRCMPSTRCMP(String 1, String 2)
More about STRCMP Function

Numeric functionsString functionsRegular ExpressionsDate and Time functions
Comparison functionsEncryption & CompressionConversion functionsNULL functions
Aggregate functionsWindow functionsJSON functionsGeometric functions
Sequence functionsInformation functionsDynamic ColumnsMiscellaneous functions
More information about the IF SQL function: mysql.com and mariadb.com and w3schools.com
Updated: 2023-11-27ImprintContactTerms & conditionsPrivacy & CookiesUpdatesSitemapFacebookLinkedinTwitterStatistics©2020-2023 Heino Cunze-Fischer