Trying to understand Joins

Ömer Faruk Çalışkan :

I am trying to practice sql by myself and I was solving the questions from the website hackerrank. I came across to this question called " Ollivander's Inventory " ( here's a link if you want to see the question in detail other than my explanation https://www.hackerrank.com/challenges/harry-potter-and-wands/problem)

Question goes as

"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."

I found the solution that was pretty close to my code but still there is a part that i didnt understand in this solution. Code is:

Select w.id,wp.age,w.coins_needed,w.power
from wands as w
INNER JOIN wands_property as wp
on w.code=wp.code
Where wp.is_evil!=1 AND w.coins_needed=(Select min(Wands.coins_needed) 
from Wands
inner join Wands_Property 
on Wands.code=Wands_Property.code 
where Wands_Property.age=wp.age and Wands.power=w.power)
order by   w.power desc, wp.age desc;

I didnt understand this part of the code.

AND w.coins_needed=(Select min(Wands.coins_needed) 
from Wands
inner join Wands_Property 
on Wands.code=Wands_Property.code 
where Wands_Property.age=wp.age and Wands.power=w.power)

As I know (correct me if im wrong please) we coudlnt use min(wands.coins_needed) in the beginning of the query because after wards we would need to use group by clause and it would cause problem on w.id. But what is the reason why we used another join after the w.coins_needed=(Select min(Wands.coins_needed)? Any help would be appricated. Please show me where im thinking wrong.

Thanks!

Marlin Pierce :

OK, you likely understand this, but just to be clear and answer completely, the query, (based on Hermione's suggestion) is listing every combination of power and age for all the wands listed, but when more than one wand has the same power and age, only list the least expensive one.

The outer query is going through all the wands.

The inner query, will query for all the other wands with the same power and age to find the cheapest price.

To do that, the inner query needs to take the power and age of the "current" wand from the outer query.

To query for all wands with the same power and age, we need to join the Wands and Wands_Property tables. This is the answer. We need both tables to match power and age in our inner query looking for the cheapest price of all equivalent wands.

Once we know the cheapest price we can get a wand with a given power and age, we can have a condition in our WHERE clause to only show those wands.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=10127&siteId=1