문제
1번. 전년도 대비 GNP가 감소한 국가 중 인구 수가 1,000만명 이상인 국가만 추출
with a as (
select *
from country
where GNPOld != 0 or GNPOld is not null
),
b as (
select Code, Name, GNP, GNPOld, Population
from a
where GNP < GNPOld and Population >= 1000)
2번. 도시가 3개 이상 존재하는 행정구역의 평균 인구수 추출
select District, round(sum(Population)/count(distinct ID)) as AVERAGE_POPULATION
from city
group by District
having count(distinct ID) >= 3
order by round(sum(Population)/count(distinct ID)) desc
3번. 각 대륙에서 인구가 가장 많은 도시 추출
with a as (
select c1.Name as CITY_NAME, c2.Name as COUNTRY_NAME, c2.Continent, c1.Population, row_number() over(partition by c2.Continent, c1.Name order by c1.Population desc) as rn
from country c2 inner join city c1 on c2.Code = c1.CountryCode)
select CITY_NAME, COUNTRY_NAME, Continent, Population
from a
where rn =1
order by Population desc
오답 이유
다시 푼 코드
with a as (
select c1.Name as CITY_NAME, c2.Name as COUNTRY_NAME, c2.Continent, c1.Population
from country c2 inner join city c1 on c2.Code = c1.CountryCode),
b as (
select Continent, max(Population) as max_p
from a
group by Continent)
select CITY_NAME, COUNTRY_NAME, Continent, Population
from a
where Population = (select max_p from b where a.Continent = b.Continent)
order by Population desc
with a as (
select c1.Name as CITY_NAME, c2.Name as COUNTRY_NAME, c2.Continent, c1.Population, row_number() over(partition by c2.Continent order by c1.Population desc) as rn
from city c1 inner join country c2 on c1.CountryCode = c2.Code)
select CITY_NAME, COUNTRY_NAME, Continent, Population
from a
where rn =1
order by Population desc
TMI..
QCC 해설을 듣다가 튜터님이 "머피"를 닮았다길래 검색해보았다...
그렇다.. 그녀도 정상은 아니었다..
우리가 친한 데에는 이유가 있지 후후