I started studying SQL from a very famous site - HackerRank. Here I will try to provide multiple approaches & solutions to the same problem. It will help you learn and understand SQL in a better way. Please make use of my blog posts for learning purpose only and feel free to ask your questions in the comment box below in case of any doubt.
Click Here for the previous blog-post in the series.
SQL Problem Statement:
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 in which the hacker 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 name of the hacker.
- Difficulty: The difficult_level is the level of difficulty of the challenge, and score is the score of the challenge for the difficulty level.
- Challenges: The challenge_id is the id of the challenge, the hacker_id is the id of the hacker who created the challenge, and difficulty_level is the level of difficulty of the challenge.
- Submissions: The submission_id is the id of the submission, hacker_id is the id of the hacker who made the submission, challenge_id is the id of the challenge that the submission belongs to, and score is the score of the submission.
Sample Input:
Hackers Table:
Difficulty Table:
Challenges Table:
Submissions Table:
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 full score for this challenge.
Hacker 90411 got a score of 30 for challenge 71055 with a difficulty level of 2, so 90411 earned a full score for this challenge.
Hacker 90411 got a score of 100 for challenge 66730 with a difficulty level of 6, so 90411 earned a full score for this challenge.
Only hacker 90411 managed to earn a full score for more than one challenge, so we print the their hacker_id and name as space-separated values.
Solution: Using INNER JOIN & HAVING (MySQL Query):
SELECT
H.HACKER_ID,
H.NAME
FROM
HACKERS H
INNER JOIN SUBMISSIONS S ON H.HACKER_ID = S.HACKER_ID
INNER JOIN CHALLENGES C ON S.CHALLENGE_ID = C.CHALLENGE_ID
INNER JOIN DIFFICULTY D ON C.DIFFICULTY_LEVEL = D.DIFFICULTY_LEVEL
WHERE
D.SCORE = S.SCORE
AND D.DIFFICULTY_LEVEL = C.DIFFICULTY_LEVEL
GROUP BY
H.HACKER_ID,
H.NAME
HAVING
COUNT(S.SUBMISSION_ID) > 1
ORDER BY
COUNT(S.SUBMISSION_ID) DESC,
H.HACKER_ID ASC;
NOTE:
- The HAVING clause was added to SQL because the WHERE keyword cannot be used with aggregate functions.
- JOIN and INNER JOIN are the same in SQL. It returns the records that have matching values in both tables.
Expected Output:
27232 Phillip
28614 Willie
15719 Christina
43892 Roy
14246 David
14372 Michelle
18330 Lawrence
26133 Jacqueline
26253 John
30128 Brandon
35583 Norma
13944 Victor
17295 Elizabeth
--------------------------------------------------------------------------------
Click here to see solutions for all Machine Learning Coursera Assignments. Click here to see more codes for Raspberry Pi 3 and similar Family. Click here to see more codes for NodeMCU ESP8266 and similar Family. Click here to see more codes for Arduino Mega (ATMega 2560) and similar Family. Feel free to ask doubts in the comment section. I will try my best to answer it.
If you find this helpful by any mean like, comment and share the post.
This is the simplest way to encourage me to keep doing such work.