← CodePrimer
SQL Intermediate Database

COUNT with GROUP BY

How do I count rows per category?

GROUP BY aggregate.

Sample
SELECT course, COUNT(*) AS total
FROM enrollments
GROUP BY course
ORDER BY total DESC;

Notes

Every selected non-aggregate column must appear in GROUP BY.

Keywords: count · group by · aggregate

Related

Need more coins?

You do not have enough coins for this action.

Your balance: 0 coins

Share