본문 바로가기
  • KEEP HUSTLE!
MYSQL 코테 준비

[HacKerRank SQL] Placements - MYSQL

by 하수군 2021. 3. 14.

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.

 

아래의 조건으로 Name 조회하기

- 자신의 급여(salary)보다 친구의 급여(salary)가 높은 사람의 이름 조회하기

- 이를 친구의 급여(salary) 오름차순

 

SELECT
    S.Name
FROM 
    Students AS S
    INNER JOIN Friends AS F
    ON S.ID = F.ID
    INNER JOIN Packages AS P
    ON F.ID = P.ID
    INNER JOIN Packages AS P2
    ON F.Friend_ID = P2.ID
WHERE P.salary < P2.salary
ORDER BY P2.salary;

 

댓글