본문 바로가기
MySQL/문제풀이

[HackerRank] Placements

by MINNI_ 2021. 3. 23.

1. 문제

 

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

 

Sample Output

Samantha

Julia

Scarlet


Explanation

See the following table:

 

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

2. 답

SELECT s.name
FROM friends f
    LEFT JOIN students s ON f.id = s.id
    LEFT JOIN packages my_p ON f.id = my_p.id
    LEFT JOIN packages fp ON f.friend_id = fp.id
WHERE my_p.salary < fp.salary
ORDER BY fp.salary

3. KEY POINT

 

  • package를 LEFT JOIN을 2번 사용하지만 쓰임새가 다르므로, 별명을 붙여주어 다르게 사용하기

 

www.hackerrank.com/challenges/placements/problem?h_r=internal-search

 

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

 

댓글