[해커랭크] Ollivander's Inventory
문제링크 : https://www.hackerrank.com/challenges/harry-potter-and-wands/problem?isFullScreen=true

❓문제
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.
🔉 문제 설명
해리포터 문제
id, age, coins_needed, power를 출력하는 쿼리를 짜라.
단, 헤르미온느는 최고의 선택 방법은 높은 힘과 나이를 가진 각각의 사악하지 않은 지팡이를 사는 데 필요한 최소 금 갤리온 수를 결정하는 것이다.
❗답
SELECT w.id, p.age, w.coins_needed, w.power
FROM WANDS w join Wands_Property p using(code)
WHERE w.coins_needed = (SELECT MIN(w2.coins_needed)
FROM WANDS w2 INNER JOIN Wands_Property p2 on w2.code = p2.code
WHERE w.power = w2.power
AND p.age = p2.age
AND p.is_evil = 0)
ORDER BY w.power DESC, p.age DESC
🤔풀이
괄호 하나를 안 써서 계속 ERROR가 떴었던 문제
문제의 추가적인 설명을 보면 code가 동일하면 age도 동일하다는 것을 알 수 있다.(1:1)
그러기 위해서 자기 테이블을 한 번 더 참조해주도록 쿼리를 작성했다.
inner join을 해서 같은 테이블만 남기고 power랑 age가 같은 것 중 최소값을 찾았다.
그 후 is_evil = 0(악하지 않은 것)을 찾으면 되는 문제였다.