[HackerRank][SQL(Oracle)] Top Competitors

728x90

 

※  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.

 

■  [Basic Join]  Top Competitors

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

Q.

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.

A.

SELECT		H.HACKER_ID, H.NAME
FROM		SUBMISSIONS S, CHALLENGES C, DIFFICULTY D, HACKERS H
WHERE		S.CHALLENGE_ID = C.CHALLENGE_ID AND
			C.DIFFICULTY_LEVEL = D.DIFFICULTY_LEVEL AND
			S.HACKER_ID = H.HACKER_ID AND
			S.SCORE = D.SCORE
GROUP BY	H.HACKER_ID, H.NAME
HAVING 		COUNT(C.CHALLENGE_ID) > 1
ORDER BY 	COUNT(C.CHALLENGE_ID) DESC, H.HACKER_ID;
SELECT		ID_HKR, NM_HKR
FROM		(
				SELECT		H.HACKER_ID ID_HKR, H.NAME NM_HKR, COUNT(C.CHALLENGE_ID) CNT_CHG
				FROM		SUBMISSIONS S, CHALLENGES C, DIFFICULTY D, HACKERS H
				WHERE		S.CHALLENGE_ID = C.CHALLENGE_ID AND
							C.DIFFICULTY_LEVEL = D.DIFFICULTY_LEVEL AND
							S.HACKER_ID = H.HACKER_ID AND
							S.SCORE = D.SCORE
				GROUP BY	H.HACKER_ID, H.NAME
			)
WHERE		CNT_CHG > 1
ORDER BY	CNT_CHG DESC, ID_HKR;
SELECT		H.HACKER_ID, H.NAME
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(C.CHALLENGE_ID) > 1
ORDER BY	COUNT(C.CHALLENGE_ID) DESC, H.HACKER_ID;
SELECT		ID_HKR, NM_HKR
FROM		(
				SELECT		H.HACKER_ID ID_HKR, H.NAME NM_HKR, COUNT(C.CHALLENGE_ID) CNT_CHG
				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
			)
WHERE		CNT_CHG > 1
ORDER BY	CNT_CHG DESC, ID_HKR;

 

 

반응형