HackerRank SQL

Top Competitors

Problem:

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.

Sample Output:

90411 Joe

Logic:

The output only have hacker_id and name, but Submissions table has only hacker_id while Hackers table has only name. So, we have to write a query to select hacker_id and name by joining Submissions table with Hackers, Challenges and Difficulty tables.

Next, filter the full score users by using s.score = d.score.

Then select count(*) > 1, group by h.hacker_id, h.name, and then order by count desc and hacker_id asc

Solution:

select h.hacker_id, h.name from Submissions as s join Hackers as h 
on s.hacker_id = h.hacker_id
join Challenges as c on s.challenge_id = c.challenge_id
join Difficulty as d on c.Difficulty_level = d.Difficulty_level
where s.score = d.score
group by h.hacker_id, h.name
having count(*) > 1
order by count(*) desc, h.hacker_id;

Question Link

Reference

--

--

--

My homepage to record my thought processes for solving SQL and Algorithm questions

Recommended from Medium

Straightforward & Simple: Client-side Tracking

Why Java Is Perfectly Alive

cup of coffee with a heart pattern on top

The challenge of cross-compiling with go, cgo and xgo

Recovering Deleted Adobe Illustrator Files (*.ai, *.eps, *.ait, *.svg)

Authenticating users across multiple subdomains using single Oauth proxy

Sending emails programmatically with Gmail API and Python

How Much Does It Cost To Make An App in 2022?

How To: Fork a GitHub Repository & Submit a Pull Request

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Isabelle

Isabelle

In love with telling stories with data

More from Medium

Limit Rows with the WHERE clause — MySQL Beginner Series

5 Advance SQL functions you must know

02. One SQL A Day (17/06/22)

How to sort result set in custom order in SQL?