简体   繁体   中英

SQL: What country has the most cities?

Hello I started using MySQL and I seem to be having trouble trying to nest formulas. I'm working on a problem and the question is, What country has the most cities?

I have two tables:

CITY:
city
city_id
country_id

COUNTRY:
country 
country_id

I am able to join the two tables together to get the cities to match with the countries but after that I don't know how to count to the country that has the most cities.

My current code is:

SELECT city.city, country.country 
FROM city, country
WHERE city.country_id = country.country_id

From there I don't know how to add a count function without it coming back as as error. I dont fully understand the basics of nesting.

Thank you, any help is appreciated.

You do not need to do nesting necessarily. To simply know, which country has most number of cities, just use group by:

select country_id, count(1)
from city
group by country_id

This will give you the number of cities in each country. Then you could use a CTE to get the country with the largest number of cities.

You need to GROUP BY if you want to use aggregate functions.

Given the fact that you're very new to this I think you'll get a lot more out of this if you spend a few minutes reading up on some documentation. Don't worry, this is easy stuff so you'll understand this in no time. Please have a look at the following basic info ( MySQL GROUP BY basic info ) regarding the use of GROUP BY in MySQL. Your questions are answered in the topic regarding 'MySQL GROUP BY with aggregate functions'.

Basic group by:

SELECT 
    status, COUNT(*)
FROM
    orders
GROUP BY status;

Group by using a join:

SELECT 
    status, SUM(quantityOrdered * priceEach) AS amount
FROM
    orders
        INNER JOIN
    orderdetails USING (orderNumber)
GROUP BY status;
SELECT x.country 
  FROM country x
  JOIN city y
    ON y.country_id = x.country_id
 GROUP
    BY x.country
 ORDER 
    BY COUNT(*) DESC LIMIT 1;

On the (fantastically unlikely) chance that the most civilised countries have equal numbers of cities, you would have to amend this a little.

What makes this difficult is possible ties, ie two or more countries sharing the maximum number of cities. As of MySQL 8 you can use window functions to help you with this. Here I compare the country counts and their maximum and then pick the rows were the two match.

select *
from country
where (country_id, true) in -- true means it is a maximum city country
(
  select country_id, count(*) = max(count(*)) over()
  from city
  group by country_id
);

Try the following code:

**select
    country.country_id,
    count(city.city_id)
from
    country
inner join
    city
on
    city.country_id=country.country_id
group by
    city.country_id
having
    count(city.city_id) =
    (SELECT
        max(count(city.city_id))
    FROM
        city
    GROUP BY
        city.city_id);**

You need to group by country_id to make sure all cities that are connected to one country_id can be counted.

Where is a good approach, however it does not work together with "group by" as it will be accounted before the "group by" command.

The way you joined your data from different tables is not a very proper yet working way. I suggest to use the inner join in this case to make the command more obvious/better readable.

Count() is used to count the number of cities that accumulate on one country max() is used to get the country with the most cities (highest count()).

Sorry it was my first post, my code looks terrible.

following my code again.

select
    country.country_id,
    count(city.city_id)
from
    country
inner join
    city
on
    city.country_id=country.country_id
group by
    city.country_id
having
    count(city.city_id) =
    (SELECT
        max(count(city.city_id))
    FROM
        city
    GROUP BY
        city.city_id);

Best regards,

Jens

SELECT country_id, count(1)
 FROM city
 GROUP BY country_id
 ORDER BY count(1) desc;

Try following Code -

SELECT *, 
  (SELECT COUNT(*) FROM cities WHERE cities.country_id=C.country_id) as cities_count 
FROM country C 
ORDER BY cities_count DESC
LIMIT 0,1

Also is joining the two tables necessary? In your query, you said you need to find What country has the most cities?

Above query will only return one country with max cities.

You can find the country like this:

SELECT MAX(c.id) FROM (SELECT COUNT(id) AS id FROM city group by country_id) c

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM