DB/SQL오답노트

[DB/SQL오답노트/HackerRank] Average Population of Each Continent

각시탈코더 2023. 8. 10. 08:47

1.문제

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:

 

2.내 풀이

SELECT COUNTRY.Continent, trunc(avg(CITY.population)) 
FROM CITY  JOIN COUNTRY 
ON City.CountryCode = Country.Code

 

3.정답

SELECT COUNTRY.Continent, floor(avg(CITY.population)) 
FROM CITY  JOIN COUNTRY 
ON City.CountryCode = Country.Code 
group by COUNTRY.Continent;

 

4.내가 문제를 틀린 이유

  • 소수점 내림 함수 floor를 제대로 알지 못했다.
  • 문제에서 '대륙별' 평균 정수 값을 구하는 건데 평균과, 버림에만 신경썼지, '대륙별' 단어를 놓쳤다. 당연히 집계함수 avg가 나오면, group by가 나와야 하는데 거기까지 생각이 닿지 못했다.