FAQ: Queries - Case

This community-built FAQ covers the “Case” exercise from the lesson “Queries”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Web Development
Data Science

Learn SQL

FAQs on the exercise Case

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

How or where do I input ORDER BY, as I’d like to see Chill first then Intense?

Insert the line after FROM movies and end that line with the semi-colon, not this one.

Thanks but it’s not working. Can’t get all the "Chills’ together. This seems to always sort by genre:

SELECT name,

 CASE
  WHEN genre = 'romance' THEN 'Chill'
  WHEN genre = 'comedy'  THEN 'Chill'
  ELSE 'Intense'
 END AS 'Mood'

FROM movies
WHERE genre IS NOT NULL 
ORDER BY 'Mood';

If it is using the genre to order rows, how can I bring up ‘comedy’ then ‘romance’ then the rest in that order?

Column headings are not strings.

ORDER BY Mood;

It is not in this instance. The two columns are name and Mood.

If we wanted the names to be sorted as well as the mood,

ORDER BY Mood, name;

First, examine the Schema

Database Schema
movies 230 rows
id	INTEGER
name	TEXT
genre	TEXT
year	INTEGER
imdb_rating	REAL

If we wish to sort by genre, then we need to include that column in the selection.

SELECT name, genre,

and in the sort line,

ORDER BY Mood, genre, name;

Hi,

I wonder why there has to be a comma after ‘SELECT name’ from the code below.
And also curious about why ‘FROM movie’ has to be at the end.

SELECT name,
CASE
WHEN imdb_rating > 8 THEN ‘Fantastic’
WHEN imdb_rating > 6 THEN ‘Poorly Received’
ELSE ‘Avoid at All Costs’
END AS ‘Review’
FROM movies;

Thanks,
Sihyeon

Because it separates the name from the CASE clause, which is also a parameter of the SELECT clause. Everything before FROM is part of SELECT.

Aha! Thank you soooo much!:slight_smile:

1 Like

In this example, is it not possible to use OR along with the CASE statement. Something like below.
SELECT name,
CASE
WHEN genre = ‘romance’ OR genre = ‘comedy’ THEN ‘Chill’
ELSE ‘Intense’
END AS ‘Mood’
FROM movies;

Hi, I’m very curious, why this code:
SELECT name,
CASE
WHEN imdb_rating > 8 THEN ‘Fantastic’
WHEN imdb_rating > 6 THEN ‘Poorly Received’
ELSE ‘Avoid at All Costs’
END
FROM movies;

print a column with “CASE WHEN imdb_rating > 8 THEN 'Fantastic’ WHEN imdb_rating > 6 THEN ‘Poorly Received’ ELSE 'Avoid at All Costs’ END AS ‘Review’ “ as column name?