Algorithm/SQL

[SQL][HackerRank] Average Population of Each Continent

융무 2020. 6. 13. 16:54

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.

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

Input Format

The CITY and COUNTRY tables are described as follows:

  • join two tables ==> FROM CITY AS i JOIN COUNTRY AS o ON i.COUNTRYCODE=o.CODE
  • query names of continents ==> SELECT o.CONTINENT
  • respective city average population ==> AVG(i.POPULATION) … GROUP BY o.CONTINENT
  • round down to nearest integer ==> FLOOR(AVG(i.POPULATION)) … GROUP BY o.CONTINENT
SELECT O.CONTINENT, FLOOR(AVG(I.POPULATION)) 
FROM CITY AS I 
JOIN COUNTRY AS O ON I.COUNTRYCODE=O.CODE 
GROUP BY O.CONTINENT;