I\'m trying to write an aggregate query in SQL which returns the count of all records joined to a given record in a table; If no records were joined to the given record, the
Don't forget to use DISTINCT in case you will LEFT JOIN more than one table:
SELECT m.MESSAGEID, COUNT(DISTINCT mp.MESSAGEID) FROM MESSAGE m LEFT JOIN MESSAGEPART mp ON mp.MESSAGEID = m.MESSAGEID GROUP BY m.MESSAGEID;