SQL/HackerRank
[MySQL/HackerRank] Placements 문제풀이
레아킴
2022. 5. 22. 19:23
반응형
https://www.hackerrank.com/challenges/placements/problem
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
문제 설명
각 학생의 friend 의 salary 가 해당 학생의 salary 보다 높은 학생의 이름을 출력하세요.
( 본인의 salary의 오름차순으로 정렬하시오)
나는 packages 테이블을 2번 조인하는 방법으로 풀었다. (1개는 본인의 salary, 다른 1개는 친구의 salary와 매치가 되도록 조인하였다.)
생각보다 간단한 문제였던 것 같다.
정답 쿼리
SELECT s.name
FROM students s
JOIN friends f on s.id = f.id
JOIN packages p1 on p1.id = s.id
JOIN packages p2 on p2.id = f.friend_id
WHERE p1.salary < p2.salary
ORDER BY p2.salary
반응형