Using Control Flow Functions
There are four Control Flow functions in MySQL - CASE operator, IF/ELSE construct, IFNULL, and NULLIF.
Control Flow functions return a value for each row processed. The returned value is a result of comparison or condition evaluated.
Control Flow functions can be used in SELECT, WHERE, ORDER BY, GROUP BY (will be covered in aggregate functions section) clause.
| No. |
Name |
Description |
| 1 |
CASE |
CASE is probably more suitable to be called CASE statement rather than CASE function.
There are two versions of using CASE statement:
Version 1:
CASE value WHEN [compare_value] THEN result [WHEN [compare_value] THEN result ...] [ELSE result] END
In this version, result is returned when value is equal to compare_value.
If nothing is matched, the result after ELSE is returned, or NULL is returned if there is no ELSE part.
Practice #1-1: Get each supplier's continent from Suppliers table based on the supplier's country.
Practice #1-2: Get each supplier's continent from Suppliers table and use the CASE statement's column alias in ORDER BY clause.
Version 2:
CASE WHEN [condition] THEN result [WHEN [condition] THEN result ...] [ELSE result] END
In this version, if condition is true, result is returned.
If nothing is matched, the result after ELSE is returned, or NULL is returned if there is no ELSE part.
Version 2 returns the same result as Version 1 when condition is for equal comparison (=). See Practice #1-3 which returns the same result as Practice #1-1.
condition can use any of the comparison operators covered in Using Comparison Operators, Part I and Part II.
See Practice #1-4 where IN and = operator are used.
|
| 2 |
IF(expr1,expr2,expr3) |
IF function accepts three arguments and the result is returned based on if expr1 is TRUE.
If expr1 is evaluated to TRUE, the function returns expr2. Otherwise, expr3 is returned. See Practice #2-1.
IF() function can also be used in ORDER BY clause. See Practice #2-2.
|
| 3 |
IFNULL(expr1,expr2) |
If expr1 is not NULL, the function returns expr1. Otherwise it returns expr2. See Practice #3.
IFNULL() returns a numeric or string value, depending on the context in which it is used.
|
| 4 |
NULLIF(expr1,expr2) |
Returns NULL if expr1 = expr2 is true, otherwise returns expr1.
This is the same as
CASE WHEN expr1 = expr2 THEN NULL ELSE expr1 END
See Practice #4.
|
Practice #1-1: Using CASE statement to get the supplier's continent based on its country.
In Firefox (not IE), copy and paste the following SQL to your SQLyog query
window. To execute a query, move your cursor anywhere inside the query and then
press F9. Note that the SQL needs to end with semi-colon.
SELECT CompanyName,
CASE Country WHEN 'USA' THEN 'North America'
WHEN 'Canada' THEN 'North America'
WHEN 'Brazil' THEN 'South America'
WHEN 'Japan' THEN 'Asia'
WHEN 'Singapore' THEN 'Asia'
WHEN 'Australia' THEN 'Australia'
ELSE 'Europe' END AS Continent
FROM Suppliers
ORDER BY CompanyName;
Query result set - 29 rows returned:

Practice #1-2: Using CASE statement to get the supplier's continent and order result by using column alias.
In Firefox (not IE), copy and paste the following SQL to your SQLyog query
window. To execute a query, move your cursor anywhere inside the query and then
press F9. Note that the SQL needs to end with semi-colon.
SELECT CompanyName,
CASE Country WHEN 'USA' THEN 'North America'
WHEN 'Canada' THEN 'North America'
WHEN 'Brazil' THEN 'South America'
WHEN 'Japan' THEN 'Asia'
WHEN 'Singapore' THEN 'Asia'
WHEN 'Australia' THEN 'Australia'
ELSE 'Europe' END AS Continent
FROM Suppliers
ORDER BY Continent;
Query result set - 29 rows returned:

Practice #1-3: Using the second version of CASE statement
In Firefox (not IE), copy and paste the following SQL to your SQLyog query
window. To execute a query, move your cursor anywhere inside the query and then
press F9. Note that the SQL needs to end with semi-colon.
SELECT CompanyName,
CASE WHEN Country = 'USA' THEN 'North America'
WHEN Country = 'Canada' THEN 'North America'
WHEN Country = 'Brazil' THEN 'South America'
WHEN Country = 'Japan' THEN 'Asia'
WHEN Country = 'Singapore' THEN 'Asia'
WHEN Country = 'Australia' THEN 'Australia'
ELSE 'Europe' END AS Continent
FROM Suppliers
ORDER BY CompanyName;
Query result set - 29 rows returned:

Practice #1-4: Using the second version of CASE statement
In Firefox (not IE), copy and paste the following SQL to your SQLyog query
window. To execute a query, move your cursor anywhere inside the query and then
press F9. Note that the SQL needs to end with semi-colon.
SELECT CompanyName,
CASE WHEN Country IN ('USA', 'Canada') THEN 'North America'
WHEN Country = 'Brazil' THEN 'South America'
WHEN Country IN ('Japan', 'Singapore') THEN 'Asia'
WHEN Country = 'Australia' THEN 'Australia'
ELSE 'Europe' END AS Continent
FROM Suppliers
ORDER BY CompanyName;
Query result set - 29 rows returned:

Practice #2-1: Using IF control flow function
In Firefox (not IE), copy and paste the following SQL to your SQLyog query
window. To execute a query, move your cursor anywhere inside the query and then
press F9. Note that the SQL needs to end with semi-colon.
SELECT ProductName,
UnitPrice,
IF(UnitPrice>=20, 'High Price', 'Low Price') AS "Price Level"
FROM Products;
Query result set - 77 rows returned:

Practice #2-2: Using IF control flow function in ORDER BY clause
In Firefox (not IE), copy and paste the following SQL to your SQLyog query
window. To execute a query, move your cursor anywhere inside the query and then
press F9. Note that the SQL needs to end with semi-colon.
SELECT FirstName, LastName
FROM Employees
ORDER BY IF(UNIX_TIMESTAMP()%2=0, FirstName, LastName);
Query result set - 9 rows returned and ordered by LastName:

Query result set - 9 rows returned and ordered by FirstName:

Practice #3: Using IFNULL function
In Firefox (not IE), copy and paste the following SQL to your SQLyog query
window. To execute a query, move your cursor anywhere inside the query and then
press F9. Note that the SQL needs to end with semi-colon.
SELECT FirstName, LastName, IFNULL(ReportsTo, 'Is CEO')
FROM Employees;
Query result set - 9 rows returned:

Practice #4: Using NULLIF function
In Firefox (not IE), copy and paste the following SQL to your SQLyog query
window. To execute a query, move your cursor anywhere inside the query and then
press F9. Note that the SQL needs to end with semi-colon.
SELECT CompanyName,
Fax,
NULLIF(IF(Fax='', 'No Fax', 'Got Fax'), 'No Fax') AS Note
FROM Customers;
SELECT CompanyName,
Fax,
CASE WHEN IF(Fax='', 'No Fax', 'Got Fax')='No Fax' THEN NULL
ELSE IF(Fax='', 'No Fax', 'Got Fax')
END AS Note
FROM Customers;
Query result set - 91 rows returned:
