Packages<\/strong><\/figcaption><\/figure>\n\n\n\nWrite a query to output the names of those students whose best friends got offered a higher salary than them. Names must be ordered by the salary amount offered to the best friends. It is guaranteed that no two students got same salary offer.<\/p>\n\n\n\n
Sample Input<\/strong><\/p>\n\n\n\nID<\/th> | Friend_Id<\/th><\/tr><\/thead> |
---|
1<\/em><\/td>2<\/em><\/td><\/tr>2<\/em><\/td>3<\/em><\/td><\/tr>3<\/em><\/td>4<\/em><\/td><\/tr>4<\/em><\/td>1<\/em><\/td><\/tr><\/tbody><\/table>Friends<\/strong><\/figcaption><\/figure>\n\n\n\nID<\/th> | Name<\/th><\/tr><\/thead> |
---|
1<\/em><\/td>Ashley<\/em><\/td><\/tr>2<\/em><\/td>Samantha<\/em><\/td><\/tr>3<\/em><\/td>Julia<\/em><\/td><\/tr>4<\/em><\/td>Scarlet<\/em><\/td><\/tr><\/tbody><\/table>Students<\/strong><\/figcaption><\/figure>\n\n\n\nID<\/th> | Name<\/th><\/tr><\/thead> |
---|
1<\/em><\/td>15.20<\/em><\/td><\/tr>2<\/em><\/td>10.06<\/em><\/td><\/tr>3<\/em><\/td>11.55<\/em><\/td><\/tr>4<\/em><\/td>12.12<\/td><\/tr><\/tbody><\/table>Packages<\/strong><\/figcaption><\/figure>\n\n\n\n Sample Output<\/strong><\/p>\n\n\n\nSamantha\nJulia\nScarlet<\/code><\/pre>\n\n\n\n<\/span>Explanation<\/strong><\/span><\/h2>\n\n\n\nNow,<\/p>\n\n\n\n - Samantha’s<\/em> best friend got offered a higher salary than her at 11.55<\/li>
- Julia’s<\/em> best friend got offered a higher salary than her at 12.12<\/li>
- Scarlet’s<\/em> best friend got offered a higher salary than her at 15.2<\/li>
- Ashley’s<\/em> best friend did NOT get offered a higher salary than her<\/li><\/ul>\n\n\n\n
The name output, when ordered by the salary offered to their friends, will be:<\/p>\n\n\n\n - Samantha<\/em><\/li>
- Julia<\/em><\/li>
- Scarlet<\/em><\/li><\/ul>\n\n\n\n
<\/span>Solution – Placements in SQL <\/strong><\/span><\/h2>\n\n\n\n<\/span>MySQL<\/strong><\/span><\/h3>\n\n\n\nselect temp1.sn\nfrom (select S.ID si,S.Name sn,P.Salary ps from Students S join Packages P on S.ID=P.ID) temp1 join (select FF.ID fi,FF.Friend_ID fd,PP.Salary pps from Friends FF join Packages PP on FF.Friend_ID=pp.ID) temp2 on temp1.si=temp2.fi and temp1.ps<temp2.pps\norder by temp2.pps asc;<\/pre>\n\n\n\n
| | | | | | | |
| | | | | | | |
| | | | | | | |