Average Population of Each Continent 문제 바로가기
문제해석
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.
city와 country 테이블에서 모든 대륙(COUNTRY.Continent)의 이름과 대륙별 도시인구수(CITY.Population)의 평균을 구하는 쿼리 입력. 도시인구수의 평균값은 가장 가까운 정수로 내림처리.
Input Format
The CITY and COUNTRY tables are described as follows:


문제풀이
SELECT country.continent, FLOOR(AVG(city.population))
FROM city
INNER JOIN country ON city.countrycode = country.code
GROUP BY country.continent
1. country code로 city와 country 조인
2. 대륙(continent)으로 그룹화
3. population을 AVG함수로 평균값 구하기
4. FLOOR 함수로 소수점 내림처리
(* 올림 : CEIL , 반올림: ROUND)
Tip
-- GROUP BY 함수에서 그룹화의 기준이 되는 컬럼은 반드시 SELECT 에 적어주기
-- population 컬럼은 두 테이블에 중복되어 테이블명을 명시해줘야 함. 명시하지 않으면 에러 발생
-- continent 컬럼은 country테이블에만 있어 테이블명을 명시하지 않아도 추출 가능, 쿼리 결과 차이없음
그러나 다른 사람이 읽는 경우 테이블명을 명시하는 것이 좋고, 쿼리 성능면에서도 전체 테이블이 아닌 해당 테이블 내에서만 컬럼 검색을 하기 때문에 연산속도에 영향을 줄 수 있음
'SQL > MySQL 문제풀이' 카테고리의 다른 글
[Leetcode] Employees Earning More Than Their Managers (풀이) (0) | 2022.05.24 |
---|---|
[SQL LEFT JOIN][Leetcode] Customer Who Never Order (풀이) (0) | 2022.05.23 |
[SQL INNER JOIN][HackerRank] Population Census (풀이/해석) (0) | 2022.05.23 |
[SQL INNER JOIN][HackerRank] African Cities Solution(해석/풀이) (0) | 2022.05.23 |
[HackerRank] Weather Observation Station 18 Solution(해석/풀이) (0) | 2022.05.22 |