The Task
Julia just finished conducting a coding contest, and she needs your help assembling the leaderboard! Write a query to print the respective hacker_id and name of hackers who achieved full scores for more than one challenge. Order your output in descending order by the total number of challenges the hacker has earned a full score. If more than one hacker received full scores in same number of challenges, then sort them by ascending hacker_id.
Input Format
The following tables contain contest data:
Hackers: The hacker_id is the id of the hacker, and name is the hacker’s name.
Column | Type |
hacker_id | Integer |
name | Integer |
Difficulty: The difficult_level is the difficulty level of the challenge, and the score is the challenge’s score for that difficulty level.
Column | Type |
difficulty_level | Integer |
score | Integer |
Challenges: The challenge_id is the challenge’s ID, the hacker_id is the ID of the hacker who created the challenge, and the difficulty_level is the challenge’s difficulty level.
Column | Type |
challenge_id | Integer |
hacker_id | Integer |
difficulty_level | Integer |
Submissions: The submission_id is the id of the submission, hacker_id is the id of the hacker who submitted, challenge_id is the id of the challenge that the submission belongs to, and score is the submission’s score.
Column | Type |
submission_id | Integer |
hacker_id | Integer |
Challenge_id | Integer |
score | Integer |
Sample Input
Hackers Table:
hacker_id | name |
---|---|
5580 | Rose |
8439 | Angela |
27205 | Frank |
52243 | Patrick |
52348 | Lisa |
57645 | Kimberly |
77726 | Bonnie |
83082 | Michael |
86870 | Todd |
90411 | Joe |
Difficulty Table:
difficulty_level | score |
---|---|
1 | 20 |
2 | 30 |
3 | 40 |
4 | 60 |
5 | 80 |
6 | 100 |
7 | 120 |
Challenges Table:
challenge_id | hacker_id | difficulty_level |
---|---|---|
4810 | 77726 | 4 |
21089 | 27205 | 1 |
36566 | 5580 | 7 |
66730 | 52243 | 6 |
71055 | 52243 | 2 |
Submissions Table:
submission_id | hacker_id | challenge_id | score |
---|---|---|---|
68628 | 77726 | 36566 | 30 |
65300 | 77726 | 21089 | 10 |
40326 | 52243 | 36566 | 77 |
8941 | 27205 | 4810 | 4 |
83554 | 77726 | 66730 | 30 |
43353 | 52243 | 66730 | 0 |
55385 | 52348 | 71055 | 20 |
39784 | 27205 | 71055 | 23 |
94613 | 86870 | 71055 | 3030 |
45788 | 52348 | 36566 | 0 |
93058 | 86870 | 36566 | 30 |
7344 | 8439 | 66730 | 92 |
2721 | 8439 | 4810 | 36 |
523 | 5580 | 71055 | 4 |
49105 | 52348 | 66730 | 0 |
55877 | 57645 | 66730 | 80 |
38355 | 27205 | 66730 | 35 |
3924 | 8439 | 36566 | 80 |
97397 | 90411 | 66730 | 100 |
84162 | 83082 | 4810 | 40 |
97431 | 90411 | 71055 | 30 |
Sample Output
90411 Joe
Explanation
Hacker 86870 got a score of 30 for challenge 71055 with a difficulty level of 2, so 86870 earned a total score for this challenge.
Hacker 90411 scored 30 for challenge 71055, which had a difficulty level of 2, so 90411 earned a full score for this challenge.
Hacker 90411 scored 100 for challenge 66730 with a difficulty level of 6, so 90411 earned a full score for this challenge.
Only hacker 90411 earned a full score for more than one challenge, so we print their hacker_id and name as space-separated values.
Let’s solve this problem in MySQL
To solve this problem, you need to create a SQL query that identifies hackers who have achieved full scores on more than one challenge. The full score for a challenge is defined by the maximum score for that challenge’s difficulty level, as specified in the Difficulty
table.
Here’s the general strategy for the SQL query:
- Join the
Submissions
table with theChallenges
andDifficulty
tables to associate each submission with the maximum possible score for its challenge. - Determine which submissions achieved the full score.
- Count the number of challenges where each hacker achieved full scores.
- Filter out hackers who achieved full scores in more than one challenge.
- Order the results by the number of full scores in descending order, and by
hacker_id
in ascending order when there is a tie.
Here’s a sample SQL query implementing the above logic:
SELECT h.hacker_id, h.name
FROM Hackers h
JOIN (
SELECT s.hacker_id, COUNT(DISTINCT s.challenge_id) AS full_score_count
FROM Submissions s
JOIN Challenges c ON s.challenge_id = c.challenge_id
JOIN Difficulty d ON c.difficulty_level = d.difficulty_level
WHERE s.score = d.score
GROUP BY s.hacker_id
HAVING COUNT(DISTINCT s.challenge_id) > 1
) AS fs ON h.hacker_id = fs.hacker_id
ORDER BY fs.full_score_count DESC, h.hacker_id;
Explanation of the query:
- The innermost
JOIN
combinesSubmissions
,Challenges
, andDifficulty
to get each submission with its associated maximum score (d.score
). - The
WHERE
clause filters submissions to only those that match the full score for their difficulty level. GROUP BY s.hacker_id
groups results by hacker.- The
HAVING COUNT(DISTINCT s.challenge_id) > 1
filters out hackers who did not achieve full scores in more than one challenge. - The outer query selects hacker’s details from the
Hackers
table and orders the results as required.
Ensure that your database schema correctly reflects the relationships and data types assumed in this query for it to work effectively. Adjust field and table names as necessary to match your actual database schema.