Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- 프로그래머스
- Gaimification
- kaggle
- 파이썬
- oracle
- ProfileReport
- 데이터분석
- SQL
- ABC Analysis
- 코딩테스트연습
- eda
- Inventory Optimization
- Labor Management System
- 코딩테스트
- 피그마인디언
- 웨어하우스 보관 최적화
- SKU Consolidation
- 당신의 인생이 왜 힘들지 않아야 한다고 생각하십니까
- MS SQL Server
- HackerRank
- 신경쓰기의 기술
- TensorFlowGPU
- Product Demand
- 딥러닝
- leetcode
- ModelCheckPoint
- pandas profiling
- MySQL
- tensorflow
- forecast
Archives
- Today
- Total
오늘도 배운다
Top Competitors / HackerRank, SQL, 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.
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.
source: HackerRank
제출답안(MySQL)
-- query a.hacker_id, b.name with more than one full score achivement ODBY # of full score achivements DESC, name
-- table: hackers(h), difficulty(d), challenges(c), submissions(s)
SELECT hacker_id, name
FROM
(
SELECT
s.hacker_id, h.name, c.challenge_id
FROM (
submissions AS s
LEFT JOIN hackers AS h ON s.hacker_id = h.hacker_id
LEFT JOIN challenges AS c ON s.challenge_id = c.challenge_id
LEFT JOIN difficulty AS d ON c.difficulty_level = d.difficulty_level
)
WHERE s.score = d.score
)AS tb_join
GROUP BY hacker_id, name
HAVING COUNT(challenge_id)>1
ORDER BY COUNT(challenge_id) DESC, 1
풀이(MySQL)
더보기
1. 목표: 테이블 조인 후 hacker_id, name 출력
SELECT hacker_id, name
2-1.제출기록이 담긴 'submissions' 테이블 기준으로 다른 테이블을 조인
>> Challenge 테이블 내 hacker_id 는 challenge 를 생성한 hacker's id
>> 따라서, submissions 테이블 내 hacker_id를 기준으로 조인해야함
>> 조인해야하는 테이블이 많으므로 submissions 테이블을 기준으로 모두 LEFT JOIN
2-2. 조인 후 필요한 정보인 hacker_id, name, challenge_id를 불러오기
>> 조인 할 기준은 hacker_id >> name, challenge_id >> difficulty_level >> score (full score)
FROM
(
SELECT
s.hacker_id, h.name, c.challenge_id
FROM (
submissions AS s
LEFT JOIN hackers AS h ON s.hacker_id = h.hacker_id
LEFT JOIN challenges AS c ON s.challenge_id = c.challenge_id
LEFT JOIN difficulty AS d ON c.difficulty_level = d.difficulty_level
)
WHERE s.score = d.score
)AS tb_join
3. hacker_id, name 기준으로 그룹화하고 full score challenge가 1개 초과하는 데이터만 추출하여 challenge 수 기준 내림차순, hacker_id 기준 오름차순 정렬
GROUP BY hacker_id, name
HAVING COUNT(challenge_id)>1
ORDER BY COUNT(challenge_id) DESC, 1
문제 바로가기(MySQL)
728x90
'코딩테스트연습(SQL)' 카테고리의 다른 글
Placements / HackerRank, SQL, MySQL (0) | 2023.02.17 |
---|---|
Contest Leaderboard / HackerRank SQL MySQL (0) | 2023.01.10 |
New Companies / HackerRank, SQL, MySQL (0) | 2023.01.03 |
The Report / HackerRank, SQL, MySQL (0) | 2022.12.28 |
Average Population of Each Continent / HackerRank, SQL, MySQL (0) | 2022.12.27 |
Comments