Growth ๐ŸŒณ/Practice ๐Ÿ’ป

[Hacker Rank] Placement

์ธ” 2022. 8. 27. 02:20

๐Ÿ“ข ๋ณธ ํฌ์ŠคํŒ…์— ํ™œ์šฉ๋˜๋Š” ๊ธฐ๋ณธ ๋ฌธ์ œ ๋ฐ ์ž๋ฃŒ ์ถœ์ฒ˜๋Š” HackerRank ์ž„์„ ๋ฐํž™๋‹ˆ๋‹ค.

https://www.hackerrank.com


โ–  ๋ฌธ์ œ

https://www.hackerrank.com/challenges/placements/problem

 

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

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