### 获得最大的城市人口

``````Country Name - United States
LargestCity  - New York
CityPopulation - 8008278
Percentage to the country population - 2.88
``````

``````SELECT
co.Name, co.Population,
MAX(ci.Population),
((MAX(ci.Population) * 100) / Co.Population) “PercentageOfCountryPopulation”
FROM
Country co
JOIN
City ci ON (co.Code = ci.CountryCode)
WHERE
Continent = ‘North America’ OR Continent = ‘South America’
GROUP BY
Co.Name
``````

#### 1 个答案:

Y / X =（（Y / X）* 100）％= P％

15是百分之十五？

``````Step 1: 15/150 = 0.10

step 2:((15/150) * 100) = 10%
``````

``````SELECT co.NAME,
co.population,
max_population,
( ( co.population / max_population ) * 100 ) as percentageofcountrypopulation
FROM   country co
JOIN city ci
ON co.code = ci.countrycode
INNER JOIN (SELECT countrycode,
Max(population) max_population
FROM   city
GROUP  BY countrycode) mc
ON mc.countrycode = co.code
WHERE  continent = 'north america'
OR continent = 'south america'
``````

``````(SELECT countrycode,
SUM(population) total_population
FROM   city
GROUP  BY countrycode)
``````