You are viewing a single comment's thread. Return to all comments →
select w.id, p.age, w.coins_needed, w.power from wands w
join wands_property p on w.code=p.code where p.is_evil=0 and w.coins_needed=(select min(w2.coins_needed) from wands w2 where w.code=w2.code and w.power=w2.power) order by w.power desc, p.age desc;
In subquery there is no need to join wands with wands_property because one code = one age (you can just compare codes instead of ages).