Placements in SQL | HackerRank Solution

Hello coders, today we are going to solve Placements HackerRank Solution in SQL.

Placements

Problem

You are given three tables: Students, Friends and Packages. Students contains two columns: ID and NameFriends contains two columns: ID and Friend_ID (ID of the ONLY best friend). Packages contains two columns: ID and Salary (offered salary in $ thousands per month).

ColumnType
IDInteger
NameString
Students
ColumnType
IDInteger
Friend_IDInteger
Friends
ColumnType
IDInteger
SalaryFloat
Packages

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

Sample Input

IDFriend_Id
12
23
34
41
Friends
IDName
1Ashley
2Samantha
3Julia
4Scarlet
Students
IDName
115.20
210.06
311.55
412.12
Packages

Sample Output

Samantha
Julia
Scarlet

Explanation

Now,

  • Samantha’s best friend got offered a higher salary than her at 11.55
  • Julia’s best friend got offered a higher salary than her at 12.12
  • Scarlet’s best friend got offered a higher salary than her at 15.2
  • Ashley’s best friend did NOT get offered a higher salary than her

The name output, when ordered by the salary offered to their friends, will be:

  • Samantha
  • Julia
  • Scarlet

Solution – Placements in SQL

MySQL

select temp1.sn
from (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
order by temp2.pps asc;

Disclaimer: The above Problem (Placements) is generated by Hacker Rank but the Solution is Provided by CodingBroz. This tutorial is only for Educational and Learning Purpose.

1 thought on “Placements in SQL | HackerRank Solution”

  1. Vikram Singh

    SELECT S.NAME
    FROM STUDENTS S
    JOIN PACKAGES P1
    ON P1.ID = S.ID
    JOIN FRIENDS F
    ON F.ID = S.ID
    JOIN PACKAGES P2
    ON P2.ID = F.FRIEND_ID
    WHERE P2.SALARY>P1.SALARY
    ORDER BY P2.SALARY;

Leave a Comment

Your email address will not be published. Required fields are marked *