Number of users from a specific country

I have a user table, and in this table there is a field of the country in which these people are indicated (i.e. "Sweden", "Italy", ...). How can I make a SQL query to get something like:

Country     Number
Sweden      10
Italy       50
...         ...

Users select their countries from the list that I give them, but the list is really huge, so it would be nice to have an SQL query that can avoid using this list, that is, look in the database and return only those countries that are in the database, therefore that, for example, I do not have anyone from Barbados, even if I have this option in the country field of the registration form :)

Thanks in advance!

+3
source share
5 answers

, . . .

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

, ( , ), :

SELECT
    DISTINCT country
FROM
    users
+3

Users, - :

SELECT Country, COUNT (*) AS Number
FROM Users
GROUP BY Country
ORDER BY Country

,

SELECT Contries.CountryName, Count (*) AS Number
FROM Users
INNER JOIN Countries
    ON Users.CountryId = Countries.CountryId
GROUP BY Countries.CountryName
ORDER BY Countries.CountryName
+2

, - ...?

SELECT Country, COUNT(*) AS Number
FROM Users
GROUP BY Country
0

:

SELECT
    Country, COUNT(*) AS 'Number'
FROM
     YourTable
GROUP BY
    Country
ORDER BY
    Country

.

, , , ORDER BY:

SELECT
    Country, COUNT(*) AS 'Number'
FROM
     YourTable
GROUP BY
    Country
ORDER BY
    COUNT(*) DESC
0

:

select country, count(*) from users group by country;

:

select distinct country from users;
0

Source: https://habr.com/ru/post/1766082/


All Articles