Counting GROUP BY results

This post was published 4 years ago. It may be exremely outdated.

When counting results from a SQL query that uses GROUP BY clause, you should take into account that it will count items of each group rather than groups themselves, even if you count fields that appear in the GROUP BY clause:

SELECT COUNT( country ) FROM users GROUP BY country

Will return a row for each different country containing the amount of users from that country.

SELECT COUNT( COUNT ( country ) ) FROM users GROUP BY country

Will return a single row with the amount of different countries in the table.