How the IF() function works in Mariadb?

The IF() function is a conditional function that returns a value based on a given condition.

Posted on

The MariaDB IF() function is used to return a value based on a condition. This function is particularly useful in SQL queries where the output needs to be dynamically determined based on the evaluation of a boolean expression. It can be used in SELECT statements, WHERE clauses, and anywhere an expression is valid.

Syntax

The syntax for the MariaDB IF() function is as follows:

IF(expression, value_if_true, value_if_false)
  • expression: A boolean expression that is evaluated.
  • value_if_true: The value returned if expression evaluates to true.
  • value_if_false: The value returned if expression evaluates to false.

Examples

Example 1: Simple Condition

This example demonstrates how to use the IF() function to evaluate a simple condition.

SELECT IF(100 > 10, 'Yes', 'No');

Output:

+---------------------------+
| IF(100 > 10, 'Yes', 'No') |
+---------------------------+
| Yes                       |
+---------------------------+

This statement outputs “Yes” because the condition 100 > 10 is true.

Example 2: Using Variables

This example shows the IF() function with variables.

SET @a := 20;
SELECT IF(@a < 30, 'Less than 30', '30 or more');

Output:

+-------------------------------------------+
| IF(@a < 30, 'Less than 30', '30 or more') |
+-------------------------------------------+
| Less than 30                              |
+-------------------------------------------+

The variable @a is less than 30, so the output is “Less than 30”.

Example 3: Nested IF()

This example uses nested IF() functions to handle multiple conditions.

SELECT IF(10 > 20, 'First', IF(10 > 5, 'Second', 'Third'));

Output:

+-----------------------------------------------------+
| IF(10 > 20, 'First', IF(10 > 5, 'Second', 'Third')) |
+-----------------------------------------------------+
| Second                                              |
+-----------------------------------------------------+

The first condition is false, so it evaluates the second condition, which is true, resulting in “Second”.

Example 4: With Table Data

This example involves a table to demonstrate the IF() function with actual data.

DROP TABLE IF EXISTS employees;
CREATE TABLE employees (
    id INT,
    name VARCHAR(50),
    salary INT
);
INSERT INTO employees (id, name, salary) VALUES (1, 'John', 5000), (2, 'Jane', 6000);

SELECT name, IF(salary > 5500, 'Above Average', 'Below Average') AS SalaryStatus FROM employees;

Output:

+------+---------------+
| name | SalaryStatus  |
+------+---------------+
| John | Below Average |
| Jane | Above Average |
+------+---------------+

The IF() function is used to return whether each employee’s salary is above or below average.

Example 5: In a WHERE Clause

This example shows how to use the IF() function within a WHERE clause.

SELECT * FROM employees WHERE IF(salary > 5500, TRUE, FALSE);

Output:

+------+------+--------+
| id   | name | salary |
+------+------+--------+
|    2 | Jane |   6000 |
+------+------+--------+

Only the employees with a salary greater than 5500 are returned.

Here are a few functions related to the MariaDB IF() function:

  • MariaDB IFNULL() function is used to return the specified value if the expression is NULL, otherwise return the expression.
  • MariaDB CASE statement is used for more complex conditional logic, similar to if-else statements in programming languages.
  • MariaDB COALESCE() function returns the first non-null value in a list.

Conclusion

The IF() function in MariaDB is a versatile tool that allows for conditional logic directly within SQL queries. Understanding how to use this function effectively can greatly enhance the flexibility and readability of your database operations. Remember to consider the performance implications when using functions like IF() in large-scale databases or complex queries.