I have two tables, Snippets and Comments. Snippets has an Id column and Comments has a SnippetId column.
I want to count the number of comments for each snippet.
I tried this:
SELECT
S.Id,
S.Title,
COUNT(*) AS CommentCount
FROM
Snippets S,
Comments C
WHERE
S.Id = C.SnippetId
but it does not work. It returns the total number of comments, and only returns one row.
I want to have a result like this:
Id | Title | CommentCount
1 | Test | 314
2 | Test2 | 42
How can I achieve this?
You almost have it correct; you’re just missing a
GROUP BYclause: