Difference between COUNT() and SUM()?


I used the following two queries for calculating the percentage of airports with elevation>=2000 by states and it seems to give me the same results. Could anyone help to explain what the difference between them is OR is it similar?

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

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

The only difference is I used NULL for the CASE statement in COUNT whereas I used 0 for SUM.


For such a question you could use a google search
== discussions / opinions ==
sql sum() vs count() site:stackoverflow.com

== the Book ==
sum() site:oracle.com
count() site:oracle.com


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