How to Write a Case Statement in MySQL

Case statements are useful when you’re dealing with multiple IF statements in your select clause. It comes in two forms:

SELECT
  CASE
    WHEN score < 70 THEN 'failed'
    WHEN score BETWEEN 70 AND 80 THEN 'passed'
    WHEN score BETWEEN 81 AND 90 THEN 'very good'
    ELSE 'outstanding'
  END AS performance
FROM test_scores;

SELECT
  CASE grade
    WHEN 'A' THEN 'Excellent'
    WHEN 'B' THEN 'Good'
    WHEN 'C' THEN 'Needs Improvement'
    ELSE 'Failed'
  END AS grade_interpretation
FROM grades;