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

SQL

[SQL] ๋ณต์žกํ•œ ์„œ๋ธŒ์ฟผ๋ฆฌ ๋ฌธ์ œํ’€์ด(HackerRank - Challenges ๋ฌธ์ œ)

๋ฐ˜์‘ํ˜•

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

 

HackerRank ์‚ฌ์ดํŠธ

 

์ด๋ฒˆ ํฌ์ŠคํŒ…์—์„œ๋Š” ์—ฌ๋Ÿฌ๊ฐ€์ง€ ์„œ๋ธŒ์ฟผ๋ฆฌ๋ฅผ ์ด์šฉํ•ด์•ผ ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ๋Š” HackerRank์˜ Challenges ๋ฌธ์ œ๋ฅผ ๊ฐ™์ด ํ’€์–ด๋ณด๋ ค๊ณ  ํ•œ๋‹ค. ์šฐ์„  ๋ฌธ์ œ ์„ค๋ช…๊ณผ ์ถœ๋ ฅ ํ˜•ํƒœ๋ฅผ ์‚ดํŽด๋ณด์ž. ๋ฌธ์ œ ์›๋ณธ์„ ์‚ดํŽด๋ณด๋ ค๋ฉด ์—ฌ๊ธฐ๋ฅผ ํด๋ฆญํ•ด๋ณด์ž. ๋ฌธ์ œ ์„ค๋ช…์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

 

Julia asked her students to create some coding challenges. Write a query to print the hacker_id, name, and the total number of challenges created by each student. Sort your results by the total number of challenges in descending order. If more than one student created the same number of challenges, then sort the result by hacker_id. If more than one student created the same number of challenges and the count is less than the maximum number of challenges created, then exclude those students from the result.
(์ค„๋ฆฌ์•„๋Š” ํ•™์ƒ๋“ค์—๊ฒŒ ์ฝ”๋”ฉ ๋ฌธ์ œ(challenges)๋ฅผ ๋งŒ๋“ค์–ด๋‹ฌ๋ผ๊ณ  ์š”์ฒญํ–ˆ๋‹ค. ์ด ๋•Œ ๊ฐ ํ•™์ƒ์˜ hacker_id, name, ๋ฌธ์ œ๋ฅผ ๋งŒ๋“  ์ด ๊ฐœ์ˆ˜๋ฅผ ์ถœ๋ ฅ์‹œ์ผœ๋ผ. ์ด ๋•Œ ์ •๋ ฌ์€ ์ด ๋ฌธ์ œ ๊ฐœ์ˆ˜์˜ ๋‚ด๋ฆผ์ฐจ์ˆœ์œผ๋กœ ํ•œ๋‹ค. ๋งŒ์•ฝ ๋˜‘๊ฐ™์€ ๋ฌธ์ œ ๊ฐœ์ˆ˜๋ฅผ ๋งŒ๋“  ํ•™์ƒ์ด 2๋ช… ์ด์ƒ์ด๋ผ๋ฉด, ๊ทธ ๋•Œ๋Š” hacker_id ๋ฅผ ๊ธฐ์ค€์œผ๋กœ ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌ์‹œ์ผœ๋ผ. ๋˜ํ•œ ๋งŒ์•ฝ 2๋ช… ์ด์ƒ์ด ๋˜‘๊ฐ™์€ ๋ฌธ์ œ ๊ฐœ์ˆ˜๋ฅผ ๋งŒ๋“ค์—ˆ์„ ๋•Œ, ๊ทธ ๋ฌธ์ œ ๊ฐœ์ˆ˜๊ฐ€ ๋ฌธ์ œ ๊ฐœ์ˆ˜์˜ ์ตœ๋Œ“๊ฐ’๋ณด๋‹ค ์ž‘๋‹ค๋ฉด ๊ทธ ๋•Œ์˜ ํ•™์ƒ๋“ค์€ ์ถœ๋ ฅ์‹œํ‚ค์ง€ ์•Š์•„์•ผ ํ•œ๋‹ค.)

 

