<\/span><\/h2>\n\n\n\nJulia asked her students to create some coding challenges. Write a query to print the hacker_id<\/em>, name<\/em>, 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<\/em>. 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.<\/p>\n\n\n\n<\/span>Input Format<\/strong><\/span><\/h2>\n\n\n\nThe following tables contain challenge data:<\/p>\n\n\n\n
- Hackers:<\/em> The hacker_id<\/em> is the id of the hacker, and name<\/em> is the name of the hacker. <\/li>
- Challenges:<\/em> The challenge_id<\/em> is the id of the challenge, and hacker_id<\/em> is the id of the student who created the challenge. <\/li><\/ul>\n\n\n\n
Sample Input 0<\/strong><\/p>\n\n\n\nHackers Table <\/p>\n\n\n\n
Challenges Table <\/p>\n\n\n\n
Sample Output 0<\/strong><\/p>\n\n\n\n21283 Angela 6\n88255 Patrick 5\n96196 Lisa 1<\/code><\/pre>\n\n\n\nSample Input 1<\/strong><\/p>\n\n\n\nHackers Table <\/p>\n\n\n\n
Challenges Table <\/p>\n\n\n\n
Sample Output 1<\/strong><\/p>\n\n\n\n12299 Rose 6\n34856 Angela 6\n79345 Frank 4\n80491 Patrick 3\n81041 Lisa 1<\/code><\/pre>\n\n\n\n<\/span>Explanation<\/strong><\/span><\/h2>\n\n\n\nFor Sample Case 0<\/em>, we can get the following details:
Students 5077<\/strong> and 62743<\/strong> both created 4<\/strong> challenges, but the maximum number of challenges created is 6<\/strong> so these students are excluded from the result.<\/p>\n\n\n\nFor Sample Case 1<\/em>, we can get the following details:
Students 12299<\/strong> and 34856<\/strong> both created 6<\/strong> challenges. Because 6<\/strong> is the maximum number of challenges created, these students are included in the result.<\/p>\n\n\n\n<\/span>Solution – Challenges in SQL <\/strong><\/span><\/h2>\n\n\n\n<\/span>MySQL<\/strong><\/span><\/h3>\n\n\n\nSELECT h.hacker_id, \n h.name, \n COUNT(c.challenge_id) AS c_count\nFROM Hackers h\nJOIN Challenges c ON c.hacker_id = h.hacker_id\nGROUP BY h.hacker_id, h.name\nHAVING c_count = \n (SELECT COUNT(c2.challenge_id) AS c_max\n FROM challenges as c2 \n GROUP BY c2.hacker_id \n ORDER BY c_max DESC limit 1)\nOR c_count IN \n (SELECT DISTINCT c_compare AS c_unique\n FROM (SELECT h2.hacker_id, \n h2.name, \n COUNT(challenge_id) AS c_compare\n FROM Hackers h2\n JOIN Challenges c ON c.hacker_id = h2.hacker_id\n GROUP BY h2.hacker_id, h2.name) counts\n GROUP BY c_compare\n HAVING COUNT(c_compare) = 1)\n\nORDER BY c_count DESC, h.hacker_id;\n<\/pre>\n\n\n\n