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

SQL

[SQL] HackerRank - Contest Leaderboard ๋ฌธ์ œ

๋ฐ˜์‘ํ˜•

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

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

 

HackerRank ์‚ฌ์ดํŠธ

 

์ด๋ฒˆ ํฌ์ŠคํŒ…์—์„œ๋Š” HackerRank์˜ Contest Leaderboard ๋ฌธ์ œ๋ฅผ ํ’€์–ด๋ณด๋ ค๊ณ  ํ•œ๋‹ค. ๋ฌธ์ œ ์›๋ณธ์€ ์—ฌ๊ธฐ๋ฅผ ํด๋ฆญํ•ด ํ™•์ธํ•ด๋ณด๊ณ  ๋ฌธ์ œ์—์„œ ์š”๊ตฌํ•˜๋Š” ์‚ฌํ•ญ์„ ํ™•์ธํ•ด๋ณด์ž.

 

The total score of a hacker is the sum of their maximum scores for all of the challenges. Write a query to print thehacker_id,name, and total score of the hackers ordered by the descending score. If more than one hacker achieved the same total score, then sort the result by ascendinghacker_id. Exclude all hackers with a total score of '0' from your result.
(ํ•œ ๋ช… hacker์˜ total score๋ž€, ๊ทธ hacker๊ฐ€ ํ‘ผ ๋ชจ๋“  ๋ฌธ์ œ๋“ค์— ๋Œ€ํ•œ score์˜ ์ตœ๋Œ“๊ฐ’์˜ ํ•ฉ์„ ์˜๋ฏธํ•œ๋‹ค. ์ด ๋•Œ hacker_id, name, total_score๋ฅผ ์ถœ๋ ฅํ•˜๋Š”๋ฐ 1์ฐจ์ ์ธ ์ •๋ ฌ ๊ธฐ์ค€์€ total_score๋ฅผ ๋‚ด๋ฆผ์ฐจ์ˆœ์œผ๋กœ, 2์ฐจ์ ์ธ ์ •๋ ฌ ๊ธฐ์ค€์€ hacker_id ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌํ•ด๋ผ. ๋‹จ, ๊ฒฐ๊ณผ๊ฐ’์—์„œ total score๊ฐ€ 0์ธ ๋ฐ์ดํ„ฐ๋“ค์€ ์ œ์™ธ์‹œ์ผœ๋ผ.)

 

๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•œ ๊ฐ€์žฅ ์ค‘์š”ํ•œ ํฌ์ธํŠธ๋Š” total score๋ฅผ ์–ด๋–ป๊ฒŒ ๊ณ„์‚ฐํ•  ์ˆ˜ ์žˆ๋Š”์ง€์ด๋‹ค. total score์— ๋Œ€ํ•œ ์ง๊ด€์ ์ธ ์ดํ•ด๋Š” ๋ฌธ์ œ ์›๋ณธ์— ๋“ค์–ด๊ฐ€์„œ Sample I/O๋ฅผ ์‚ดํŽด๋ณด๋ฉด ๋„์›€์ด ๋  ๊ฒƒ์ด๋‹ค.

 

ํ•œ ๋ช…์˜ total score๊ฐ€ ๋ฌด์—‡์ธ์ง€ ์•Œ์•„๋ณด๊ธฐ ์œ„ํ•ด ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๋ฐ์ดํ„ฐ๊ฐ€ ์žˆ๋‹ค๊ณ  ๊ฐ€์ •ํ•ด๋ณด์ž.

 

Name Challenge Score
John 23 50
John 23 70
John 21 80

 

์œ„ ๋ฐ์ดํ„ฐ๋Š” John์ด 2๊ฐœ์˜ ๋ฌธ์ œ์ธ 21๋ฒˆ, 23๋ฒˆ์— ๋Œ€ํ•œ ์ ์ˆ˜์ด๋‹ค. ์ด ๋•Œ 21๋ฒˆ์€ 1๋ฒˆ ํ’€์—ˆ๊ธฐ ๋•Œ๋ฌธ์— 21๋ฒˆ์— ๋Œ€ํ•œ score ์ตœ๋Œ“๊ฐ’์€ ๊ทธ๋ƒฅ 80์ ์ด ๋œ๋‹ค. ๋ฐ˜๋ฉด์— 23๋ฒˆ์€ ๋‘๋ฒˆ์„ ํ’€์—ˆ๋Š”๋ฐ ํ•œ ๋ฒˆ์€ 50์ , ํ•œ ๋ฒˆ์€ 70์ ์„ ๋งž์•˜๋‹ค. ์ด ๋•Œ score์˜ ์ตœ๋Œ“๊ฐ’์€ 70์ ์ด ๋œ๋‹ค. ๋”ฐ๋ผ์„œ ์ตœ์ข…์ ์ธ John์˜ total score๋Š” 70 + 80 ์ธ 150์ ์ด ๋˜๊ฒŒ ๋œ๋‹ค.

 

