Using MySQL 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.
Most of the queries in the tutorials need Northwind MySQL database, you can download the database script on this page.
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.
Most of the queries in the tutorials need Northwind MySQL database, you can download the database script on this page.
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
Most of the queries in the tutorials need Northwind MySQL database, you can download the database script on this page.
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
Most of the queries in the tutorials need Northwind MySQL database, you can download the database script on this page.
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
Most of the queries in the tutorials need Northwind MySQL database, you can download the database script on this page.
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
Most of the queries in the tutorials need Northwind MySQL database, you can download the database script on this page.
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
Most of the queries in the tutorials need Northwind MySQL database, you can download the database script on this page.
SELECT FirstName, LastName, IFNULL(ReportsTo, 'Is CEO')
FROM Employees;
Query result set - 9 rows returned:
Practice #4: Using NULLIF function
Most of the queries in the tutorials need Northwind MySQL database, you can download the database script on this page.
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:
Other tutorials in this category
1. Using String Functions, Part 1
2. Using String Functions, Part 2
3. Using String Functions, Part 3
4. Using MySQL Date and Time Functions, Part 1
5. Using MySQL Date and Time Functions, Part 2
6. Using MySQL Date and Time Functions, Part 3
7. Using MySQL Conversion Functions
8. Using MySQL Comparison Functions
9. Using Aggregate Functions in MySQL
10. MySQL RANK() function
11. MySQL DENSE_RANK() function
12. MySQL ROW_NUMBER() function
13. Compare RANK(), DENSE_RANK(), and ROW_NUMBER() function
14. Using LIMIT and OFFSET to Page Thru Query Result in MySQL
15. Using LIMIT, SQL_CALC_FOUND_ROWS and FOUND_ROWS() to Page Thru Query Result in MySQL
16. Use LEAST function to find the SMALLEST value from multiple arguments
17. How to make case-sensitive comparison in MySQL
18. Use GREATEST function to find the LARGEST value from multiple arguments
19. MySQL MID() function with examples
20. MySQL LOCATE() function with examples
21. MySQL SUBSTR() function with examples
22. MySQL POSITION() function with examples
23. How to use MySQL REPLACE function in SELECT and UPDATE queries
Back to Tutorial Index Page