SQL/MySQL

[MySQL/HackerRank] Ollivander's Inventory

NLP Developer 2023. 8. 29. 16:55
728x90
반응형

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

문제

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.

Code

SELECT W.ID, P.AGE, W.COINS_NEEDED, W.POWER
FROM WANDS W
 INNER JOIN WANDS_PROPERTY P ON W.CODE = P.CODE
WHERE P.IS_EVIL = 0
AND W.COINS_NEEDED = (SELECT MIN(W1.COINS_NEEDED)
                      FROM WANDS W1
                       INNER JOIN WANDS_PROPERTY P1 ON W1.CODE = P1.CODE
                      WHERE P1.IS_EVIL = 0 
                      AND W1.POWER = W.POWER
                      AND P1.AGE = P.AGE)
ORDER BY W.POWER DESC, P.AGE DESC
728x90
반응형