HackerRank MySQL - 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. SELECT WANDS.ID,
  2. MIN_PRICES.AGE,
  3. WANDS.COINS_NEEDED,
  4. WANDS.POWER
  5. FROM WANDS
  6. INNER JOIN
  7. (SELECT WANDS.CODE,
  8. WANDS.POWER,
  9. MIN(WANDS_PROPERTY.AGE) AS AGE,
  10. MIN(WANDS.COINS_NEEDED) AS MIN_PRICE
  11. FROM WANDS
  12. INNER JOIN WANDS_PROPERTY ON WANDS.CODE = WANDS_PROPERTY.CODE
  13. WHERE WANDS_PROPERTY.IS_EVIL = 0
  14. GROUP BY WANDS.CODE,
  15. WANDS.POWER) MIN_PRICES ON WANDS.CODE = MIN_PRICES.CODE
  16. AND WANDS.POWER = MIN_PRICES.POWER
  17. AND WANDS.COINS_NEEDED = MIN_PRICES.MIN_PRICE
  18. ORDER BY WANDS.POWER DESC,
  19. MIN_PRICES.AGE DESC;

download android app