SQL, HackerRank, Challenges ํ’€์ด

Journey logยท2022๋…„ 4์›” 15์ผ
0

sql

๋ชฉ๋ก ๋ณด๊ธฐ
2/7

1. ๋ฌธ์ œ

๐Ÿ’ก ๋ฌธ์ œ ๋งํฌ
https://www.hackerrank.com/challenges/challenges/problem?isFullScreen=true&h_r=next-challenge&h_v=zen

1.1 ํ…Œ์ด๋ธ”, ํ•„๋“œ ์ •๋ณด

Hackers

  • hacker_id : (interger)
  • name : (string)

Challenges

  • challenge_id : (integer)
  • hacker_id : (integer) challenges createํ•œ ํ•™์ƒ์˜ id

1.2 ๋ฌธ์ œ ์š”์•ฝ

  • hacker_id, name, total = (์ด challenges ๊ฐœ์ˆ˜) ์ถœ๋ ฅ
  • challenges ํ…Œ์ด๋ธ”์€ hacker ํ…Œ์ด๋ธ”์˜ ๋ถ€๋ถ„์ง‘ํ•ฉ
  • total ๋‚ด๋ฆผ์ฐจ์ˆœ, hacker_id ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌ
  • total ๊ฐ’์ด ๊ฐ™์€ ํ•™์ƒ๋“ค์˜ record๋Š” ์ œ์™ธ. (๋‹จ, total๊ฐ’์ด ์ตœ๋Œ€์ธ ๊ฒฝ์šฐ๋Š” ํฌํ•จ)
    - ๋‹ค์‹œ ๋งํ•ด์„œ, total ๊ฐ’์ด ์ตœ๋Œ€ or total ๊ฐ’์ด ์œ ์ผํ•œ ํ•™์ƒ์ธ ๊ฒฝ์šฐ๋งŒ ์ถœ๋ ฅ

2. ํ’€์ด (1)

-- (1) total ๊ฐ’์ด ์ตœ๋Œ€์ธ ํ•™์ƒ
SELECT H.HACKER_ID, H.NAME, B.TOTAL
FROM (
    SELECT A.TOTAL, COUNT(A.TOTAL) TOTAL_COUNT
    FROM (
        SELECT HACKER_ID, COUNT(CHALLENGE_ID) TOTAL 
        FROM CHALLENGES
        GROUP BY 1
        ) A
    GROUP BY A.TOTAL
    ORDER BY 1 DESC
    LIMIT 1) B
JOIN (
    SELECT HACKER_ID, COUNT(CHALLENGE_ID) TOTAL
    FROM CHALLENGES
    GROUP BY 1
) C ON B.TOTAL = C.TOTAL
JOIN HACKERS H ON H.HACKER_ID=C.HACKER_ID
ORDER BY 3 DESC, 1;

--- (2) total ๊ฐ’์ด ์œ ์ผํ•œ ํ•™์ƒ
SELECT H.HACKER_ID, H.NAME, B.TOTAL
FROM (
    SELECT A.TOTAL, COUNT(A.TOTAL) TOTAL_COUNT
    FROM (
        SELECT HACKER_ID, COUNT(CHALLENGE_ID) TOTAL 
        FROM CHALLENGES
        GROUP BY 1
        ) A
    GROUP BY A.TOTAL
    HAVING TOTAL_COUNT = 1) B
JOIN (
    SELECT HACKER_ID, COUNT(CHALLENGE_ID) TOTAL
    FROM CHALLENGES
    GROUP BY 1
) C ON B.TOTAL = C.TOTAL
JOIN HACKERS H ON H.HACKER_ID=C.HACKER_ID
ORDER BY 3 DESC, 1;

2.1 ๋ฌด์—‡์ด ์ž˜๋ชป๋˜์—ˆ๋‚˜

  • (1) total๊ฐ’์ด ์ตœ๋Œ€์ธ ํ•™์ƒ์„ ๋ถˆ๋Ÿฌ์˜ค๋Š” ์ฟผ๋ฆฌ์™€ (2) total๊ฐ’์ด ์œ ์ผํ•œ ๊ฒฝ์šฐ์˜ ํ•™์ƒ์„ ๋ถˆ๋Ÿฌ์˜ค๋Š” ์ฟผ๋ฆฌ๋ฅผ ๋”ฐ๋กœ ์งœ๊ณ  ์ด์–ด๋ถ™์˜€๋‹ค.
  • (2)์—์„ , total๊ฐ’์ด ์œ ์ผํ•œ ๊ฒฝ์šฐ์˜ total๊ฐ’์„ ๊ฐ€์ ธ์™€์„œ ์ด๋ฅผ key๋กœ inner join ํ–ˆ๋‹ค.
    ์ด๋Ÿฌ๋‹ค ๋ณด๋‹ˆ (1)๊ณผ (2)๋ฅผ or ๊ตฌ๋ฌธ์œผ๋กœ ์ด์–ด๋ถ™์ด์ง€ ๋ชปํ•ด ์ฟผ๋ฆฌ๊ฐ€ ๊ธธ์–ด์กŒ๋‹ค. inner join ๋Œ€์‹  in ์—ฐ์‚ฐ์„ ์ด์šฉํ•œ ํ’€์ด๊ฐ€ ์žˆ์–ด ์ฐธ๊ณ ํ–ˆ๋‹ค.

2. ํ’€์ด (2)

๐Ÿ’ก ์ถœ์ฒ˜ :
https://techblog-history-younghunjo1.tistory.com/157?category=962943

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 -- hacker id๊ฐ€ primary key์—ญํ• ์„ ํ•œ๋‹ค๋ฉด, 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
  • challenges_created(=ํ•™์ƒ ๋ณ„ total ์ฑŒ๋ฆฐ์ง€์ˆ˜)๋ฅผ ๋งŒ๋“ค๊ณ  IN ํ•จ์ˆ˜๋กœ ์กฐ๊ฑด์„ ๋งŒ์กฑํ•˜๋Š” challenges_created๋ฅผ ๊ฐ€์ ธ์™”๋‹ค.
  • ๋ณ„๋„๋กœ challenges_created์˜ ๋นˆ๋„๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ๋ณ€์ˆ˜๋ฅผ ๋งŒ๋“ค์ง€ ์•Š์•˜๋‹ค. ๋Œ€์‹ , group by ๋ฅผ ์“ฐ๊ณ  having count(*)=1 ์œผ๋กœ ๋นˆ๋„์— ๋Œ€ํ•œ ์กฐ๊ฑด๋ฌธ์„ ๊ฑธ์—ˆ๋‹ค.
profile
DEEP DIVER

0๊ฐœ์˜ ๋Œ“๊ธ€