How do I get a count of comments by querying the database table?
I think you can do it using the following query:

SELECT count(comments.cid), node.nid
FROM node
INNER JOIN comments on node.nid = comments.nid
WHERE node.type='forum'
GROUP BY node.nid