[HackerRank] Top Competitors

당당·2023년 7월 18일
0

HackerRank

목록 보기
15/27

https://www.hackerrank.com/challenges/full-score/problem

📔문제

Julia just finished conducting a coding contest, and she needs your help assembling the leaderboard! Write a query to print the respective hacker_id and name of hackers who achieved full scores for more than one challenge. 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 ascending hacker_id.

The following tables contain contest data:

  • Hackers: The hacker_id is the id of the hacker, and name is the name of the hacker.

  • Difficulty: The difficult_level is the level of difficulty of the challenge, and score is the score of the challenge for the difficulty level.

  • Challenges: The challenge_id is the id of the challenge, the hacker_id is the id of the hacker who created the challenge, and difficulty_level is the level of difficulty of the challenge.

  • Submissions: The submission_id is the id of the submission, hacker_id is the id of the hacker who made the submission, challenge_id is the id of the challenge that the submission belongs to, and score is the score of the submission.


📝예시

Hackers Table:

Difficulty Table:

Challenges Table:

Submissions Table:

90411 Joe

Hacker 86870 got a score of 30 for challenge 71055 with a difficulty level of 2, so 86870 earned a full score for this challenge. Hacker 90411 got a score of 30 for challenge 71055 with a difficulty level of 2, so 90411 earned a full score for this challenge. Hacker 90411 got a score of 100 for challenge 66730 with a difficulty level of 6, so 90411 earned a full score for this challenge. Only hacker 90411 managed to earn a full score for more than one challenge, so we print the their hacker_id and name as 2 space-separated values.


🧮분야

  • JOIN

📃SQL 코드

select h.hacker_id||'  '||name
from hackers h, difficulty d, challenges c, submissions s
where s.hacker_id=h.hacker_id
and s.challenge_id=c.challenge_id
and c.difficulty_level=d.difficulty_level --JOIN
and d.score=s.score
group by h.hacker_id, name
having count(*)>=2
order by count(*) desc, h.hacker_id;

📰출력 결과


📂고찰

해커가 만점을 얻은 총 챌린지 수에 따라 출력을 내림차순으로 정렬하십시오.

얻은 총 챌린지 수가 기준인데, 챌린지 점수로 order을 해서 결과가 잘못나왔었다.

각각의 테이블을 조인한 후,
group by hacker_id, name으로 그룹핑을 해서 각 사람이 만점을 받은 수를 계산했다.

profile
MySQL DBA 신입

2개의 댓글

comment-user-thumbnail
2023년 7월 18일

글이 잘 정리되어 있네요. 감사합니다.

답글 달기
comment-user-thumbnail
2023년 7월 18일

덕분에 좋은 정보 얻어갑니다, 감사합니다.

답글 달기