해커랭크 Population Census 문제 바로가기
문제해석
Given the CITY and COUNTRY tables, query the sum of the populations of all cities where the CONTINENT is 'Asia'.
Note: CITY.CountryCode and COUNTRY.Code are matching key columns.
city, country 테이블에서 continent가 'Asia'인 도시들의 인구수 합을 구하는 쿼리입력
Input Format
The CITY and COUNTRY tables are described as follows:


문제풀이
SELECT SUM(city.population)
FROM city
INNER JOIN country ON city.countrycode = country.code
WHERE country.continent = 'Asia'
1. countrycode를 중심으로 INNER JOIN으로 city와 country 두 테이블을 조인
2. continent가 'Asia'인 데이터 WHERE 조건문으로 필터링
3. population의 합 SUM함수로 구하기
Tip
-- population 컬럼은 두 테이블에 중복되어 테이블명을 명시해줘야 함. 명시하지 않으면 에러 발생
-- continent 컬럼은 country테이블에만 있어 테이블명을 명시하지 않아도 추출 가능, 쿼리 결과 차이없음
그러나 다른 사람이 읽는 경우 테이블명을 명시하는 것이 좋고, 쿼리 성능면에서도 전체 테이블이 아닌 해당 테이블 내에서만 컬럼 검색을 하기 때문에 연산속도에 영향을 줄 수 있음
'SQL > MySQL 문제풀이' 카테고리의 다른 글
[SQL LEFT JOIN][Leetcode] Customer Who Never Order (풀이) (0) | 2022.05.23 |
---|---|
[SQL INNER JOIN][HackerRank] Average Population of Each Continent(해석/풀이) (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 |
[HackerRank] Weather Observation Station 17 Solution(해석/풀이) (0) | 2022.05.22 |