Conditional IF function
In the previous lesson, we looked at the CASE statement for implementing conditional logic in SQL. However, this is not the only mechanism by which it is possible to implement logic branching in a query. It's time to turn our attention to the IF function.
IF Syntax
IF(condition, value_if_true, value_if_false)- condition
- The logical expression being checked, for example price > 100.
- value_if_true
- The value IF returns if the condition is true.
- value_if_false
The value IF returns if the condition is false or NULL.
Additional conditional logic functions
In the previous lesson, we studied the CASE statement for implementing conditional logic in SQL. PostgreSQL provides additional functions that simplify working with conditional logic in special cases. These functions are especially useful when working with NULL values and creating more readable code.
Functions for conditional logic
In addition to the universal CASE operator, PostgreSQL provides:
- COALESCE function - for working with NULL values
- NULLIF function - for special cases with NULL
These functions are standard SQL functions and make code more readable in certain situations.
Examples
-
Simple comparison of two numbers. Since 10 is not greater than 20, the function will return 'FALSE'.
MySQL 8.4SELECT IF(10>20, "TRUE", "FALSE");
-
Simple example of conditional logic using the CASE operator from the previous lesson:
MySQL 8.4SELECT CASE WHEN 10 > 20 THEN 'TRUE' ELSE 'FALSE' END;
-
Example of use with a real database. Based on the price, it is necessary to determine whether the housing belongs to one of two classes: "Comfort class" and "Economy class". If the price is greater than or equal to 150, then this housing belongs to "Comfort class".
MySQL 8.4SELECT id, price, IF(price >= 150, "Comfort class", "Economy class") AS category FROM Rooms
-
Example with real data. The CASE operator helps categorize housing by price:
MySQL 8.4SELECT id, price, CASE WHEN price >= 150 THEN 'Comfort class' ELSE 'Economy class' END AS category FROM Rooms
-
IF functions can also be nested within each other, emulating the CASE operator.
MySQL 8.4SELECT id, price, IF(price >= 200, "Business Class", IF(price >= 150, "Comfort class", "Economy class")) AS category FROM Rooms
-
CASE is perfect for multiple conditions:
MySQL 8.4SELECT id, price, CASE WHEN price >= 200 THEN 'Business Class' WHEN price >= 150 THEN 'Comfort class' ELSE 'Economy class' END AS category FROM Rooms
However, for special cases, PostgreSQL provides more specialized functions.
IFNULL and NULLIF functions
In addition to the IF function, MySQL also has simpler, but less universal functions IFNULL and NULLIF, aimed at processing NULL values.
IFNULL syntax
IFNULL(value, alternative_value)- value
The value being checked, for example a column that may contain NULL. If it is not NULL, the function returns it.
- alternative_value
The value the function returns if value is NULL.
COALESCE function
The COALESCE function is an elegant solution for working with NULL values. It returns the first non-NULL value from the list of arguments.
Syntax
COALESCE(value [, ...])- [ ]optional part
- ...can be repeated
- value
Values separated by commas, for example table columns. They are checked from left to right, and you can put a fallback value last.
This is much more convenient than writing long CASE expressions to handle NULL.
Comparison of approaches
Using CASE:
MySQL 8.4CASE WHEN value1 IS NOT NULL THEN value1 WHEN value2 IS NOT NULL THEN value2 ELSE value3 END
Using COALESCE (much simpler):
MySQL 8.4COALESCE(value1, value2, value3)
-
If the first argument is not equal to NULL, then it will be returned.
MySQL 8.4SELECT IFNULL("SQL Academy", "Alternative SQL Academy") AS sql_trainer;
-
If the first argument is not equal to NULL, then it will be returned.
MySQL 8.4SELECT COALESCE('SQL Academy', 'Alternative SQL Academy') AS sql_trainer;
-
If the first argument is equal to NULL, then the value passed by the second argument will be returned.
MySQL 8.4SELECT IFNULL(NULL, "Alternative SQL Academy") AS sql_trainer;
-
If the first argument is equal to NULL, then the next non-NULL value will be returned.
MySQL 8.4SELECT COALESCE(NULL, 'Alternative SQL Academy') AS sql_trainer; -
COALESCE can accept multiple arguments, making the code very readable:
MySQL 8.4SELECT COALESCE(NULL, NULL, 'SQL Academy', 'Backup option') AS sql_trainer;
NULLIF function
The NULLIF function is useful when you need to replace a specific value with NULL. This can be helpful for filtering or processing "empty" values.
NULLIF syntax
NULLIF(value_1, value_2)- value_1
The value the function returns if it is not equal to value_2.
- value_2
The value to compare with. If value_1 equals it, the function returns NULL.
Examples with the NULLIF function
-
If the value of the first argument is equal to the value of the second argument, then NULL is returned.
MySQL 8.4SELECT NULLIF("SQL Academy", "SQL Academy") AS sql_trainer;
-
If the value of the first argument is equal to the value of the second argument, then NULL is returned.
MySQL 8.4SELECT NULLIF('SQL Academy', 'SQL Academy') AS sql_trainer;
-
If the values of the first and second arguments are different, then the value of the first argument is returned.
MySQL 8.4SELECT NULLIF("SQL Academy", "Alternative SQL Academy") AS sql_trainer;
-
If the values of the first and second arguments are different, then the value of the first argument is returned.
MySQL 8.4SELECT NULLIF('SQL Academy', 'Alternative SQL Academy') AS sql_trainer;
When to use each function:
- CASE: When you need complex conditional logic with multiple conditions
- COALESCE: When you need to replace NULL values with default values
- NULLIF: When you need to turn specific values into NULL
These functions make code more readable and are part of the SQL standard.