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.
MYSQL Solution:
The concept is to create a Sub query to find the min of Coins_needed and Join all the tables together.
SELECT id, age, Coins_needed, w1.power
FROM Wands w1
JOIN Wands_property wp1 ON w1.code = wp1.code
JOIN (SELECT w.code, power, min(w.coins_needed) AS Min_coins
FROM Wands w
JOIN Wands_property wp ON w.code = wp.code
WHERE wp.is_evil = 0
GROUP BY w.code, w.power) AS CTE ON CTE.code = w1.code
AND CTE.power = w1.power
AND CTE.Min_coins = w1.Coins_needed
ORDER BY w1.power DESC, 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 →
MYSQL Solution: The concept is to create a Sub query to find the min of Coins_needed and Join all the tables together. SELECT id, age, Coins_needed, w1.power FROM Wands w1 JOIN Wands_property wp1 ON w1.code = wp1.code JOIN (SELECT w.code, power, min(w.coins_needed) AS Min_coins FROM Wands w JOIN Wands_property wp ON w.code = wp.code WHERE wp.is_evil = 0 GROUP BY w.code, w.power) AS CTE ON CTE.code = w1.code AND CTE.power = w1.power AND CTE.Min_coins = w1.Coins_needed ORDER BY w1.power DESC, age DESC