Ulzurrun de Asanza i Sàez

Tag: Oracle

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.