์ด์ œ total score ๊ฐœ๋…์„ ์•Œ์•˜๋‹ค. ๊ทธ๋ ‡๋‹ค๋ฉด ์ด๋ฅผ ๊ณ„์‚ฐํ•˜๊ธฐ ์œ„ํ•ด์„œ ์–ด๋–ป๊ฒŒ ํ•ด์ฃผ์–ด์•ผ ํ• ๊นŒ? ๋ฐ”๋กœ hacker_id์™€ challenge_id ๋‘ ๊ฐœ์˜ ์นผ๋Ÿผ ๊ธฐ์ค€์œผ๋กœ ๋™์‹œ์— ๊ทธ๋ฃนํ•‘์„ ํ•ด์ฃผ์–ด์•ผ ํ•œ๋‹ค. ๊ทธ๋ž˜์•ผ ์–ด๋–ค hacker_id์— ๋Œ€ํ•ด์„œ ๊ทธ hacker_id๊ฐ€ ํ’€์€ ๋ชจ๋“  challenge์— ๋Œ€ํ•œ score๊ฐ’์ด ๋‚˜์˜ฌํ…Œ๋‹ˆ๊นŒ!

 

์ด์ œ ์ด๋ฅผ ํ™œ์šฉํ•œ ์ตœ์ข…์ ์ธ ์ •๋‹ต SQL ๊ตฌ๋ฌธ์„ ์‚ดํŽด๋ณด์ž.

 

SELECT H.hacker_id, H.name, sub2.total_score
FROM (SELECT sub.hacker_id, SUM(max_score) AS total_score
      FROM (SELECT hacker_id, challenge_id, MAX(score) AS max_score
            FROM Submissions 
            GROUP BY hacker_id, challenge_id) sub
      GROUP BY sub.hacker_id
      HAVING total_score != 0) sub2
 INNER JOIN Hackers H ON sub2.hacker_id = H.hacker_id
ORDER BY sub2.total_score DESC, H.hacker_id

 

๊ฐ€์žฅ ๋‚ด๋ถ€์— ์กด์žฌํ•˜๋Š” ์„œ๋ธŒ์ฟผ๋ฆฌ๋กœ ๊ฐ hacker_id์— ๋Œ€ํ•ด์„œ ์ด hacker_id๊ฐ€ ํ’€์€ challenges ์ค‘ ๊ฐ€์žฅ ๋†’์€ score๊ฐ’์„ ๋„์ถœํ•ด๋‚ด๊ณ  ์ด score๋“ค์„ ๋”ํ•œ total score๋ฅผ ๋งŒ๋“ค๊ธฐ ์œ„ํ•ด ๋‹ค์‹œ ํ•œ ๋ฒˆ hacker_id๋ฅผ ๊ธฐ์ค€์œผ๋กœ ๊ทธ๋ฃนํ•‘์„ ํ•ด์ค€๋‹ค.

 

๋งˆ์ง€๋ง‰์œผ๋กœ ๊ฐ hacker_id์— ๋Œ€ํ•œ name์„ ๊ฐ™์ด ์ถœ๋ ฅํ•ด์ฃผ๊ธฐ ์œ„ํ•ด์„œ INNER JOIN์„ ์‚ฌ์šฉํ•˜๊ณ  ์ •๋ ฌ ๊ธฐ์ค€ 2๊ฐ€์ง€๋ฅผ ๋งŒ์กฑ์‹œ์ผœ ์ค€๋‹ค.

 

ํ•ด๋‹น ๋ฌธ์ œ๋Š” ๋‹ค๋ฅธ Medium ๋‚œ์ด๋„์˜ ๋ฌธ์ œ๋ณด๋‹ค ๋น ๋ฅธ ์‹œ๊ฐ„ ๋‚ด์— ํ’€์—ˆ๋‹ค. ์•„๋งˆ๋„ ๋ฌธ์ œ ์ดํ•ด์— ์žˆ์–ด์„œ ์ง๊ด€์ ์ธ ์ดํ•ด๊ฐ€ ์ž˜ ๋œ ๋•๋ถ„์ธ ๋“ฏ ํ•˜๋‹ค.

๋ฐ˜์‘ํ˜•