Exercise 4.05 – writing case statements
Your company wants to run analysis on the country data of the world database to determine the size of the countries. They have asked you to create a query that categorizes countries based on the following criteria:
- If a country has a population under 100,000, it is small.
- If a country has a population between 100,000 and 500,000, it is medium.
- In all other cases, the country is large.
To achieve this, we can use a case statement. Here are the steps to write the query:
- Open MySQL Workbench and create a new query window.
- First, it is helpful to determine the cases that our query has. There are three cases to consider:
- WHEN population < 100,000, THEN
'small'
- WHEN population < 500,000, THEN
'medium'
- ELSE
'large'
- WHEN population < 100,000, THEN
- Next, we will put these cases into a formal case statement. This will give us the following query:
SELECT Name, CASE WHEN population < 100000 THEN 'small...