본문 바로가기
  • KEEP HUSTLE!
MYSQL 코테 준비

[HacKerRank SQL] Asian Population- MYSQL

by 하수군 2021. 2. 24.

Note: CITY.CountryCode and COUNTRY.Code are matching key columns.

 

Input Format

The CITY and COUNTRY tables are described as follows: 

 

 

Given the CITY and COUNTRY tables, query the sum of the populations of all cities where the CONTINENT is 'Asia'.

SELECT
    SUM(A.POPULATION)
FROM
    CITY AS A INNER JOIN COUNTRY AS B 
    ON A.COUNTRYCODE = B.CODE 
WHERE B.CONTINENT = 'Asia';

 

Given the CITY and COUNTRY tables, query the names of all cities where the CONTINENT is 'Africa'.

SELECT 
    A.NAME
FROM 
    CITY AS A INNER JOIN COUNTRY AS B 
    ON A.COUNTRYCODE = B.CODE 
WHERE B.CONTINENT = 'Africa';

 

Given the CITY and COUNTRY tables, query the names of all the continents (COUNTRY.Continent) and their respective average city populations (CITY.Population) rounded down to the nearest integer.

SELECT 
    B.CONTINENT, TRUNCATE(AVG(A.POPULATION), 0) 
FROM 
    CITY AS A INNER JOIN COUNTRY AS B 
    ON A.COUNTRYCODE = B.CODE 
GROUP BY B.CONTINENT;

댓글