CASE A_value
WHEN B_value THEN B_statement
[WHEN C_Value THEN C_statement] ...
[ELSE X_statement]
END CASE
SELECT `id` , `name` , `class` , `mark` , `gender` ,
CASE class
WHEN 'four' THEN '1st floor'
WHEN 'five' THEN '2nd floor'
WHEN 'three' THEN '2nd floor'
WHEN 'two' THEN '1st floor'
ELSE 'Ground floor'
END AS location
FROM `student`
id | name | class | mark | gender | location |
---|---|---|---|---|---|
1 | John Deo | Four | 75 | male | 1st floor |
2 | Max Ruin | Three | 85 | male | 2nd floor |
3 | Arnold | Three | 55 | male | 2nd floor |
4 | Krish Star | Four | 60 | male | 1st floor |
5 | John Mike | Four | 60 | male | 1st floor |
6 | Alex John | Four | 55 | male | 1st floor |
7 | My John Rob | Fifth | 78 | male | Ground floor |
8 | Asruid | Five | 85 | male | 2nd floor |
9 | Tes Qry | Six | 78 | male | Ground floor |
10 | Big John | Four | 55 | male | 1st floor |
SELECT `id` , `name` , `class` , `mark` , `gender` ,
CASE class
WHEN 'four' THEN '1st floor'
WHEN 'five' THEN '2nd floor'
WHEN 'three' THEN '2nd floor'
WHEN 'two' THEN '1st floor'
ELSE 'Ground floor'
END AS location
FROM `student` ORDER BY location
CASE
WHEN condition_to_check THEN statement
[WHEN condition_to_check THEN statement ...
[ELSE statement]
END CASE
Here we check the condition and if it is True then the respective statement is executed. SELECT `id` , `name` , `class` , `mark` , `gender` ,
CASE
WHEN mark >= 90 THEN 'A'
WHEN mark >= 80 THEN 'B'
WHEN mark >= 70 THEN 'C'
ELSE 'FAIL'
END AS grade
FROM `student`
id | name | class | mark | gender | grade |
---|---|---|---|---|---|
1 | John Deo | Four | 75 | male | C |
2 | Max Ruin | Three | 85 | male | B |
3 | Arnold | Three | 55 | male | FAIL |
4 | Krish Star | Four | 60 | male | FAIL |
5 | John Mike | Four | 60 | male | FAIL |
6 | Alex John | Four | 55 | male | FAIL |
7 | My John Rob | Fifth | 78 | male | C |
8 | Asruid | Five | 85 | male | B |
9 | Tes Qry | Six | 78 | male | C |
10 | Big John | Four | 55 | male | FAIL |
11 | Ronald | Six | 89 | male | B |
12 | Recky | Six | 94 | male | A |
13 | Kty | Seven | 88 | male | B |
While grouping in a rang it is better to use MySQL BETWEEN Query
SELECT `id` , `name` , `class` , `mark` , `gender` ,
CASE
WHEN mark BETWEEN 90 AND 100 THEN 'A'
WHEN mark BETWEEN 80 AND 89 THEN 'B'
WHEN mark BETWEEN 70 AND 79 THEN 'C'
ELSE 'FAIL'
END AS grade
FROM `student`
SELECT class, count(*) as Total,
sum(CASE WHEN gender ='male' THEN 1 ELSE 0 END) as Male,
sum(CASE WHEN gender ='Female' THEN 1 ELSE 0 END) as Female
FROM student group by class;
Output
class | Total | Male | Female |
---|---|---|---|
Eight | 1 | 1 | 0 |
Five | 3 | 3 | 0 |
Four | 9 | 4 | 5 |
Nine | 2 | 1 | 1 |
Seven | 10 | 5 | 5 |
Six | 7 | 2 | 5 |
Three | 3 | 2 | 1 |
SELECT CASE WHEN c_name IS NOT NULL THEN 'checked'
ELSE 'not_checked'
END as my_status from STUDENT
Here c_name is the column name storing the status of the record, checked or not.
Download the SQL dump of the above student table
Author
🎥 Join me live on YouTubePassionate about coding and teaching, I publish practical tutorials on PHP, Python, JavaScript, SQL, and web development. My goal is to make learning simple, engaging, and project‑oriented with real examples and source code.