Question : simple MS SQL count query


How do I write a query that will list each name and the number of comments from the comment table associated with each?

Example Data Below:

Table "names"

NameID      name
1               Mark
2             Jan
3             Fred


Table "comments"
CommentID      NameID            Comment
1                   1                   Yes
2                   1                   No
3                   3                   Yes
4                   3                   Yes


Example output:

name      Comments
Mark             2
Jan             0
Fred             2

Answer : simple MS SQL count query

On second thoughts, this is a better way to do it:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
SELECT  n.NameID,
        n.NAME NAME,
        n.age age,
        c,
        Cmments
FROM    Names n
        LEFT JOIN (SELECT   NameID,
                            COUNT(*) Comments
                   FROM     Comments
                   GROUP BY n.NameID
                  ) c ON n.NameID = n.NameID
Random Solutions  
 
programming4us programming4us