관리 메뉴

오늘도 배운다

Placements / HackerRank, SQL, MySQL 본문

코딩테스트연습(SQL)

Placements / HackerRank, SQL, MySQL

LearnerToRunner 2023. 2. 17. 23:44

문제

source: HackerRank
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.

 

 

 

 

제출답안(MySQL)

SELECT s.name
FROM students AS s 
    LEFT JOIN packages AS my_s ON s.id = my_s.id
    LEFT JOIN friends AS f ON s.id = f.id
    LEFT JOIN packages AS frd_s ON f.friend_id = frd_s.id
WHERE my_s.salary < frd_s.salary
ORDER BY frd_s.salary

 

 

 

문제 바로가기(MySQL)

728x90
Comments