SQL / MySQL
Intermediate
Database
· 1 min read
COUNT with GROUP BY
How do I count rows per category?
GROUP BY aggregate.
Example
SELECT course, COUNT(*) AS total
FROM enrollments
GROUP BY course
ORDER BY total DESC;
Line-by-Line Walkthrough
Every selected non-aggregate column must appear in GROUP BY.