์œ„ ๋ฌธ์ œ์‚ฌํ•ญ๋งŒ ์ฝ๋‹ค๋ณด๋ฉด ๋ฌด์Šจ ๋ง์ธ์ง€ ์ดํ•ด๊ฐ€ ๊ฐ€์ง€ ์•Š์„ ์ˆ˜ ์žˆ๋‹ค. ๋”ฐ๋ผ์„œ ๋ฌธ์ œ ์›๋ณธ์„ ์‚ดํŽด๋ณด๋ฉด์„œ Input / Output ์˜ˆ์‹œ๋ฅผ ๋ณด๋ฉด์„œ ๊ผญ ์ดํ•ดํ•˜๊ณ  ์ด ํฌ์ŠคํŒ…์„ ์ฝ์–ด๋ณด์ž. ๋ฌธ์ œ์—์„œ ์‚ฌ์šฉ๋œ ์˜ˆ์‹œ Output ํ˜•ํƒœ๋ฅผ ์‚ดํŽด๋ณด์ž. 2๊ฐ€์ง€ ๊ฒฝ์šฐ๊ฐ€ ์žˆ๋‹ค.

 

Challenges ๋ฌธ์ œ์˜ ์˜ˆ์‹œ Output

 

๊ทธ๋ฆผ ์† 2 ๊ฐ€์ง€ ์˜ˆ์‹œ์˜ ์ฐจ์ด์ ์€ ์˜์–ด ์„ค๋ช…์—๋„ ์จ์žˆ๋‹ค ์‹œํ”ผ challenges_created ๊ฐ’์ด ๋™์ผํ•œ ๋ฐ์ดํ„ฐ๊ฐ€ 2๊ฐœ ์ด์ƒ์ผ ๋•Œ ๊ทธ ๊ฐ’์ด challenges_created์˜ ์ตœ๋Œ“๊ฐ’๋ณด๋‹ค ์ž‘๋‹ค๋ฉด ์ฟผ๋ฆฌํ•˜์ง€ ์•Š๊ณ  ๋™์ผํ•˜๋‹ค๋ฉด ์ฟผ๋ฆฌ์‹œํ‚ค๋ฉด ๋œ๋‹ค.

์ด์ œ ์œ„ ๋ฌธ์ œ๋ฅผ ํ’€์ดํ•˜๊ธฐ ์œ„ํ•œ ์‚ฌ๊ณ  ๊ณผ์ •์„ ๋‹จ๊ณ„๋ณ„๋กœ ๋‚˜์—ด์‹œ์ผœ๋ณด์ž.

 

1. ์šฐ์„  ๋ฌธ์ œ๋ฅผ ๋งŒ๋“  ๊ฐœ์ˆ˜ ์ฆ‰, challenges_created ๊ฐ’์ด 2๊ฐœ ์ด์ƒ ์ค‘๋ณต๋˜์ง€ ์•Š๊ณ  1๊ฐœ(uniqueํ•œ)์ธ challenges_created ๊ฐ’๋“ค์„ ์•Œ์•„๋‚ด์•ผ ํ•œ๋‹ค. ์ด์— ๋Œ€ํ•œ SQL ๊ตฌ๋ฌธ ํ’€์ด๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

 

SELECT sub2.challenges_created
FROM (SELECT hacker_id, COUNT(*) AS challenges_created
      FROM Challenges
      GROUP BY Challenges.hacker_id) sub2
GROUP BY sub2.challenges_created
HAVING COUNT(*) = 1

 

์šฐ์„  ์œ„ ์ฝ”๋“œ์˜ ์„œ๋ธŒ์ฟผ๋ฆฌ์˜ ์—ญํ• ์€ hacker_id ๋ณ„๋กœ ๋ฌธ์ œ๋ฅผ ๋ช‡ ๊ฐœ๋ฅผ ๋งŒ๋“ค์—ˆ๋Š”์ง€ ์ฟผ๋ฆฌํ•˜๋Š” ์—ญํ• ์„ ํ•œ๋‹ค. ๊ทธ๋ž˜์„œ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ํ˜•ํƒœ๊ฐ€ ๋‚˜์˜ฌ ๊ฒƒ์ด๋‹ค. ํ•˜๋‹จ์˜ ํ…Œ์ด๋ธ”์˜ ๊ฐ’๋“ค์€ ๋ฌธ์ œ์™€ ์—ฐ๊ด€์ด ์—†๋Š” ๊ฐ’๋“ค์ด๋‹ค. ์ถœ๋ ฅ ์นผ๋Ÿผ๋“ค๋งŒ ๋™์ผํ•  ๋ฟ!

 

 

hacker_id challenges_created
1 8
2 12
3 20
4 20
5 24

 

