← CodePrimer
SQL
Intermediate
Database
COUNT with GROUP BY
How do I count rows per category?
GROUP BY aggregate.
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
SQL
CREATE DATABASE
How do I create a MySQL database?
SQL
ALTER TABLE add column
How do I add a column to an existing table?
SQL
Foreign key relationship
How do I relate posts to users?
SQL
LIKE search pattern
How do I search text with a keyword?
SQL
Basic transaction
How do I commit or roll back multiple writes?
SQL
WHERE with AND / OR
How do I combine filters in SQL?