> : A Question

Working on this page:
https://www.codecademy.com/paths/data-science/tracks/sql-basics/modules/dspath-sql-queries/lessons/queries/exercises/case

I wanted to ask a question about this example:

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

Why in this example doesn’t everything above 6-including everything above 8-all resolve to ‘Poorly Received’

I finished the exercise, just curious as to whether > happens to end at the next WHEN…or something like that.

Thanks! :v:t3:

Hi there.

It’s because SQL evaluates the CASE statement for each row by comparing the WHEN clauses in order and stops as soon as it finds a match.

So, if you have a row where imdb_rating is 9, SQL will match your first WHEN statement and return “Fantastic”. It doesn’t then re-evaluate the same row to check if it matches the second WHEN statement.

1 Like

Ah okay! That makes sense. Thanks! Good to know…

1 Like

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.