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 |
Tags
- PYTHON
- github
- Git
- leetcode
- 선형 모형
- hackerrank
- conflict
- full request
- 깃헙협업
- early stopping
- 온라인협업
- RLIKE
- 편향-분산 교환
- elastic net
- L2정규화
- programmers
- sql
- L1정규화
- branch
- CSS
- mysql
- 버전충돌
- 코딩공부
- window function
- AWS
- 클라우드컴퓨팅
- merge
- HTML
- 교차 엔트로피
- coding
Archives
- Today
- Total
Im between cherry
Hackerrank | MySQL | Challenges 본문
Challenges
Julia asked her students to create some coding challenges. Write a query to print the hacker_id, name, and the total number of challenges created by each student. Sort your results by the total number of challenges in descending order. If more than one student created the same number of challenges, then sort the result by hacker_id. If more than one student created the same number of challenges and the count is less than the maximum number of challenges created, then exclude those students from the result.
-- 출력 hackers.hacker_id, hackers.name, challenges_created
-- sort challenges_created DESC, hacker_id
SELECT hackers.hacker_id
, hackers.name
, COUNT(*) challenges_created
FROM Challenges
INNER JOIN Hackers On Challenges.hacker_id = Hackers.hacker_id
GROUP BY hackers.hacker_id, hackers.name
-- MAX(challenges_created), example 50
HAVING challenges_created = (SELECT MAX(challenges_created)
FROM(
SELECT hacker_id
, COUNT(*)
FROM Challenges
GROUP BY hacker_id) sub)
-- If more than one student created the same number of challenges and the count is less than the maximum number of challenges created, then exclude those students from the result.
OR challenges_created IN (SELECT challenges_created
FROM (
SELECT hacker_id
, COUNT(*) AS challenges_created
FROM Challenges
GROUP BY hacker_id)sub)
GROUP BY challenges_created
HAVING COUNT(*) = 1)
ORDER BY challenges_created DESC, hacker_id
#WITH 써서 하기
WITH counter AS(
SELECT hackers.hacker_id
, hackers.name
, COUNT(*) AS challenges_created
FROM Challenges
INNER JOIN Hackers ON Challenges.hacker_id = Hackers.hacker_id
GROUP BY hackers.hacker_id, hackers.name
)
SELECT counter.hacker_id
, counter.name
, counter.challenges_created
FROM counter
WHERE challenges_created = (SELECT MAX(challenges_created) FROM counter)
OR challenges_created IN (SELECT challenges_created
FROM counter
GROUP BY challenges_created
HAVING COUNT(*) = 1)
ORDER BY counter.challenges_created DESC, counter.hacker_id
'데이터분석 > practice_query' 카테고리의 다른 글
Leet Code | MySQL | 181. Employees Earning More Than Their Managers (0) | 2020.08.30 |
---|---|
Hackerrank | MySQL | The Report (0) | 2020.08.30 |
Hackerrank | MySQL | Type of Triangle (0) | 2020.08.29 |
Hackerrank | MySQL | Symmetric Pairs (0) | 2020.08.29 |
Hackerrank | MySQL | Draw The Triangle 2 (0) | 2020.08.29 |
Comments