본문 바로가기
일일 문제 풀이/HackerRank

[SQL] 해커랭크 HackerRank Placements 문제 풀이

by 엘리는 코딩 마스터 2025. 3. 16.

문제

문제

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.

 

해석

주어진 세 개의 테이블을 이용해서 자신보다 베스트 프렌드가 더 높은 월급을 받는 학생들의 이름을 출력하는 쿼리를 작성해라

 

 

정답

SELECT s.Name
FROM Students s
JOIN Friends f ON s.ID = f.ID
JOIN Packages p ON s.ID = p.ID
JOIN Packages pf ON f.Friend_ID = pf.ID
WHERE p.Salary < pf.Salary
ORDER BY pf.Salary;

 

배운 점

  • join을 여러번 해도 된다
  • 한 테이블이 다른 테이블 여러개에 조인해도 된다.
  • 해당 문제는 패키지 테이블을 학생과 친구에 진행했고 거기에 맞춰 조건을 쉽게 주었다.