0

using my SQL list, for each country, the number of cities. Would you think that this statement means count the number of cities of each country?

+----+----------------+---------+---------------+------------+
| id | name           | country | original-name | population |
+----+----------------+---------+---------------+------------+
|  1 | Kabul          | AFG     | Kabol         | 1780000    |
|  2 | Qandahar       | AFG     | Qandahar      | 237500     |
|  3 | Herat          | AFG     | Herat         | 186800     |
|  4 | Mazar-e-Sharif | AFG     | Balkh         | 127800     |
|  5 | Amsterdam      | NLD     | Noord-Holland | 731200     |
|  6 | Rotterdam      | NLD     | Zuid-Holland  | 593321     |
|  7 | Haag           | NLD     | Zuid-Holland  | 440900     |
|  8 | Utrecht        | NLD     | Utrecht       | 234323     |
|  9 | Eindhoven      | NLD     | Noord-Brabant | 201843     |
| 10 | Tilburg        | NLD     | Noord-Brabant | 193238     |
+----+----------------+---------+---------------+------------+

my code that works but it's over 4000 lines:

SELECT name, country 
FROM city 
GROUP BY name, country 
ORDER BY country ASC;

1 Answer 1

1

You need to GROUP BY country only:

SELECT country, COUNT(*) as count_of_cities_per_country
FROM city 
GROUP BY country 
ORDER BY country ASC;
Sign up to request clarification or add additional context in comments.

1 Comment

mysql> SELECT country, COUNT(*) as count_of_cities_per_country -> FROM city -> GROUP BY country -> ORDER BY country ASC -> ; +---------+-----------------------------+ | country | count_of_cities_per_country | +---------+-----------------------------+ | ABW | 1 | | AFG | 4 | | AGO | 5 | | AIA | 2 |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.