Query Exercise: Find Posts with the Wrong CommentCount


For 2024, I’m trying something new: weekly homework challenges! For this week’s query challenge, we’re going to do some data validation. Pop open the Stack Overflow database and check out the CommentCount column on the Posts table:

In theory, that’s a fast way to check how many Comments a particular Post has, without querying the Comments table to find them all. For example, if I want to see the Comments for Post.Id 13, I might write:

SELECT * FROM dbo.Comments WHERE PostId = 13;

And presto, I get the 6 comments, matching the CommentCount = 6 value in the above