๊ทธ๋ฆฌ๊ณ  ์œ„ ํ…Œ์ด๋ธ” ํ˜•ํƒœ์—์„œ ๋งŒ๋“  ๋ฌธ์ œ ๊ฐœ์ˆ˜์ธ challenges_created๋ฅผ ๊ทธ๋ฃนํ•‘์œผ๋กœ ํ•ด์„œ ๋™์ผํ•œ challenges_created ๊ฐ’์ด ๋ช‡ ๊ฐœ๋‚˜์™”๋Š”์ง€ ์ถœ๋ ฅํ•˜๊ณ  ์ด ๋•Œ 1๊ฐœ์ธ ๊ฐ’๋“ค๋งŒ ์ถœ๋ ฅํ•ด์ค€๋‹ค. ๋”ฐ๋ผ์„œ ์œ„ SQL ๊ตฌ๋ฌธ์„ ์ˆ˜ํ–‰ํ•œ ์ตœ์ข… ์ถœ๋ ฅ ํ˜•ํƒœ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.(์œ„ ์ฝ”๋“œ๋ฅผ ์ˆ˜ํ–‰ํ•˜๋ฉด ํ•˜๋‹จ ํ…Œ์ด๋ธ”์˜ COUNT(*) ์นผ๋Ÿผ์€ ๋‚˜์˜ค์ง€ ์•Š๋Š”๋‹ค๋Š” ์  ์ฐธ๊ณ ํ•˜์ž.)

 

 

challenges_created COUNT(*)
8 1
12 1
24 1

 

 

2. ์ด์ œ challenges_created ๊ฐ’์˜ ์ตœ๋Œ“๊ฐ’์„ ๊ตฌํ•ด๋ณด์ž. ์ด์— ๋Œ€ํ•œ SQL ๊ตฌ๋ฌธ์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

 

SELECT MAX(sub1.challenges_created)
FROM (SELECT COUNT(*) AS challenges_created
      FROM Hackers 
       INNER JOIN Challenges ON Hackers.hacker_id = Challenges.hacker_id
      GROUP BY Hackers.hacker_id) sub1

 

1๋ฒˆ ๋‹จ๊ณ„์— ๋น„ํ•ด ๋น„๊ต์  ๊ฐ„๋‹จํ•˜๋‹ค. ์œ„ ์ฝ”๋“œ์˜ ์„œ๋ธŒ์ฟผ๋ฆฌ ๋ถ€๋ถ„์€ 1๋ฒˆ ๋‹จ๊ณ„์˜ ์„œ๋ธŒ์ฟผ๋ฆฌ ๊ตฌ๋ฌธ๊ณผ ํ•˜๋Š” ์—ญํ• ์ด ๋™์ผํ•˜๋‹ค. ์ฐจ์ด์ ์€ MAX() ๋ฅผ ์ทจํ•ด์ฃผ์—ˆ๋‹ค๋Š” ๊ฒƒ!

 

 

3. ๋งˆ์ง€๋ง‰์œผ๋กœ ์œ„ 1, 2๋ฒˆ ์กฐ๊ฑด์„ ์ถฉ์กฑ์‹œํ‚ค๋Š” challenges_created๋ฅผ ๊ฐ–๋Š” hacker_id, name์„ ์ถœ๋ ฅ์‹œํ‚ค๊ณ  challenges_created ๊ธฐ์ค€์œผ๋กœ ์šฐ์„  ๋‚ด๋ฆผ์ฐจ์ˆœ ์ •๋ ฌ ํ›„ ๋™์ผํ•œ challenges_created๊ฐ’์„ ๊ฐ–๋Š” ๋ฐ์ดํ„ฐ๋“ค ๊ฐ„์—๋Š” hacker_id๋ฅผ ๊ธฐ์ค€์œผ๋กœ ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌ์‹œ์ผœ์ฃผ๋ฉด ๋œ๋‹ค. ์ „์ฒด์ ์ธ SQL ํ’€์ด ๊ตฌ๋ฌธ์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

 

SELECT Hackers.hacker_id, Hackers.name, COUNT(*) AS challenges_created
FROM Hackers
 INNER JOIN Challenges ON Hackers.hacker_id = Challenges.hacker_id
GROUP BY Hackers.hacker_id, Hackers.name
HAVING challenges_created IN (SELECT sub2.challenges_created
                              FROM (SELECT hacker_id, COUNT(*) AS challenges_created
                                    FROM Challenges
                                    GROUP BY Challenges.hacker_id) sub2
                              GROUP BY sub2.challenges_created
                              HAVING COUNT(*) = 1)
    OR challenges_created = (SELECT MAX(sub1.challenges_created)
                             FROM (SELECT COUNT(*) AS challenges_created
                                   FROM Challenges
                                   GROUP BY Challenges.hacker_id) sub1)
ORDER BY challenges_created DESC, Hackers.hacker_id

 

๋ฐ˜์‘ํ˜•