The Metropolitan Museum of Art - Case Statement

Exercise 7, The Metropolitan Museum of Art

Hi Everyone,

For the exercise #7 in the link above, the recommended code under the hint section below,

SELECT CASE
WHEN medium LIKE ‘%gold%’ THEN ‘Gold’
WHEN medium LIKE ‘%silver%’ THEN ‘Silver’
ELSE NULL
END AS ‘Bling’,
COUNT(*)
FROM met
WHERE Bling IS NOT NULL
GROUP BY 1
ORDER BY 2 DESC;

would yield the following results is derived.

  • Silver - 293
  • Gold - 41

However, Within the count of 41 under Gold, there exists a record with ‘Gold, silver, platinum, black opals, boulder opals, demantoid garnets, rubies, enamel.’ under the ‘medium’ column, which contains both words Gold and Silver.

I am not quite sure how the above record got “thrown” into the Gold category despite having both Gold and Silver in it. Would anyone be able to shed some light on this?

Thanks in advance!

A CASE statement goes through the conditions and returns a value once it’s met then goes on to the next condition. So, in your query CASE WHEN medium LIKE '%gold%' THEN 'Gold'
“gold” is the first value in that description, so that condition is met.

It’s like an IF-ELSE statement in Python.

1 Like

Thanks @lisalisaj for the enlightenment! :smiley:

1 Like