Back to Developer Roadmap

Count Users By Country

src/data/question-groups/sql-queries/content/count-users-by-country.md

4.01005 B
Original Source

Given a table Users that looks like this:

userIdfirstNamelastNameagecountry
1JohnDoe30Portugal
2JaneDon31Belgium
3WillLiam25Argentina
4WadeGreat32Denmark
5PeterSmith27USA
6RichMond30USA
7RachMane30Argentina
8ZachRidge30Portugal

The query to COUNT the number of users by country is:

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

The query uses the GROUP BY clause to group the users by country and then shows the count in the next column. The result of the query looks like this:

countrycount
USA2
Portugal2
Argentina2
Belgium1
Denmark1