Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions episodes/02-sql-aggregation.md
Original file line number Diff line number Diff line change
Expand Up @@ -226,11 +226,16 @@ Write a query that returns, from the `species` table, the number of

## Solution

This query counts the number of times each value (Bird, Rabbit, Reptile or Rodent) in the `taxa` field occurs, defining a new field named `taxa_count` to hold the result.
The `GROUP BY` clause means the query will create an aggregated table with one row for each taxa.
Only those `taxa` values that have more than ten records will be included because of the `HAVING` clause.
This filtering is applied _after_ grouping has been done.

```sql
SELECT taxa, COUNT(*) AS n
SELECT taxa, COUNT(*) AS taxa_count
FROM species
GROUP BY taxa
HAVING n > 10;
HAVING taxa_count > 10;
```

:::::::::::::::::::::::::
Expand Down