SQL Cheatsheet - Part Two (GROUP BY)

  • GROUP BY is 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"), not AVG "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_daycount
2021-02-132
2021-02-152
2021-02-161
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_daychannelcount
2021-02-13web1
2021-02-13ad1
2021-02-15ad2
2021-02-16web1

Aggregation functions: (ι›†θ¨ˆι–’ζ•°)

Aggregates values

  • COUNT: number of records
  • AVG: the average value
  • MAX: the maximum
  • MIN: the minimum
  • SUM: the total
SELECT "created_day", AVG("age"), MAX("age")
  FROM members
GROUP BY "created_day";
created_dayavgmax
2021-02-132727
2021-02-153033
2021-02-162424