I've just installed MySQL, I'm interested in learning SQL. One of the default database of MySQL is "World". This database has 5 columns: ID, name (city name), CountryCode, District and Population. I tried a simple query that should have showed the ID of the city with the lowest population per country.
USE world;
SELECT ID, MIN(POPULATION), COUNTRYCODE
FROM CITY
GROUP BY COUNTRYCODE
order by ID
The problem is that the result of the query shows the first ID of the country, and its not related to the city's ID with the lowest population of the country.
Exemple: AFG countrycode has 4 cities:
+----+----------------+-------------+------------+------------+ | ID | CITY | COUNTRYCODE | CITY_AGAIN | POPULATION | +----+----------------+-------------+------------+------------+ | 1 | Kabul | AFG | Kabol | 1780000 | | 2 | Qandahar | AFG | Qandahar | 237500 | | 3 | Herat | AFG | Herat | 186800 | | 4 | Mazar-e-Sharif | AFG | Balkh | 127800 | +----+----------------+-------------+------------+------------+
The result of the query for AFG country is
+----+------------+-------------+ | ID | POPULATION | COUNTRYCODE | +----+------------+-------------+ | 1 | 127800 | AFG | +----+------------+-------------+
The ID was supposed to be 4, not 1.
So, why the result of the query is not correct on ID column?