karasms.com

Mastering SQL: A Guide to the HackerRank Contest Leaderboard

Written on

Chapter 1: Introduction to the Contest Leaderboard Problem

If you aim to enhance your SQL expertise or prepare for your upcoming technical interviews, consistent practice is essential. Platforms like HackerRank provide numerous challenges to sharpen your skills. In this article, we will delve into the Contest Leaderboard challenge and how to effectively address it.

Problem Overview

Julia was impressed by your assistance in her previous coding contest, and she now seeks your help with a new challenge! The task is to calculate the total score for each hacker, which is derived from their highest scores across all challenges. Your objective is to write a query that returns the hacker_id, name, and total score, sorted by score in descending order. If multiple hackers have the same total score, arrange them by hacker_id in ascending order. Additionally, exclude hackers who have a total score of zero.

The sample input provided by HackerRank consists of two tables: hackers and submissions.

#### Hackers Table

hacker_id | name |
4071 | Rose |
4806 | Angela |
26071 | Frank |
49438 | Patrick |
74842 | Lisa |
80305 | Kimberly|
84072 | Bonnie |
87868 | Michael |
92118 | Todd |
95895 | Joe |

#### Submissions Table

submission_id | hacker_id | challenge_id | score |
67194 | 74842 | 63132 | 76 |
64479 | 74842 | 19797 | 98 |
40742 | 26071 | 49593 | 20 |
17513 | 4806 | 49593 | 32 |
69846 | 80305 | 19797 | 19 |
41002 | 26071 | 89343 | 36 |
52826 | 49438 | 49593 | 9 |
31093 | 26071 | 19797 | 2 |
81614 | 84072 | 49593 | 100 |
44829 | 26071 | 89343 | 17 |
75147 | 80305 | 49593 | 48 |
14115 | 4806 | 49593 | 76 |
6943 | 4071 | 19797 | 95 |
12855 | 4806 | 25917 | 13 |
73343 | 80305 | 49593 | 42 |
84264 | 84072 | 63132 | 0 |
9951 | 4071 | 49593 | 43 |
45104 | 49438 | 25917 | 34 |
53795 | 74842 | 19797 | 5 |
26363 | 26071 | 19797 | 29 |
10063 | 4071 | 49593 | 96 |

Solution Steps

To solve this problem, we need to create a query that retrieves the hacker ID, name, and total score for each hacker. If a hacker has multiple submissions for a single challenge, only their highest score should be accounted for. Moreover, hackers with a total score of zero will be omitted from the final results.

Our first step is to compute the maximum score achieved by each hacker for every challenge:

SELECT

h.hacker_id,

h.name,

s.challenge_id,

MAX(score) AS max_score

FROM

hackers h

INNER JOIN submissions s ON h.hacker_id = s.hacker_id

GROUP BY

h.hacker_id,

h.name,

s.challenge_id

Next, we refine our query to sum the maximum scores for each hacker while excluding those with zero scores:

SELECT

hacker_id,

name,

SUM(max_score) AS total_score

FROM (

SELECT

h.hacker_id,

h.name,

s.challenge_id,

MAX(score) AS max_score

FROM

hackers h

INNER JOIN submissions s ON h.hacker_id = s.hacker_id

GROUP BY

h.hacker_id,

h.name,

s.challenge_id

) AS x

GROUP BY

hacker_id,

name

HAVING SUM(max_score) <> 0

ORDER BY

total_score DESC,

hacker_id

Conclusion

In this brief tutorial, we have tackled the Contest Leaderboard SQL problem on HackerRank. I hope this guide was clear and helpful. Remember to keep practicing!

A detailed walkthrough of the Contest Leaderboard problem on HackerRank, illustrating step-by-step SQL solutions.

Explore the subquery version of the Contest Leaderboard problem, enhancing your SQL skills with practical examples.

Thank you for being part of our community! Before you leave, consider engaging with the content and following the author to stay updated on more insightful articles.

Share the page:

Twitter Facebook Reddit LinkIn

-----------------------

Recent Post:

Crafting Your Success: A Practical Guide to Self-Improvement

Discover a straightforward approach to achieving your goals through determination, mentorship, time management, and hard work.

Mastering Classification: A Comprehensive Guide to Techniques

Dive into advanced classification techniques in machine learning, exploring algorithms and evaluation metrics essential for success.

Understanding BPPV: A Comprehensive Guide to Dizziness Relief

Explore the fundamentals of BPPV, its symptoms, causes, and effective treatments to regain balance and manage dizziness.