Code Editor

Query Results

Run a query to see results.

Database Schema

Schema undefined.
Learn
Queries

Case

A CASE statement allows us to create different outputs (usually in the SELECT statement). It is SQL's way of handling if/then logic.

Suppose we want to condense the ratings in movies to just 'Good' and 'Bad':

SELECT name, CASE WHEN imdb_rating > 7 THEN 'Good' WHEN imdb_rating > 5 THEN 'Okay' END FROM movies;
  • Each WHEN tests a condition.

  • The following THEN gives us the string if the condition is true.

  • CASE statement must end with END.

Here, we set all movies above 8 as 'Good' and movies above 5 'Okay'.

Report a Bug
If you see a bug or any other issue with this page, please report it here.