What a Beautiful Data!

[HackerRank] Top Competitors 문제 풀이 mysql

by darami

[HackerRank] Top Competitors 문제 풀이 mysql

 

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.

줄리아는 막 코딩 테스트를 실행하는 것을 끝냈어, 그리고 그녀는 니가 리더보드를 조립해주는게 필요해.

각각의 해커 아이디와 누가 한 챌린지 이상 만점을 받았는지 이름을 출력하는 쿼리문을 작성해줘.

너의 출력은 만점을 받은 해커의 총 챌린지 숫자를 내림차순으로 정렬해줘. 만약 한명 이상의 해커가 같은 숫자의 챌린지에서 만점을 받았다면, 해커 아이디 오름차순으로 정렬해줘. 

 

조건 

1. hacker_id, name 출력 (근데 한 챌린지 이상 만점이상을 받아야함)

2. 한 챌린지 이상 만점이상 -> 챌린지 점수랑 제출 점수가 같다. 

3. ORDER BY 

 

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  space-separated values.

 

1. 처음 풀이 (에러) 

SELECT DISTINCT h.hacker_id,h.name
FROM Submissions AS s
JOIN Challenges AS c ON c.challenge_id = s.challenge_id
JOIN Difficulty AS d ON d.difficulty_level=c.difficulty_level
JOIN Hackers AS h ON h.hacker_id=s.hacker_id 
WHERE s.score=d.score;
ORDER BY c.challenge_id DESC

이유: more than 을 ~ 이상으로 해석해서 

 

Q: 왜 Error가 뜨지..? more than 은 이상이라서 하나부터고 greater than 부터 초과 아닌가..?

그런데  order by the total number of challenges in which the hacker earned a full score. 이게 걸리긴 했어...

 

more than  = greater than = 초과
more than or equal to = 이상

 

그렇구나.. more than 을 ~ 이상 이라고 하는 건 한국말로 해석상 그런 것 뿐이래! 

SELECT h.hacker_id,h.name
FROM Submissions AS s
JOIN Challenges AS c ON c.challenge_id = s.challenge_id
JOIN Difficulty AS d ON d.difficulty_level=c.difficulty_level
JOIN Hackers AS h ON h.hacker_id=s.hacker_id 
WHERE s.score=d.score
GROUP BY h.hacker_id,h.name
HAVING COUNT(s.challenge_id)>1
ORDER BY COUNT(s.challenge_id) DESC,hacker_id

그렇다면 이렇게 할 수 있다.

 

Q:  어 잠시만 Where 절에는 알리아스 못쓴다고 했던것 같은데, 테이블 AS 라서 가능한가

- SELECT절에 사용된 alias라면 WHERE 절에서 사용할 수 없지만, FROM절 서브쿼리를 통해 생성된 테이블의 컬럼 이기 때문에 WHERE절에서 사용이 가능합니다. 즉, 질문 주신 WHERE절의 dr은 alias가 아니며, FROM절 테이블의 컬럼 이기에 사용 가능한 것입니다. [인프런 질문 답변 참고]

 

아..그러니까 s랑 d는 알리아스가 아니라 테이블 컬럼 이라서 가능한 것! 

 

 

 

느낀점

1. 의문점 든거 해결하기

 

 2. 모 IT 회사의 과제를 풀었을 때 이렇게 매일 SQL 문제를 풀었다면 leaderboard 얘기가 나왔을 때 태블로 리더보드 만드는 법을 며칠간 찾아보지 않았을텐데.. 더 빨리 문제를 풀었을텐데.. 역시 핵심 역량!

 

3. 한 문제 풀이에 너무 지나친 시간을 쏟지는 말자 오히려 시간 낭비가 될 수 있다. 수학 문제 푸는 것처럼 끝까지    

답지 안보고 버티는 것도 어느 정도것 하자.  

 

4. 역시 이렇게 블로그에 정리하는게 풀이 과정도 더 빠르고 좋다! 

 

블로그의 정보

다람

darami

활동하기