We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
-- query to print id,age,coins_need and power of the wands sorted by order of descending power
-- if power is same sort the results by descending age
--lets get a CTE for minimum coins needed
WITH coins_min AS (
SELECT code, power, MIN(coins_needed) AS minimum_coins
FROM wands
GROUP BY code, power
)
SELECT w.id, p.age, w.coins_needed, w.power
FROM wands w
JOIN wands_property p ON w.code = p.code
JOIN coins_min c ON w.code = c.code AND w.power = c.power
WHERE p.is_evil = 0 AND w.coins_needed = c.minimum_coins
ORDER BY w.power DESC, p.age DESC;
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Ollivander's Inventory
You are viewing a single comment's thread. Return to all comments →
-- query to print id,age,coins_need and power of the wands sorted by order of descending power -- if power is same sort the results by descending age --lets get a CTE for minimum coins needed WITH coins_min AS ( SELECT code, power, MIN(coins_needed) AS minimum_coins FROM wands GROUP BY code, power ) SELECT w.id, p.age, w.coins_needed, w.power FROM wands w JOIN wands_property p ON w.code = p.code JOIN coins_min c ON w.code = c.code AND w.power = c.power WHERE p.is_evil = 0 AND w.coins_needed = c.minimum_coins ORDER BY w.power DESC, p.age DESC;