Exercise 7/8 - Why my values become NULL


#1


https://www.codecademy.com/courses/sql-table-transformation/lessons/conditional-aggregates/exercises/combining-aggregates-ii?action=lesson_resume&link_content_target=interstitial_lesson

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.




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;


#3

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.


#4

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;


#5

thank you very much!


#6

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