CASE

The CASE command is a logical test that returns different output based on the conditions of each statement and closes with an END clause.

Syntax

CASE
  WHEN this_condition THEN this_result
  WHEN that_condition THEN that_result
  ELSE fallback_result
END;

The result will come from the first WHEN .. THEN ... statement that evaluates as “True”. If none of these statements are “True”, a fallback_result from the ELSE clause will be returned. If there is no ELSE clause and none of the WHEN .. THEN ... statements evaluate to “True”, NULL is returned.

Example

The following example showcases the CASE command returning output based on several conditions:

SELECT student_name AS 'Student',
overall_gpa AS 'GPA',
CASE
WHEN overall_gpa > 3.0 THEN "Exceptional grades, keep up the good work!"
WHEN overall_gpa BETWEEN 2.0 AND 3.0 THEN "Good job! Study hard this term!"
ELSE "You're at risk of academic probation, seek help if needed."
END
FROM students;

Contributors

Interested in helping build Docs? Read the Contribution Guide or share your thoughts in this feedback form.

Learn SQL on Codecademy