티스토리 뷰

728x90

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

 

SELECT S.NAME
FROM (
	--본인 SALARY
    SELECT A.ID, B.Salary, A.Friend_ID
    FROM Friends A
        JOIN Packages B
        ON A.ID = B.ID
    ) M
    JOIN (
    --친구 SALARY
    SELECT A.Friend_ID, B.Salary
    FROM Friends A
        JOIN Packages B
        ON A.Friend_ID = B.ID    
    ) F
    ON M.Friend_ID = F.Friend_ID
    AND M.Salary < F.Salary --본인보다 친구의 SALARY가 높은경우
    JOIN Students S
    ON M.ID = S.ID 
ORDER BY F.Salary

- 1차풀이 - 20분

- 11/16일 2차풀이 - 10분

728x90
LIST

'Programming > MySQL' 카테고리의 다른 글

[MySQL/Hackerrank] Interviews  (0) 2021.11.07
[MySQL/Hackerrank] Symmetric Pairs  (0) 2021.11.06
[MySQL/Hackerrank] SQL Project Planning  (0) 2021.11.06
[MySQL/Hackerrank] Contest Leaderboard  (0) 2021.11.04
[MySQL/Hackerrank] Challenges  (0) 2021.11.04
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
글 보관함