SELECT with CASE or IF syntax in MySQL

Example of SELECT with CASE or IF syntax in MySQL

Example of using CASE with SELECT statement


SELECT name, population,
CASE WHEN population >= 1000000 THEN 'Over' ELSE 'Below' END AS 'Status Population'
 FROM city limit 5;

+----------------+------------+-------------------+
| name           | population | Status Population |
+----------------+------------+-------------------+
| Kabul          |    1780000 | Over              |
| Qandahar       |     237500 | Below             |
| Herat          |     186800 | Below             |
| Mazar-e-Sharif |     127800 | Below             |
| Amsterdam      |     731200 | Below             |
+----------------+------------+-------------------+
5 rows in set (0.00 sec)

Example of using IF with SELECT statement


SELECT name, population,
IF( population >= 1000000, 'Over', 'Below') AS 'Status Population'
FROM City limit 5;

+----------------+------------+-------------------+
| name           | population | Status Population |
+----------------+------------+-------------------+
| Kabul          |    1780000 | Over              |
| Qandahar       |     237500 | Below             |
| Herat          |     186800 | Below             |
| Mazar-e-Sharif |     127800 | Below             |
| Amsterdam      |     731200 | Below             |
+----------------+------------+-------------------+
5 rows in set (0.00 sec)

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.