tak's data blog

[SQL] HackerRank - Ollivander's Inventory 본문

SQL

[SQL] HackerRank - Ollivander's Inventory

hyuntaek 2022. 6. 5. 20:54
반응형
SMALL

 

출처: https://www.hackerrank.com/challenges/harry-potter-and-wands/problem?isFullScreen=true 

 

Ollivander's Inventory | HackerRank

Help pick out Ron's new wand.

www.hackerrank.com

 

 

역시 꾸준함이 중요한 것 같네요... 얼마전에 풀었던 기억이 있는데 오랜만에 다시 풀어보니 바로 풀리지가 않습니다..

 

이번에는 해커랭크의 Ollivander's Inventory 문제로 해리포터를 배경으로 지팡이를 교체하려 할 때 여러 조건에 맞춰야하는 문제입니다. 문제의 핵심은 서브쿼리를 기존쿼리문과 조인해서 사용해야 하는 것입니다.

 

문제:

Harry Potter and his friends are at Ollivander's with Ron, finally replacing Charlie's old broken wand.

Hermione decides the best way to choose is by determining the minimum number of gold galleons needed to buy each non-evil wand of high power and age. Write a query to print the id, age, coins_needed, and power of the wands that Ron's interested in, sorted in order of descending power. If more than one wand has same power, sort the result in order of descending age.

 

문제의 조건으로는 

1) is_evil = 0

2) Wands_Property의 age와 Wands의 power값이 각각 같은 지팡이들 중 coins_needed가 최소가 되는 값을 골라야 함

3) 문제 마지막줄의 2개 기준에따라 정렬

 

풀이:

select w.id, p.age, w.coins_needed, w.power
from wands w
    join wands_property p on w.code = p.code
where p.is_evil = 0
  and
      w.coins_needed in (select min(w1.coins_needed)
                          from wands w1
                          join wands_property p1 on w1.code = p1.code
                         where w.power = w1.power
                          and p.age = p1.age)
order by w.power desc, p.age desc

 

 

where절에 서브쿼리를 통해 coins_needed가 최소가 되는 값을 기존쿼리문과 조건을 주었습니다.

 

문제가 영어로 되어 있는점과 여러 조건들이 많아서 헷갈릴 수 있는 문제라고 생각합니다. 하지만 조건에따라 하나하나 맞춰나가면 쉽게 해결할 수 있습니다!!

 

 

 

반응형
LIST

'SQL' 카테고리의 다른 글

LeetCode 문제풀이 (184, 185)  (0) 2022.06.26
LeetCode 문제풀이 (176, 178, 180, 181, 182, 183)  (1) 2022.06.24
[SQL] HackerRank - New Companies  (0) 2022.04.03
[SQL] HackerRank - Challenges  (0) 2022.03.17
[SQL] HackerRank - Contest Leaderboard  (1) 2022.02.12