Even though the CASE expression seems to remind us of imperative structures such as IF, SWITCH, and the like, it still does not allow for program flow control like those imperative structures, but rather allows for declarative evaluation of values based on certain conditions. Let's have a look at the following example in order to better understand this feature.
Please enter the following query in the SQL tab of the test database via the phpMyAdmin interface:
SELECT id, COUNT(*) as Total, COUNT(CASE WHEN superior IS NOT NULL THEN id END) as 'Number of superiors'FROM employeesWHERE id = 2;
This query should yield the following result set:
As the result shows, the row with an id value ...