Boolean Functions
IF Function
Overview
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.Examples
Case #1: IF()
with a table
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:
- 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”.
- It will return the following result:
Case #2: IF() with expressions as return value
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:
- The following IF function states that IF the actual is less than the budget, then return the budget difference, otherwise return 0.
- We get the following result using the
IF()
function: