๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

SQL

[SQL] HackerRank - Top Competitors ๋ฌธ์ œํ’€์ด

๋ฐ˜์‘ํ˜•

๐Ÿ”Š ๋ณธ ํฌ์ŠคํŒ…์—์„œ ์‚ฌ์šฉ๋˜๋Š” ํ…Œ์ด๋ธ”์˜ ์ž๋ฃŒ์™€ ์ถœ์ฒ˜๋Š” HackerRank ์ž„์„ ๋ฐํž™๋‹ˆ๋‹ค. ๋” ๋‹ค์–‘ํ•œ SQL ๋ฌธ์ œ๋ฅผ ํ’€์–ด๋ณด์‹œ๋ ค๋ฉด

HackerRank ์‚ฌ์ดํŠธ๋ฅผ ๋ฐฉ๋ฌธํ•ด ๋ณด์„ธ์š”!

 

HackerRank ์‚ฌ์ดํŠธ

 

์ด๋ฒˆ ํฌ์ŠคํŒ…์—์„œ๋Š” ํ•ด์ปค๋žญํฌ ์‚ฌ์ดํŠธ์˜ Top Competitors ๋ฌธ์ œ๋ฅผ ํ’€์ดํ•ด๋ณด์ž. ๋ฌธ์ œ ์›๋ณธ์€ ์—ฌ๊ธฐ๋ฅผ ํด๋ฆญํ•ด ์ฝ์–ด๋ณด์ž.

๋ฌธ์ œ์—์„œ ์ฃผ์–ด์ง„ ํ…Œ์ด๋ธ”๋“ค์€ ๋ฌธ์ œ ์›๋ณธ์„ ์‚ดํŽด๋ณด๊ณ  ์—ฌ๊ธฐ์—์„œ๋Š” ์–ด๋–ค ์ถœ๋ ฅ์˜ ๋‚ด์šฉ์€ ์š”๊ตฌํ•˜๋Š”์ง€์— ๋Œ€ํ•ด์„œ ์ดํ•ดํ•ด๋ณด์ž.

 

Julia just finished conducting a coding contest, and she needs your help assembling the leaderboard! Write a query to print the respectivehacker_idandnameof hackers who achieved full scores formore than onechallenge. Order your output in descending order by the total number of challenges in which the hacker earned a full score. If more than one hacker received full scores in same number of challenges, then sort them by ascendinghacker_id.
(์ค„๋ฆฌ์•„๋Š” ์ฝ”๋”ฉ ๋Œ€ํšŒ๋ฅผ ๋งˆ์ณค๊ณ  ์ฐธ๊ฐ€์ž๋“ค์˜ ์ ์ˆ˜ ๋ฆฌ๋”๋ณด๋“œ๋ฅผ ๋งŒ๋“œ๋ ค๊ณ  ํ•œ๋‹ค. 2๊ฐœ ์ด์ƒ์˜ Challenge(๋ฌธ์ œ)์—์„œ Full score(๋งŒ์ )๋ฅผ ๋ฐ›์€ Hacker(์ฐธ๊ฐ€์ž)๋“ค์˜ ์ด๋ฆ„๊ณผ hacker_id๋ฅผ ์ถœ๋ ฅํ•ด๋ผ. ์ด ๋•Œ ๊ฒฐ๊ณผ ํ™”๋ฉด์„ Full score๋ฅผ ๋ฐ›์€ Challenge์˜ ๊ฐœ์ˆ˜๋ฅผ ๊ธฐ์ค€์œผ๋กœ ๋‚ด๋ฆผ์ฐจ์ˆœ ์ •๋ ฌํ•˜๊ณ  ๋งŒ์•ฝ Full score๋ฅผ ๋ฐ›์€ Challenge ๊ฐœ์ˆ˜๊ฐ€ ๋™์ผํ•˜๋‹ค๋ฉด hacker_id ๋ฅผ ๊ธฐ์ค€์œผ๋กœ ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌํ•ด๋ผ.)

 

์ด์ œ ์šฐ๋ฆฌ๊ฐ€ ํ•ด์•ผ ํ•˜๋Š” ์ผ์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

 

  1. Full score(๋งŒ์ )์„ ๋ฐ›์€ ์ฑŒ๋ฆฐ์ง€๊ฐ€ 2๊ฐœ ์ด์ƒ์ธ ์ฐธ๊ฐ€์ž๋“ค์„ ์ฐพ์ž.
  2. ์ด ๋•Œ ๋งŒ์ ์ด๋ž€, Submissions ํ…Œ์ด๋ธ”์˜ score ๊ฐ’๊ณผ Difficulty ํ…Œ์ด๋ธ”์˜ score ๊ฐ’๊ณผ ์ผ์น˜ํ•˜๋Š” ๊ฒƒ์„ ์˜๋ฏธ
  3. 2๊ฐ€์ง€ ์ •๋ ฌ ๊ธฐ์ค€์„ ๋งŒ์กฑํ•ด์„œ ์ถœ๋ ฅ์‹œํ‚ค๊ธฐ

์šฐ๋ฆฌ๋Š” ์ด๋ฅผ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•ด ์ง€๋‚œ ํฌ์ŠคํŒ…์—์„œ ํ–ˆ๋˜ ๊ฒƒ์ฒ˜๋Ÿผ ์—ฐ์†์ ์ธ JOIN ๊ตฌ๋ฌธ์„ ์‚ฌ์šฉํ•ด์„œ ํ•ด๊ฒฐํ•  ๊ฒƒ์ด๋‹ค. ๋จผ์ € ์ •๋‹ต SQL ๊ตฌ๋ฌธ์„ ์‚ดํŽด๋ณด์ž.

 

SELECT H.hacker_id, H.name
FROM Submissions S
 INNER JOIN Challenges C ON S.challenge_id = C.challenge_id
 INNER JOIN Difficulty D ON C.difficulty_level = D.difficulty_level
 INNER JOIN Hackers H ON S.hacker_id = H.hacker_id
WHERE S.score = D.score AND C.difficulty_level = D.difficulty_level
GROUP BY H.hacker_id, H.name
HAVING COUNT(H.hacker_id) > 1
ORDER BY COUNT(H.hacker_id) DESC, H.hacker_id

 

์ด์ œ INNER JOIN ๊ตฌ๋ฌธ ํ•˜๋‚˜๋ฅผ ์˜ˆ์‹œ๋กœ ์‚ดํŽด๋ณด์ž. ์šฐ์„  3๋ฒˆ์งธ ๋ผ์ธ์˜ Submissions ํ…Œ์ด๋ธ”๊ณผ Challenges ํ…Œ์ด๋ธ”์„ INNER JOIN ํ•˜๊ฒŒ ๋˜๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์•„์ง„๋‹ค.(ํ…Œ์ด๋ธ” ๊ฐ’๋“ค์˜ ์˜ˆ์‹œ๋Š” ํ•„์ž๊ฐ€ ์ž„์˜๋กœ ๋งŒ๋“  ๊ฒƒ์ž„์„ ์ฐธ๊ณ ํ•˜์ž)

 

submission_id hacker_id
(๋ฌธ์ œ ํ’€์ดํ•œhacker_id)
challenge_id score(๋งž์€ ์ ์ˆ˜) challenge_id hacker_id
(๋ฌธ์ œ๋ฅผ ๋งŒ๋“  hacker_id)
difficulty_level
1๋ฒˆ  ์ฒ ์ˆ˜ 3๋ฒˆ ๋ฌธ์ œ 60์  3๋ฒˆ ๋ฌธ์ œ ๋‹ค์†œ 5
2๋ฒˆ ๊ธฐ์˜ 9๋ฒˆ ๋ฌธ์ œ 100์  9๋ฒˆ ๋ฌธ์ œ ์œ ๋ฆฌ 7
3๋ฒˆ ๋‹ค์†œ 12๋ฒˆ ๋ฌธ์ œ 30์  12๋ฒˆ ๋ฌธ์ œ ๊ธฐ์˜ 3

 

 

