Every selected column must either be in the GROUP BY or inside an aggregate. SELECT country, COUNT(*) … GROUP BY country gives one row per country.
Group by more than one column for finer breakdowns, such as country and segment.
Worked example
How many customers are in each country?
SELECT country, COUNT(*) AS customers FROM customers GROUP BY country ORDER BY customers DESC, country;
| country | customers |
|---|---|
| Germany | 4 |
| France | 3 |
| Greece | 3 |
| Ireland | 3 |
| Netherlands | 3 |
| Italy | 2 |
| Spain | 2 |
Common mistakes
- Selecting a column that is neither grouped nor aggregated.
- Grouping by an ID when you meant a name, which splits one group into many.