GROUP BY can take several columns. Each different combination of their values is one group.
sql
SELECT country, is_member, COUNT(*)
FROM customers
GROUP BY country, is_member;customers
| id | name | country | is_member |
|---|---|---|---|
| 1 | Ava Chen | CA | true |
| 8 | Omar Haddad | CA | true |
| 2 | Liam Patel | CA | false |
| 5 | Mia Johnson | US | true |
| 4 | Noah Kim | US | false |
result
| country | is_member | count |
|---|---|---|
| CA | true | 2 |
| CA | false | 1 |
| US | true | 1 |
| US | false | 1 |
| … | … | … |
- A combination that has no rows makes no group. No country has a row with a count of 0.
- The order of the columns in GROUP BY does not change the groups. It does not sort the result either: add ORDER BY for that.
- The SELECT rule is the same: each selected column is in GROUP BY or inside an aggregate.