Troubleshooting CASE statements
In the previous exercise, you may have noticed several null
values in our case statement, which can indicate there is an issue with the code.
In these instances, it's worth investigating to understand why these null
values are appearing. In this exercise, you will go through a series of steps to identify the issue and make changes to the code where necessary.
This exercise is part of the course
Reporting in SQL
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
-- Query from last exercise shown below. Comment it out.
SELECT
sport,
CASE WHEN weight/height^2*100 <.25 THEN '<.25'
WHEN weight/height^2*100 <=.30 THEN '.25-.30'
WHEN weight/height^2*100 >.30 THEN '>.30' END AS bmi_bucket,
COUNT(DISTINCT athlete_id) AS athletes
FROM summer_games AS s
JOIN athletes AS a
ON s.athlete_id = a.id
GROUP BY sport, bmi_bucket
ORDER BY sport, athletes DESC;
-- Show height, weight, and bmi for all athletes
SELECT
____,
____,
____ AS bmi
FROM ____
-- Filter for NULL bmi values
WHERE ____;