์œ„ ์กฐ์ธ์‹œํ‚จ ํ…Œ์ด๋ธ”์„ ๋ณด์•˜์„ ๋•Œ, ํฌ์ธํŠธ ๋ถ€๋ถ„์€ 2๊ฐœ์˜ hacker_id ๊ฐ’์ด ์žˆ๋Š”๋ฐ ๊ฐ์ž ์˜๋ฏธํ•˜๋Š” ๋ฐ”๊ฐ€ ๋‹ค๋ฅด๋‹ค๋Š” ๊ฒƒ์ด๋‹ค. ์ฆ‰, challenge_id ๊ฐ’์€ ๋™์ผํ•˜์ง€๋งŒ ๊ทธ challenge๋ฅผ ๋ˆ„๊ฐ€ ๋ƒˆ๊ณ  ๋ˆ„๊ฐ€ ํ’€์ดํ–ˆ๋Š”์ง€๋Š” ๋‹ค๋ฅด๋‹ค๋Š” ๊ฒƒ์ด๋‹ค! ์œ„ ํ…Œ์ด๋ธ”์„ ๋ณด๋ฉด ๋‹ค์†œ์ด๋Š” 3๋ฒˆ ๋ฌธ์ œ๋ฅผ ๋งŒ๋“ค๊ธฐ๋„ ํ–ˆ์ง€๋งŒ 12๋ฒˆ ๋ฌธ์ œ๋ฅผ ํ’€๊ธฐ๋„ ํ–ˆ์Œ์„ ์•Œ ์ˆ˜ ์žˆ๋‹ค.

 

์ด์ œ ์œ„์™€ ๊ฐ™์€ ๋ฐฉ์‹์œผ๋กœ ์—ฐ์†์ ์œผ๋กœ Difficulty ํ…Œ์ด๋ธ”๊ณผ Hackers ํ…Œ์ด๋ธ”์„ ์—ฐ์†์ ์œผ๋กœ INNER JOIN ์‹œ์ผœ์ฃผ์ž.

 

์ด์ œ WHERE ๊ตฌ๋ฌธ์ ˆ์—๋„ ์จ์žˆ๋‹ค์‹œํ”ผ Full score๋ฅผ ๋งž์€ ์ฐธ๊ฐ€์ž๋“ค๋งŒ ํ•„ํ„ฐ๋ง์‹œ์ผœ์ฃผ์–ด์•ผ ํ•˜๋Š”๋ฐ, ์กฐ๊ฑด์„ 2๊ฐœ๋กœ ๋‹ฌ์•„์ค€๋‹ค. ์ฆ‰, ๋ฌธ์ œ์˜ ๋งŒ์  ๊ธฐ์ค€ ์ ์ˆ˜์™€ ์ฐธ๊ฐ€์ž๊ฐ€ ํ’€๊ณ  ๋‚œ ์ ์ˆ˜๊ฐ€ ๋™์ผํ•˜๋ฉด์„œ ๋ฌธ์ œ์˜ ๋‚œ์ด๋„๋„ ๋™์ผํ•ด์•ผ ํ•œ๋‹ค.

 

๊ทธ๋ฆฌ๊ณ  Full score๋ฅผ ๋งž์€ ๋ฌธ์ œ๊ฐ€ 2๊ฐœ ์ด์ƒ์ธ ์‚ฌ๋žŒ๋“ค์„ ์•Œ์•„๋ณด๊ธฐ ์œ„ํ•ด hacker_id, name์„ ๊ธฐ์ค€์œผ๋กœ ๊ทธ๋ฃนํ•‘์„ ์‹œ์ผœ์ฃผ๊ณ  hakcer_id๋ฅผ COUNTํ•ด์ค€๋‹ค. ์ด ๋•Œ COUNT(hacker_id) ํ•œ ๊ฐ’์ด 1 ๋ณด๋‹ค ํด ๋•Œ ๋ฐ”๋กœ Full score๋ฅผ ๋งž์€ ๋ฌธ์ œ๊ฐ€ 2๊ฐœ ์ด์ƒ์ธ hacker_id, name๋งŒ ๋‚จ๊ฒŒ ๋œ๋‹ค.

 

๋งˆ์ง€๋ง‰์œผ๋กœ COUNT(hacker_id)ํ•ด์ค€ ๊ฐ’์„ ์ตœ์šฐ์„  ์ •๋ ฌ ๊ธฐ์ค€์œผ๋กœ ์‚ฌ์šฉํ•ด์ฃผ๊ณ  ๋™์ผํ•œ COUNT(hacker_id)๊ฐ’์„ ๊ฐ€์งˆ ๋•Œ๋Š” hacker_id ๊ธฐ์ค€์œผ๋กœ ์ •๋ ฌํ•ด์ฃผ๋„๋ก ํ•˜์ž.

๋ฐ˜์‘ํ˜•