COUNT() Aggregation With GROUP BY
Count how many movies each director has made using COUNT() grouped by director.
Overview
COUNT() tallies rows. Combined with GROUP BY, it becomes a frequency counter,
exactly what you need to answer "how many X per Y". Here we count movies per
director in the movies dataset.
The Query
Loading playground environment...
Step-by-Step Breakdown
COUNT(*)counts every row in each group (including rows withNULLcolumns).GROUP BY director: one row per director.ORDER BY movie_count DESCsurfaces the most prolific directors first.
COUNT(*) vs COUNT(column)
COUNT(*) counts rows; COUNT(release_year) counts only rows where that
column is non-null. They differ when the column can be NULL.
Variations
Count books per author in the library dataset:
Loading playground environment...
Common Mistakes
- Using
COUNT(column)expecting row counts. If the column hasNULLs you undercount; preferCOUNT(*)for "how many rows". - Forgetting GROUP BY. Without it,
COUNT(*)returns a single grand total. - Selecting non-grouped columns.
SELECT title, COUNT(*)without groupingtitleis invalid.
Cite this resource
SQLSimplified. "COUNT() Aggregation With GROUP BY Example". Available at: https://sqlsimplified.online/examples/group-by-count