본문 바로가기
데이터 분석/SQL 연습문제

Placements (HackerRank)

2023. 3. 28.
 

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

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.

풀이

제출한 답

with sub as (
select 
    s.id as id,
    s.name as name,
    f.friend_id as friend_id,
    p.salary as salary
from students as s 
join friends as f on s.id = f.id
join packages as p on s.id = p.id
)

select
    sub.name
from sub join packages as p on sub.friend_id = p.id
where sub.salary < p.salary
order by p.salary

-> students의 salary 를 구하기 위해 조인 2회 수행, with절 / friend의 salary를 구하기 위해 with절과 조인

2023.03.28

댓글