Grouping Data
To aggregate data, we tack the GROUP BY
clause onto our SELECT statement. To do so, we must also have some aggregation methodology, most typically COUNT()
.
The format is simply:
SELECT field1, COUNT(*)
FROM tablename
WHERE ...
GROUP BY field [,field, field]
Examples
- How many of each gender are in sws.people?
- Create a query to show the number of years employed, and how many people are in that group.
Having
And then there was HAVING
. It’s like WHERE, but different.
SELECT field1, COUNT(*)
FROM tablename
GROUP BY field
HAVING expression = expression
Examples
- How many occurrences of people having the same last name?
- How many genders have more than 100 people of that type?
Back to Class 4 Contents