Published May 7, 2021Updated Jul 13, 2022
Contribute to Docs
CASE command is a logical test that returns different output based on the conditions of each statement and closes with an
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.
The following example showcases the
CASE command returning output based on several conditions:
SELECT student_name AS 'Student',overall_gpa AS 'GPA',CASEWHEN 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."ENDFROM students;
Looking to contribute?
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn SQL on Codecademy
Analyze Data with SQLLearn to analyze data with SQL and prepare for technical interviews.
Includes 8 Courses
Design Databases With PostgreSQLLearn how to query SQL databases and design relational databases to efficiently store large quantities of data.
Includes 5 Courses