Recent Posts
Recent Comments
Link
«   2024/12   »
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31
Archives
Today
Total
관리 메뉴

Studying data

[HackerRank] Basic Join > Top Competitors 본문

SQL

[HackerRank] Basic Join > Top Competitors

halloweenie 2023. 1. 30. 21:43

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.


Input Format

 

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.

Sample Input

 

Hackers Table: 

 Difficulty Table: 

 Challenges Table: 

 Submissions Table: 

 

Sample Output

90411 Joe

 

Explanation

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.

 


[MySQL Solution 1]

by me

SELECT H.hacker_id, H.name
FROM Hackers H, (SELECT S.hacker_id, COUNT(*) AS CNT   -- GROUP BY 절에서 사용한 컬럼과 집계함수
                FROM Submissions S
                    INNER JOIN Challenges C ON S.challenge_id = C.challenge_id
                    INNER JOIN Difficulty D ON C.difficulty_level = D.difficulty_level
                WHERE D.score = S.score 
                GROUP BY S.hacker_id
                HAVING COUNT(*) > 1
                ) IDs
WHERE H.hacker_id = IDs.hacker_id 
ORDER BY IDs.CNT DESC, H.hacker_id ASC;

 

[MySQL Solution 2]

from HackerRank

SELECT h.hacker_id, h.name   -- GROUP BY 절에서 사용한 컬럼들
FROM submissions s
	INNER JOIN challenges c
		ON s.challenge_id = c.challenge_id	
 	INNER JOIN difficulty d
		ON c.difficulty_level = d.difficulty_level 
	INNER JOIN hackers h
		ON s.hacker_id = h.hacker_id
WHERE s.score = d.score 
GROUP BY h.hacker_id, h.name
HAVING COUNT(s.hacker_id) > 1
ORDER BY COUNT(s.hacker_id) DESC, s.hacker_id ASC;

 

[MySQL Solution 3]

from HackerRank

SELECT h.hacker_id, h.name   -- GROUP BY 절에서 사용한 컬럼들
    FROM submissions s
    JOIN challenges c
        ON s.challenge_id = c.challenge_id
    JOIN difficulty d
        ON c.difficulty_level = d.difficulty_level 
    JOIN hackers h
        ON s.hacker_id = h.hacker_id
    WHERE s.score = d.score 
    GROUP BY h.hacker_id, h.name
        HAVING COUNT(s.hacker_id) > 1
    ORDER BY COUNT(s.hacker_id) DESC, s.hacker_id ASC;

 

 

***GROUP BY절로 컬럼을 그룹으로 묶은 후 SELECT절에는 반드시 GROUP BY절에서 사용한 컬럼이 와야 한다.

즉, GROUP BY절에 있는 컬럼들 중에서만이 SELECT절에 올 수 있다. (집계함수 사용시에는 상관없음)

 

 

문제 링크:

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

Comments