-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcountChallenges.txt
More file actions
32 lines (25 loc) · 1.3 KB
/
countChallenges.txt
File metadata and controls
32 lines (25 loc) · 1.3 KB
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
32
/*
Enter your query here.
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: The hacker_id is the id of the hacker, and name is the name of the hacker.
Challenges: The challenge_id is the id of the challenge, and hacker_id is the id of the student who created the challenge.
*/
SELECT h.hacker_id, h.name, COUNT(c.hacker_id) AS c_count
FROM Hackers AS h
JOIN Challenges AS c
ON h.hacker_id = c.hacker_id
GROUP BY c.hacker_id, h.name
HAVING c_count =
(SELECT MAX(temp1.cnt) FROM
(SELECT COUNT(hacker_id) AS cnt
FROM Challenges AS c1
GROUP BY c1.hacker_id ASC
ORDER BY c1.hacker_id DESC) AS temp1)
OR c_count IN
(SELECT t.cnt
FROM (SELECT COUNT(*) AS cnt
FROM Challenges
GROUP BY hacker_id) AS t
GROUP BY t.cnt
HAVING COUNT(t.cnt) = 1)
ORDER BY c_count DESC, c.hacker_id;