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.

I understand the question i.e for each combination of age and power you need to find the wand with minimum cost that is not evil.

I came up with this:

SELECT MIN(w.coins_needed),w.power,wp.age from wands w
inner join wands_property wp
on w.code= wp.code
where wp.is_evil=0
group by w.power,wp.age
order by w.power desc, wp.age desc;

The solution requires me to add ID of the wand and I am struggling with the same.
Any Idea how to get the ID infront of my solution

You can get the ID in front by adding one more condition in the WHERE statement after your JOIN. This is to to get the row with the minimum coins needed that matches the AGE and POWER. Please find the query below:

SELECT ID, AGE, COINS_NEEDED, POWER
FROM
(WANDS w
JOIN WANDS_PROPERTY p
ON w.Code = p.Code)
WHERE p.is_evil = 0 AND w.coins_needed =
(
SELECT MIN(coins_needed)
FROM
Wands w1
JOIN WANDS_PROPERTY p1
ON (w1.code = p1.code)
WHERE
w1.power = w.power AND p1.age = p.age
)
ORDER BY POWER DESC, AGE DESC

## Ollivander's Inventory

You are viewing a single comment's thread. Return to all comments →

Yes, but the problem never mentioned to look for the minimum among the different combinations of power and age. It is ill-posed.

Finally I know why my code doesn't work... Thank you.

Can you (or someone else) help me?

I understand the question i.e for each combination of age and power you need to find the wand with minimum cost that is not evil.

I came up with this:

SELECT MIN(w.coins_needed),w.power,wp.age from wands w inner join wands_property wp on w.code= wp.code where wp.is_evil=0 group by w.power,wp.age order by w.power desc, wp.age desc;

The solution requires me to add ID of the wand and I am struggling with the same. Any Idea how to get the ID infront of my solution

hi bhavukkhanna,

You can get the ID in front by adding one more condition in the WHERE statement after your JOIN. This is to to get the row with the minimum coins needed that matches the AGE and POWER. Please find the query below:

SELECT ID, AGE, COINS_NEEDED, POWER FROM (WANDS w JOIN WANDS_PROPERTY p ON w.Code = p.Code) WHERE p.is_evil = 0 AND

w.coins_needed = ( SELECT MIN(coins_needed) FROM Wands w1 JOIN WANDS_PROPERTY p1 ON (w1.code = p1.code) WHERE w1.power = w.power AND p1.age = p.age )ORDER BY POWER DESC, AGE DESC