๐ข ๋ณธ ํฌ์คํ ์ ํ์ฉ๋๋ ๊ธฐ๋ณธ ๋ฌธ์ ๋ฐ ์๋ฃ ์ถ์ฒ๋ HackerRank ์์ ๋ฐํ๋๋ค.
โ ๋ฌธ์
https://www.hackerrank.com/challenges/placements/problem
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.
โ ํ์ด
๋ฌธ์ ์๊ตฌ์ฌํญ
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. (๋ ํ์์ด ๊ฐ์ ๊ธ์ฌ๋ฅผ ์ ๊ณต๋ฐ์ง ์์.)
ํ ์ด๋ธ์ join ํ ๋, s.ID๋ f.ID์ ์กฐ์ธ, f.ID๋ p.ID์ ์กฐ์ธํด์ ๊ธ์ฌ๋ฅผ ํ์(๋น์ฌ์) ๊ธ์ฌ๋ก ํ๋ ์ถ๋ ฅ๊ฒฐ๊ณผ๋ฅผ ์์ฑํ๋ค.
(์ค์) ์ด ๊ฒฐ๊ณผ(T1)์ Package ํ ์ด๋ธ์ ์ถ๊ฐ๋ก ์กฐ์ธํ๋๋ฐ
์ด๋๋ T1.Friend_ID์ p2.ID๋ฅผ ์กฐ์ธํ์ฌ ๋ฒ ์คํธ ํ๋๋์ ๊ธ์ฌ ์ปฌ๋ผ์ ์์ฑํ๋ค.
์ด๋ ๊ฒ ๋๋ฉด ์ต์ข ํ ์ด๋ธ์ ํ์ID, ํ์Name, ๋ฒ ์คํธํ๋๋ ID, ํ์ ๊ธ์ฌ, ๋ฒ ์คํธ ํ๋๋ ๊ธ์ฌ ์ปฌ๋ผ์ด ์์ฑ.
where์ ์ ํ์ ๊ธ์ฌ์ ๋ฒ ์คํธ ํ๋๋ ๊ธ์ฌ ๋น๊ต ์กฐ๊ฑด์ ๋ถ์ฌํ๊ณ
order by์ ์ ๋ฒ ์คํธ ํ๋๋์ ๊ธ์ฌ(p2.Salary)๋ฅผ ์ ๋ ฌ ๊ธฐ์ค์ผ๋ก ์ก์์ค๋ค.
* ํ์ ๊ธ์ฌ์ ํ์์ ๋ฒ ์คํธ ํ๋๋ ๊ธ์ฌ๋ฅผ ๋น๊ตํ ์ ์๋๋ก ํ์(๋น์ฌ์)์ ๋ํ ๊ธ์ฌ ์ปฌ๋ผ์ด ์๋ ํ ์ด๋ธ์ ์์ฑํ๊ณ
์ถ๊ฐ๋ก ํ์๋ณ ๋ฒ ์คํธํ๋๋์ ๊ธ์ฌ ์ปฌ๋ผ์ ์ํด ์กฐ์ธํ๋ ๊ฒ์ ์๊ฐํ๋ ๊ฒ์ด ์ด๋ ค์ ๋ค.
SELECT T1.Name
FROM (SELECT f.ID, s.Name, f.Friend_ID, p.Salary AS mySal
FROM Students s
INNER JOIN Friends f
ON s.ID = f.ID
INNER JOIN Packages p
ON f.ID = p.ID) T1
INNER JOIN Packages p2
ON T1.Friend_ID = p2.ID
WHERE T1.mySal < p2.Salary
ORDER BY p2.Salary;
'Growth ๐ณ > Practice ๐ป' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Hacker Rank] Draw The Triangle 1 (0) | 2022.09.13 |
---|---|
[๊ณผ์ ] Pythob2022 #03 ์ฐ์ต๋ฌธ์ (0) | 2022.08.27 |
[Hacker Rank] Top Competitors (0) | 2022.08.27 |
[Hacker Rank] Ollivander's Inventory (0) | 2022.08.26 |
[LeetCode] 176. Second Highest Salary (0) | 2022.08.26 |