You will learn how to use the IF() function to return a value if a condition is true or false.
This function returns the specified value if the condition is TRUE and another value if the condition is FALSE. The syntax of the IF()function is shown below:
⚠️ The expression must be a Boolean expression.
In this example, we have the test_result table. We want to know which participants passed and which failed from the table below:
The above query will display the following table:
1) IF function in the query below states that IF the score is equal to or greater than 500, then return “PASSED“. Otherwise, if the score is smaller than 500, return “NOT PASSED”.
2) It will return the following result:
In the second example, we have another table named “deptcost”. We want to know which department exceeded the budget and which one did not from the following table.
Run the following query to display the table:
We have deptcost table as seen below:
1) The following IF function states that IF the actual is less than the budget, then return the budget difference, otherwise return 0.
2) We get the following result using the IF() function: