Most posts have NULL
in the CommentCount
field
The CommentCount < 30
clause explicitly filters out rows where CommentCount is NULL
. (This is because NULL
represents the absence of data so logical operators involving NULL
always return false.) Thankfully, SQL flavors offer ways to treat NULL
as something else (usually zero):
select isnull(CommentCount, 0), count(*)
from Posts
group by isnull(CommentCount, 0)
order by isnull(CommentCount, 0)
This results in a more likely looking distribution:
What does NULL
mean in CommentCount
?
By comparing the posts with 0
comments to those with NULL
, it seems that the row is created with a null CommentCount
field. As comments are added, the field is updated to reflect the number of comments on the post. When comments are removed, the field is decremented. When all comments are removed the field becomes 0
. So:
NULL
—No comments have ever been added to the post.
0
—Comments have been added to the post, but they've all been removed.