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(conditional_expression, value_1, value_2);
If the conditional expression passed as the first argument to the IF function is true, the function will return the value of the second argument value_1, otherwise the value of the third argument value_2 is returned.
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'.
SELECT IF(10>20, "TRUE", "FALSE");
-
Simple example of conditional logic using the CASE operator from the previous lesson:
SELECT 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".
SELECT 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:
SELECT 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.
SELECT id, price, IF(price >= 200, "Business Class", IF(price >= 150, "Comfort class", "Economy class")) AS category FROM Rooms
-
CASE is perfect for multiple conditions:
SELECT 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);
The IFNULL function returns the value passed by the first argument if it is not equal to NULL, otherwise it returns the alternative_value.
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(value1, value2, ..., valueN);
This is much more convenient than writing long CASE expressions to handle NULL.
Comparison of approaches
Using CASE:
CASE WHEN value1 IS NOT NULL THEN value1 WHEN value2 IS NOT NULL THEN value2 ELSE value3 END
Using COALESCE (much simpler):
COALESCE(value1, value2, value3)
-
If the first argument is not equal to NULL, then it will be returned.
SELECT IFNULL("SQL Academy", "Alternative SQL Academy") AS sql_trainer;
-
If the first argument is not equal to NULL, then it will be returned.
SELECT 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.
SELECT 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.
SELECT COALESCE(NULL, 'Alternative SQL Academy') AS sql_trainer;
-
COALESCE can accept multiple arguments, making the code very readable:
SELECT 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);
The NULLIF function returns NULL if value_1 is equal to value_2, otherwise it returns value_1.
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.
SELECT 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.
SELECT 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.
SELECT 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.
SELECT 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.