Ulzurrun de Asanza i Sàez

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:

[sql]SELECT COUNT( country ) FROM users GROUP BY country[/sql]

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

[sql]SELECT COUNT( COUNT ( country ) ) FROM users GROUP BY country[/sql]

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


No replies on “Counting GROUP BY results

There are no comments yet.

Leave a Reply

Your email address will not be published.

Required fields are marked *

Your avatar