Exercise 7/8 - Why my values become NULL

<PLEASE USE THIS TEMPLATE TO HELP YOU CREATE A GREAT POST!>

<Below this line, add a link to the EXACT exercise that you are stuck at.>
https://www.codecademy.com/courses/sql-table-transformation/lessons/conditional-aggregates/exercises/combining-aggregates-ii?action=lesson_resume&link_content_target=interstitial_lesson

<Below this line, in what way does your code behave incorrectly? Include ALL error messages.>

I am trying to understand why this code either brings value of 100 or NULL. What am I not getting right? By the way I already know the correct answer but just trying to understand how this code is causing the non 100% results become NULL. Thanks.

<In this course, it often helps to include a screenshot of your whole web browser – that lets everyone see what you see. If you wish to include a screenshot, add it below this line.>

<If you wish to copy/paste in your code, you can use this next section. This will allow others to copy/paste your code for testing – something that they won’t be able to do with just a screenshot.>

```

SELECT state,
100.00 * (CASE WHEN elevation >= 2000 THEN count(elevation) END / count(*)) as percentage_high_elevation_airports
from airports
group by 1
order by 1;

<do not remove the three backticks above>

I think it’s because you don’t have an ELSE statement before END, so SQL automatically fills it with NULL if the CASE WHEN elevation >= 2000 is not met.

SELECT state,
100.0 * COUNT(CASE WHEN elevation >= 2000 THEN 1 ELSE NULL END) / COUNT(*) AS percentage_high_elevation_airports
FROM airports
GROUP BY state;

3 Likes

thank you very much!

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