IF
On this page
If the first argument is true, returns second argument.
Syntax
IF(condition, value_if_true, value_if_false)
Arguments
-
Any SQL objects
Return Type
Returns a value based on a specified condition.
Examples
Create a table and insert values:
CREATE TABLE orders(comp_id INT(10), comp_name VARCHAR(50),order_number INT(10), order_total INT(10));INSERT INTO orders VALUES("64","Wolf-Conn","4722","9"),("924","Blick, Von and Lynch","9610","52"),("487","Sipes-Bauch","5180","14"),("835","Shields-Kiehn","7961","684"),("476","Bogisich, Brown and Hessel","4687","482"),("809","Cole-Durgan","9358","467"),("232","Towne LLC","3774","445");
Create a query using the IF
function:
SELECT order_number,comp_id,order_total,IF(order_total > 100, 'Yes', 'No') AS amount_greater_than_100FROM orders;
+--------------+---------+-------------+-------------------------+
| order_number | comp_id | order_total | amount_greater_than_100 |
+--------------+---------+-------------+-------------------------+
| 4687 | 476 | 482 | Yes |
| 9358 | 809 | 467 | Yes |
| 5180 | 487 | 14 | No |
| 3774 | 232 | 445 | Yes |
| 7961 | 835 | 684 | Yes |
| 4722 | 64 | 9 | No |
| 9610 | 924 | 52 | No |
+--------------+---------+-------------+-------------------------+
Last modified: March 13, 2024