SELECT c.`categoryId`, c.`name`, count(DISTINCT ec.`entryId`) as entryCount FROM `Category` as c LEFT JOIN `EntryCategory` as ec ON c.`categoryId` = ec.`categoryId` GROUP BY c.`categoryId`, c.`name` ORDER BY c.`name`