Q1. 멕시코(Mexico)보다 인구가 많은 나라이름과 인구수를 조회하시고 인구수 순으로 내림차순하세요.
1 2 3 4 5
use world; SELECTname, population FROM country WHERE population > (SELECT population FROM country WHEREname = "mexico") ORDERBY population DESC;
Q2. 국가별 몇개의 도시가 있는지 조회하고 도시수 순으로 10위까지 내림차순하세요.
1 2 3 4 5 6 7
use world; SELECT country.name, count(city.name) ascount FROM country, city WHERE country.code = city.countrycode GROUPBY country.name ORDERBYcountDESC LIMIT10;
Q3. 언어별 사용인구를 출력하고 언어 사용인구 순으로 10위까지 내림차순하세요.
1 2 3 4 5 6 7 8
use world; SELECTlanguage, sum(pop) ascount FROM ( SELECT countrylanguage.language, countrylanguage.countrycode, (countrylanguage.percentage / 100 * country.population) as pop FROM countrylanguage, country WHERE country.code = countrylanguage.countrycode ) as langpop GROUPBYlanguage ORDERBYcountdesc LIMIT10;
Q4. 나라 전체 인구의 10%이상인 도시에서 도시인구가 500만이 넘는 도시를 아래와 같이 조회 하세요.
1 2 3 4 5
use world; SELECT city.name, city.countrycode, country.name, (city.population / country.population) * 100as percentage FROM city, country WHERE city.countrycode = country.code AND city.population >= country.population * 0.1AND city.population >= 5000000 ORDERBY percentage DESC
Q5. 면적이 10000km^2 이상인 국가의 인구도(1km^2 당 인구수)를 구하고 인구도가 200이상인 국가들의 사용하고 있는 언어수가 5가지 이상인 나라를 조회 하세요.
1 2 3 4 5 6 7 8 9 10 11 12 13 14
use world; SELECT code, name, ROUND((population / surfaceArea)) as density FROM country WHERE surfaceArea >= 10000 ORDERBY density DESC; SELECT den.name, count(countrylanguage.language) as language_count FROM ( SELECT code, name, ROUND((population / surfaceArea)) as density FROM country WHERE surfaceArea >= 10000 ORDERBY density DESC ) as den, countrylanguage WHERE den.code = countrylanguage.countrycode AND den.density >= 200 GROUPBY den.name HAVING language_count >= 5 ORDERBY language_count DESC;
Q6. 사용하는 언어가 3가지 이하인 국가중 도시인구가 300만 이상인 도시를 아래와 같이 조회하세요.
GROUP_CONCAT(LANGUAGE) 을 사용하면 group by 할때 문자열을 합쳐서 볼수 있습니다.
VIEW를 이용해서 query를 깔끔하게 수정하세요.
1 2 3 4 5 6 7 8 9 10
use world; CREATEVIEW under_3lang AS SELECT countrycode, count(language) as language_count, GROUP_CONCAT(LANGUAGE) as languages FROM countrylanguage GROUPBY countrycode HAVING language_count <= 3; select city.countrycode, city.name as city_name, city.population, country.name, under_3lang.language_count, under_3lang.languages FROM city, under_3lang, country WHERE city.countrycode = under_3lang.countrycode AND country.code = city.countrycode AND city.population >= 3000000 ORDERBY city.population DESC;
If you like this blog or find it useful for you, you are welcome to comment on it. You are also welcome to share this blog, so that more people can participate in it. If the images used in the blog infringe your copyright, please contact the author to delete them. Thank you !