[SQL] HackerRank - Placements

문제: 여기

내가 쓴 답안:

select a.name
from
(select s.id, s.name, p.salary, f.friend_id
from students as s 
    inner join friends as f on s.id = f.id
    inner join packages as p on s.id = p.id) as a
inner join
(select f.friend_id, p.salary as friend_salary
from friends as f inner join packages as p on f.friend_id = p.id) as b
on a.friend_id = b.friend_id
where a.salary < b.friend_salary
order by b.friend_salary;

나는 테이블을 두 개로 나누어서 inner join 을 해주었는데
사실 하나로 쭉 join 해도 무방할 것 같다

아래 답안이 그렇게 한 예시 !

Select S.Name
From ( Students S join Friends F Using(ID)
       join Packages P1 on S.ID=P1.ID
       join Packages P2 on F.Friend_ID=P2.ID)
Where P2.Salary > P1.Salary
Order By P2.Salary;

좋은 웹페이지 즐겨찾기