상세 컨텐츠

본문 제목

challenges(HackerRank)

SQL/MySQL 문제풀이

by 관재탑 2022. 8. 5. 20:41

본문

https://www.hackerrank.com/challenges/challenges/problem?isFullScreen=true 

 

Challenges | HackerRank

Print the total number of challenges created by hackers.

www.hackerrank.com

 

 

 

문제

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.

Input Format

The following tables contain challenge data:

  • Hackers: The hacker_id is the id of the hacker, and name is the name of the hacker. 
  • Challenges: The challenge_id is the id of the challenge, and hacker_id is the id of the student who created the challenge. 

 

 

 

정답쿼리

WITH counter AS ( 
                  SELECT h.hacker_id,
                         h.name,
                         COUNT(*) challenges_created
                  FROM Challenges c
                  INNER JOIN Hackers h ON c.hacker_id = h.hacker_id
                  GROUP BY h.hacker_id, h.name
                  )
SELECT counter.hacker_id
       counter.name,
       counter.challenges_created
FROM counter
WHERE challenges_created = (SELECT MAX(challenges_created) FROM counter)
   OR challenges_created IN (SELECT challenges_created
                             FROM counter
                             GROUP BY challenges_created
                             HAVING COUNT(*) = 1)
ORDER BY counter.challenges_created DESC, counter.hacker_id

 

 

 

해설

with문으로 아이디, 이름, 생성한 문제의 개수를 표시하는 counter 테이블을 만들고 조건을 줘서 문제를 푼다.

 

'SQL > MySQL 문제풀이' 카테고리의 다른 글

Consecutive Numbers(LeetCode)  (0) 2022.08.05
The Report(HackerRank)  (0) 2022.08.05
Department Highest Salary(Leet Code)  (0) 2022.08.05
Delete Duplicate Emails(Leet Code)  (0) 2022.08.02
Swap Salary(LeetCode)  (0) 2022.08.02

관련글 더보기

댓글 영역