QCC 오답노트

수수수수퍼노인·2025년 10월 24일

SQL 코드카타

목록 보기
6/10
post-thumbnail

문제

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)
  • 오답 이유
    • 1,000만명인데... 조건에 1,000명으로 함..
    • 레전드 바보짓.. 정신 차립시다

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
  • 오답 이유

    • 대륙별, 도시별 인구 수로 구해야한다고 생각해서 partition by c2.Continent, c1.Name을 함
    • rn=1이 여러 개 추출됨
    • 각 “대륙–도시명” 조합마다 rn을 1부터 다시 세기 때문에, 대륙이 다르면 같은 도시명이라도 다시 rn=1이 됨
  • 다시 푼 코드

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 해설을 듣다가 튜터님이 "머피"를 닮았다길래 검색해보았다...

그렇다.. 그녀도 정상은 아니었다..
우리가 친한 데에는 이유가 있지 후후

profile
화이팅구리

0개의 댓글