MySQL CASE Statement

The case statement in MySQL is used to test a value for equality against a list of values/conditions.
Following is the syntax of the CASE statement −
CASE value
   WHEN [compare_value] THEN result
   [WHEN [compare_value] THEN result ...]
   [ELSE result]
END CASE
Or,
CASE
   WHEN [condition] THEN result
   [WHEN [condition] THEN result ...]
   [ELSE result]
END CASE

Following query demonstrates the usage of the MySQL CASE statement in a procedure −
DELIMITER //
CREATE PROCEDURE case_example(IN degree VARCHAR(20), OUT full_form Varchar(50))
   BEGIN
      CASE degree WHEN 'B-Tech' THEN
         SET full_form = 'Bachelor of Technology';
         WHEN 'M-Tech' THEN SET full_form = 'Master of Technology';
         WHEN 'BSC' THEN SET full_form = 'Bachelor of Science';
         WHEN 'MSC' THEN
         SET full_form = 'Master of Science';
         ELSE
         SET full_form = 'Irrelevant';
      END CASE ; 
   END //
DELIMITER ;
You can call the above procedure using various input values as follows −
mysql> DELIMITER ;
mysql> CALL case_example ('B-Tech', @test);
Query OK, 0 rows affected (0.13 sec)

mysql> SELECT @test;
+------------------------+
| @test                  |
+------------------------+
| Bachelor of Technology |
+------------------------+
1 row in set (0.00 sec)
mysql> CALL case_example ('M-Tech', @test);
Query OK, 0 rows affected (0.00 sec)
mysql> SELECT @test;
+----------------------+
| @test                |
+----------------------+
| Master of Technology |
+----------------------+
1 row in set (0.00 sec)

mysql> CALL case_example ('BSC', @test);
Query OK, 0 rows affected (0.00 sec)
mysql> SELECT @test;
+---------------------+
| @test               |
+---------------------+
| Bachelor of Science |
+---------------------+
1 row in set (0.00 sec)

mysql> CALL case_example ('MSC', @test);
Query OK, 0 rows affected (0.00 sec)

mysql> SELECT @test;
+-------------------+
| @test             |
+-------------------+
| Master of Science |
+-------------------+
1 row in set (0.00 sec)
MySQL also have a CASE operator which is similar to the CASE statement the only difference is that the ELSE NULL clause is not allowed in the CASE statement and it is terminated by END CASE. Whereas, the operator is terminated by END.
Assume we have created a table named student_info as follows −
CREATE TABLE student_info (ID INT, NAME VARCHAR(25), SAL INT, EDUCATION VARCHAR(25));
INSERT INTO student_info (ID, NAME, SAL, EDUCATION) VALUES (101, 'Raju', 2254, 'B-Tech');
INSERT INTO student_info (ID, NAME, SAL, EDUCATION) VALUES (101, 'Raju', 2254, 'M-Tech');
INSERT INTO student_info (ID, NAME, SAL, EDUCATION) VALUES (101, 'Raju', 2254, 'BSC');
INSERT INTO student_info (ID, NAME, SAL, EDUCATION) VALUES (101, 'Raju', 2254, 'MSC');
If you verify the contents of the EMP table you can observe its contents as −
mysql> select * from student_info;
+------+------+------+-----------+
| ID   | NAME | SAL  | EDUCATION |
+------+------+------+-----------+
| 101  | Raju | 2254 | B-Tech    |
| 101  | Raju | 2254 | M-Tech    |
| 101  | Raju | 2254 | BSC       |
| 101  | Raju | 2254 | MSC       |
+------+------+------+-----------+
4 rows in set (0.00 sec)
Following query displays the contents of the above table along with the full forms of the degrees using the CASE Operator −
SELECT ID, NAME, SAL, EDUCATION,
CASE EDUCATION
   WHEN 'B-Tech' THEN 'Bachelor of Technology' 
   WHEN 'M-Tech' THEN 'Master of Technology' 
   WHEN 'BSC' THEN 'Bachelor of Science'
   WHEN 'MSC' THEN 'Master of Science'
   ELSE 'Irrelevant'
END AS FullFORM FROM student_info;
This will produce the following result −
+------+------+------+-----------+------------------------+
| ID   | NAME | SAL  | EDUCATION | FullFORM               |
+------+------+------+-----------+------------------------+
| 101  | Raju | 2254 | B-Tech    | Bachelor of Technology |
| 101  | Raju | 2254 | M-Tech    | Master of Technology   |
| 101  | Raju | 2254 | BSC       | Bachelor of Science    |
| 101  | Raju | 2254 | MSC       | Master of Science      |
+------+------+------+-----------+------------------------+
4 rows in set (0.00 sec)