[Hackerrank] SQL - Ollivander's Inventory

DMIS·2022년 3월 7일
0

SQL

목록 보기
29/48
post-thumbnail

문제

풀이

서브쿼리가 익숙하지 않아 개인적으로 정말 어려운 문제였다.

처음에는 모든 정보를 담은 테이블을 하나 만들어서 조건을 걸려고 헀는데 그렇게 되면 coins_needed에 대한 조건을 걸수 없었다.

따라서 wands 테이블에서 id를 제외한 나머지 열들로 coins_needed의 최솟값을 정해 code를 기준으로 group by한 테이블과 wands 테이블을 join하고, 거기에 wands_property 테이블을 조인하였다.

나머지는 문제에 나온 조건에 따라 where 절과 order by 절을 사용하였다.

select t2.id, t3.age, t1.coins_needed, t1.power
from ((select code, min(coins_needed) as coins_needed, power from wands
     group by code, power) as t1
     join wands as t2
     on t1.code = t2.code and t1.power = t2.power and t1.coins_needed = t2.coins_needed
     join wands_property as t3
     on t1.code = t3.code)
where t3.is_evil = 0
order by t1.power desc, t3.age desc
profile
Data + Math

0개의 댓글