cs

https://www.hackerrank.com/challenges/placements/problem?isFullScreen=true 

 

Placements | HackerRank

Write a query to output the names of those students whose best friends got offered a higher salary than them.

www.hackerrank.com

 


You are given three tables: Students, Friends and Packages. Students contains two columns: ID and Name. Friends 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).

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

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

 

 

[My Answer]

SELECT s.Name
FROM Students S
INNER JOIN 
Packages P 
ON S.ID = P.ID
INNER JOIN 
Friends F 
ON S.ID = F.ID
INNER JOIN 
Packages p2 
ON F.Friend_ID = P2.ID
WHERE P2.Salary > P.Salary
ORDER BY P2.Salary;

 

저렇게 하나의 테이블의 이름 두개로 만들고 조인하면, 좀 오래 걸리는 것 같은데 다른 방법있는지 확인해봐야겠음..

 

 

'Code Problems > SQL_ HackerRank' 카테고리의 다른 글

[Hacker Rank] The Report  (0) 2022.02.06
[Hacker Rank] Symmetric Pairs  (0) 2022.01.24
[HackerRank] Average Population of Each Continent  (0) 2022.01.04
[HackerRank] African Cities  (0) 2022.01.03
[HackerRank] Population Census  (0) 2021.12.31

+ Recent posts