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절과 조인
'데이터 분석 > SQL 연습문제' 카테고리의 다른 글
262. Trips and Users (LeetCode) (0) | 2023.03.27 |
---|---|
Weather Observation Station 20 (HackerRank) (0) | 2022.11.07 |
Weather Observation Station 11 (HackerRank) (0) | 2022.11.06 |
Ollivander's Inventory (HackerRank) (0) | 2022.11.05 |
SQL Project Planning (HackerRank) (0) | 2022.11.04 |
댓글