SQL Cheatsheet - Part Two (GROUP BY)
GROUP BYis fundamental to perform aggregation functions (ιθ¨ι’ζ°)- When it throws an error, it's scary
- When it works for some unknown reason, it's even scarier
- Always wrap your aggregation targets in parentheses.
AVG("age"), notAVG "age". PostgreSQL will absolutely lose it otherwise.
We will work with this table
table name: members
| name | created_day |channel|age |
|:-------------:|:-------------:|:-----:|:---:|
| γ¨γ¬γ³ | 2021-02-13 |web |27 |
| γγγγ‘ | 2021-02-13 |ad |27 |
| γγγ | 2021-02-15 |ad |27 |
| δΈθ°· | 2021-02-15 |ad |33 |
| γγγ | 2021-02-16 |web |24 |
COUNT: Counts The Number of Records
SELECT COUNT("name")
FROM members
WHERE "created_day" = '2021-02-13';
| count |
|---|
| 2 |
GROUP BY: Groups the same records together
SELECT "created_day", COUNT("name")
FROM members
GROUP BY "created_day";
| created_day | count |
|---|---|
| 2021-02-13 | 2 |
| 2021-02-15 | 2 |
| 2021-02-16 | 1 |
SELECT "created_day", "channel", COUNT("name")
FROM members
GROUP BY "created_day";
This will throw an error, because the system doesn't know if 2021-02-13 in created_day corresponds to ad, or web in the column channel.
SELECT "created_day", "channel", COUNT("name")
FROM members
GROUP BY "created_day", "channel";
| created_day | channel | count |
|---|---|---|
| 2021-02-13 | web | 1 |
| 2021-02-13 | ad | 1 |
| 2021-02-15 | ad | 2 |
| 2021-02-16 | web | 1 |
Aggregation functions: (ιθ¨ι’ζ°)
Aggregates values
COUNT: number of recordsAVG: the average valueMAX: the maximumMIN: the minimumSUM: the total
SELECT "created_day", AVG("age"), MAX("age")
FROM members
GROUP BY "created_day";
| created_day | avg | max |
|---|---|---|
| 2021-02-13 | 27 | 27 |
| 2021-02-15 | 30 | 33 |
| 2021-02-16 | 24 | 24 |