Counting GROUP BY results

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
Code language: SQL (Structured Query Language) (sql)

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
Code language: SQL (Structured Query Language) (sql)

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

Leave a Reply

Your email address will not be published.

Required fields are marked *

Your avatar