Difference between NULL and 0 7/8 - conditional aggregates


#1

Hello!

As the title suggests, I was stuck at exercise 7/8 under conditional aggregates, this was my code :

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

With the above code I got an output wherein all the values under percentage_high_elevation_airports were equal to a 100 which was obviously incorrect so I formatted my code to replace the 0 with NULL like so :

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

And obtained the correct output.

So my question is :
What is the difference between NULL and 0 in SQL?

Thanks for the reply.


#2

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