RienSQL
LESSON 24 / 62 · Conditional function IF

Conditional function IF

Conditional logic in SQL: IF function in MySQL and additional functions in PostgreSQL (COALESCE, NULLIF)

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)

The value IF returns if the condition is false or NULL.

condition
The logical expression being checked, for example price > 100.
value_if_true
The value IF returns if the condition is true.

Examples

Simple comparison of two numbers. Since 10 is not greater than 20, the function will return 'FALSE'.

SELECT IF(10>20, "TRUE", "FALSE");
IF(10>20, "TRUE", "FALSE")
FALSE

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".

idINTidINTuser_idINTroom_idINTidINTowner_idINTidINTreservation_idINT

SELECT id, price,
    IF(price >= 150, "Comfort class", "Economy class") AS category
    FROM Rooms
idpricecategory
1149Economy class
2225Comfort class
3150Comfort class
489Economy class
580Economy class
6200Comfort class
760Economy class
879Economy class
979Economy class
10150Comfort class
11135Economy class
1285Economy class
1389Economy class
1485Economy class
15120Economy class
16140Economy class
17215Comfort class
18140Economy class
1999Economy class
20190Comfort class
21299Comfort class
22130Economy class
2380Economy class
24110Economy class
25120Economy class
2660Economy class
2780Economy class
28150Comfort class
2944Economy class
30180Comfort class
3150Economy class
3252Economy class
3355Economy class
3450Economy class
3570Economy class
3689Economy class
3735Economy class
3885Economy class
39150Comfort class
4040Economy class
4168Economy class
42120Economy class
43120Economy class
44135Economy class
45150Comfort class
46150Comfort class
47130Economy class
48110Economy class
49115Economy class
5080Economy class

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
idpricecategory
1149Economy class
2225Business Class
3150Comfort class
489Economy class
580Economy class
6200Business Class
760Economy class
879Economy class
979Economy class
10150Comfort class
11135Economy class
1285Economy class
1389Economy class
1485Economy class
15120Economy class
16140Economy class
17215Business Class
18140Economy class
1999Economy class
20190Comfort class
21299Business Class
22130Economy class
2380Economy class
24110Economy class
25120Economy class
2660Economy class
2780Economy class
28150Comfort class
2944Economy class
30180Comfort class
3150Economy class
3252Economy class
3355Economy class
3450Economy class
3570Economy class
3689Economy class
3735Economy class
3885Economy class
39150Comfort class
4040Economy class
4168Economy class
42120Economy class
43120Economy class
44135Economy class
45150Comfort class
46150Comfort class
47130Economy class
48110Economy class
49115Economy class
5080Economy class

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 value being checked, for example a column that may contain NULL. If it is not NULL, the function returns it.

The value the function returns if value is NULL.

Examples with the IFNULL function

If the first argument is not equal to NULL, then it will be returned.

SELECT IFNULL("RianAcademy", "Alternative RianAcademy") AS sql_trainer;
sql_trainer
RianAcademy

If the first argument is equal to NULL, then the value passed by the second argument will be returned.

SELECT IFNULL(NULL, "Alternative RianAcademy") AS sql_trainer;
sql_trainer
Alternative RianAcademy

NULLIF syntax

NULLIF(value_1, value_2)

The value the function returns if it is not equal to 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.

SELECT NULLIF("RianAcademy", "RianAcademy") AS sql_trainer;
sql_trainer

If the values of the first and second arguments are different, then the value of the first argument is returned.

SELECT NULLIF("RianAcademy", "Alternative RianAcademy") AS sql_trainer;
sql_trainer
RianAcademy

Python Academy

Practice

Solve it in the editor. The query runs against a sample database in your browser.

Task 1Conditional string output
From the table Rooms display the identifiers of the rented accommodations (field id) and the presence of a TV in the room : if the TV is present print "YES", otherwise "NO".
To display the presence of a TV, use the has_tv alias.
SQL editor
SQLite
Ctrl / ⌘ + ↵
Result

Write a query and run it to see the results.

Task 2Replacing null with a string
From the table Teacher output the field first_name, the field middle_name and the field last_name. If the field middle_name is missing, output the value "Empty" in the field middle_name.
SQL editor
SQLite
Ctrl / ⌘ + ↵
Result

Write a query and run it to see the results.