Exercise 7/8 - Why my values become NULL



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;


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;


thank you very much!


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