문제
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을 여러번 해도 된다
- 한 테이블이 다른 테이블 여러개에 조인해도 된다.
- 해당 문제는 패키지 테이블을 학생과 친구에 진행했고 거기에 맞춰 조건을 쉽게 주었다.
'일일 문제 풀이 > HackerRank' 카테고리의 다른 글
[SQL] 해커랭크 HackerRank Interviews 문제 풀이 (0) | 2025.03.19 |
---|---|
[SQL] 해커랭크 HakerRank 문제 풀이 Symmetric Pairs (0) | 2025.03.17 |
[SQL] 해커랭크 HackerRank New Companies 문제 풀이 (0) | 2025.03.15 |
[SQL] 해커랭크 HackerRank Binary Tree Nodes 문제 풀이 (0) | 2025.03.14 |
[SQL] 해커랭크 HackerRank Occupations 문제 풀이 (0) | 2025.03.13 |