MySQL CASE

Last Updated: March 15, 2022

MySQL CASE statement is a part of the control flow like if then else statement

CASE Syntax

CASE
    WHEN condition1 THEN result1
    WHEN condition2 THEN result2
    WHEN conditionN THEN resultN
    ELSE result
END; 

When the condition is met you can get the result out. If no condition is met you can get the result after ELSE statement

Example

Display student grades according to the marks

SELECT name, marks,
CASE
    WHEN marks > 75 THEN 'Grade A'
    WHEN marks = 30 THEN 'Grade B'
    ELSE 'Grade C'
END AS Grade
FROM students; 
Test