🍰
LEARNING BY DOING
  • Home
  • Skills
    • SQL
      • Leetcode
        • 177. Nth Highest Salary
        • 175. Combine Two Tables
        • 178. Rank Scores
      • Hackerrank
        • Easy
          • The Blunder
          • Top Earners
          • Weather Observation Station 2
          • Weather Observation Station 13
          • Weather Observation Station 14
          • Weather Observation Station 15
          • Weather Observation Station 16
          • Weather Observation Station 17
          • Asian Population
          • Draw The Triangle 1
          • Draw The Triangle 2
        • Medium
          • Weather Observation Station 18
          • Weather Observation Station 19
          • Weather Observation Station 20
          • Ollivander's Inventory
          • Challenges
          • Contest Leaderboard
  • Company Research
    • Google
      • Google Search
      • Youtube
      • Google Ads
Powered by GitBook
On this page
  • Analysis
  • Query
  1. Skills
  2. SQL
  3. Hackerrank
  4. Medium

Challenges

PreviousOllivander's InventoryNextContest Leaderboard

Last updated 4 years ago

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.

Input Format

The following tables contain challenge data:

  • 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.

Analysis

  • hackerid, name, and the total number of challenges created by each student ==> SELECT h.hacker_id, h.name, count(c.hacker_id))

  • two tables ==> FROM Hackers as h JOIN Challenges as c WHERE c. hacker_id = h.hacker_id

  • Sort your results by the total number of challenges in descending order ==> ORDER BY count(c.challenges_id) DESC

  • If more than one student created the same number of challenges, then sort the result by hacker_id. ==> ORDER BY count(c.challenges_id) DESC, h.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.

    • ==> ORDER BY h.hacker_id

Query

SELECT c.hacker_id, h.name, COUNT(c.challenge_id) AS cnt 
FROM Hackers AS h JOIN Challenges AS c ON h.hacker_id = c.hacker_id
GROUP BY c.hacker_id, h.name HAVING
cnt = (SELECT COUNT(c1.challenge_id) FROM Challenges AS c1 GROUP BY c1.hacker_id ORDER BY COUNT(*) DESC LIMIT 1) OR
cnt NOT IN (SELECT COUNT(c2.challenge_id) FROM Challenges AS c2 GROUP BY c2.hacker_id HAVING c2.hacker_id <> c.hacker_id)
ORDER BY cnt DESC, c.hacker_id;