Last active
March 19, 2023 20:49
-
-
Save khuyentran1401/ffe0490f47bf901fbe0a419fe05befae to your computer and use it in GitHub Desktop.
Hackerrank SQL Challenges
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
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. | |
*/ | |
with count_table as ( | |
select | |
h.hacker_id, | |
h.name, | |
count(*) challenge_count | |
from | |
challenges c | |
join hackers h on c.hacker_id = h.hacker_id | |
group by | |
h.hacker_id, | |
h.name | |
) | |
select | |
hacker_id, | |
name, | |
challenge_count | |
from | |
count_table | |
where | |
challenge_count in ( | |
select | |
challenge_count | |
from | |
count_table | |
group by | |
challenge_count | |
having | |
count(challenge_count) <= 1 | |
) | |
or challenge_count = ( | |
select | |
max(challenge_count) | |
from | |
count_table | |
) | |
order by | |
challenge_count desc, | |
hacker_id; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment