[해커랭크] Ollivander's Inventory

june·2023년 4월 9일
0

SQL

목록 보기
16/31

Ollivander's Inventory

https://www.hackerrank.com/challenges/harry-potter-and-wands

  • 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.

SELECT id, age, coins_needed, power
FROM wands w
    INNER JOIN wands_property wp ON w.code = wp.code
WHERE is_evil = 0
    AND coins_needed = (SELECT MIN(coins_needed) 
                        FROM wands w2
                            INNER JOIN wands_property wp2 ON w2.code = wp2.code
                        WHERE w2.power = w.power
                            AND wp2.age = wp.age)
ORDER BY power DESC, age DESC

Lesson & Learned

조건에 the minimum number of gold galleons 을 놓쳤다.

profile
나의 계절은

0개의 댓글