<\/span><\/h2>\n\n\n\nHarry Potter and his friends are at Ollivander’s with Ron, finally replacing Charlie’s old broken wand.<\/p>\n\n\n\n
Hermione decides the best way to choose is by determining the minimum number of gold galleons needed to buy each non-evil<\/em> wand of high power and age. Write a query to print the id<\/em>, age<\/em>, coins_needed<\/em>, and power<\/em> of the wands that Ron’s interested in, sorted in order of descending power<\/em>. If more than one wand has same power, sort the result in order of descending age<\/em>.<\/p>\n\n\n\n<\/span>Input Format<\/strong><\/span><\/h2>\n\n\n\nThe following tables contain data on the wands in Ollivander’s inventory:<\/p>\n\n\n\n
- Wands:<\/em> The id<\/em> is the id of the wand, code<\/em> is the code of the wand, coins_needed<\/em> is the total number of gold galleons needed to buy the wand, and power<\/em> denotes the quality of the wand (the higher the power, the better the wand is).<\/li>
- Wands_Property:<\/em> The code<\/em> is the code of the wand, age<\/em> is the age of the wand, and is_evil<\/em> denotes whether the wand is good for the dark arts. If the value of is_evil<\/em> is 0<\/em>, it means that the wand is not evil. The mapping between code<\/em> and age<\/em> is one-one, meaning that if there are two pairs, (code1, age1<\/strong><\/em>) and (code2, age2<\/strong><\/em>), then code1 != code2<\/em><\/strong> and age1 != age2<\/strong><\/em>.<\/li><\/ul>\n\n\n\n
Sample Input<\/strong><\/p>\n\n\n\nWands Table<\/p>\n\n\n\n
Wands_Property<\/em> Table:<\/p>\n\n\n\nSample Output<\/strong><\/p>\n\n\n\n9 45 1647 10\n12 17 9897 10\n1 20 3688 8\n15 40 6018 7\n19 20 7651 6\n11 40 7587 5\n10 20 504 5\n18 40 3312 3\n20 17 5689 3\n5 45 6020 2\n14 40 5408 1<\/code><\/pre>\n\n\n\n<\/span>Explanation<\/strong><\/span><\/h2>\n\n\n\nThe data for wands of age 45<\/em> (code 1): <\/p>\n\n\n\n- The minimum number of galleons needed for wand<\/em>(age<\/em> = 45, power<\/em> = 2) = 6020 <\/strong><\/li>
- The minimum number of galleons needed for wand<\/em>(age<\/em> = 45, power<\/em> = 10) = 1647<\/strong><\/li><\/ul>\n\n\n\n
The data for wands of age 40<\/em> (code 2): <\/p>\n\n\n\n- The minimum number of galleons needed for wand<\/em>(age<\/em> = 40, power<\/em> = 1) = 5408<\/strong><\/li>
- The minimum number of galleons needed for wand<\/em>(age<\/em> = 40, power<\/em> = 3) = 3312<\/strong><\/li>
- The minimum number of galleons needed for wand<\/em>(age<\/em> = 40, power<\/em> = 5) = 7587<\/strong><\/li>
- The minimum number of galleons needed for wand<\/em>(age<\/em> = 40, power<\/em> = 7) = 6018<\/strong><\/li><\/ul>\n\n\n\n
The data for wands of age 20<\/em> (code 4): <\/p>\n\n\n\n- The minimum number of galleons needed for wand<\/em>(age<\/em> = 20, power<\/em> = 5) = 504<\/strong><\/li>
- The minimum number of galleons needed for wand<\/em>(age<\/em> = 20, power<\/em> = 6) = 7651<\/strong><\/li>
- The minimum number of galleons needed for wand<\/em>(age<\/em> = 20, power<\/em> = 8) = 3688<\/strong><\/li><\/ul>\n\n\n\n
The data for wands of age 17<\/em> (code 5): <\/p>\n\n\n\n- The minimum number of galleons needed for wand<\/em>(age<\/em> = 17, power<\/em> = 3) = 5689<\/strong><\/li>
- The minimum number of galleons needed for wand<\/em>(age<\/em> = 17, power<\/em> = 10) = 9897<\/strong><\/li><\/ul>\n\n\n\n
<\/span>Solution – Ollivander’s Inventory in SQL<\/strong><\/span><\/h2>\n\n\n\n<\/span>MySQL<\/strong><\/span><\/h3>\n\n\n\nSELECT a.id, \n b.age, \n a.coins_needed, \n a.power \nFROM wands a \n JOIN wands_property b \n ON a.code = b.code \nWHERE b.is_evil = 0 \n AND a.coins_needed = (SELECT Min(a1.coins_needed) \n FROM wands a1 \n JOIN wands_property b1 \n ON a1.code = b1.code \n WHERE b.age = b1.age \n AND a.power = a1.power) \nORDER BY a.power DESC, \n b.age DESC;<\/pre>\n\n\n\n