Top Competitors

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.

table 1: Hackers

The hacker_id is the id of the hacker, and name is the name of the hacker.


table 2: Difficulty

The difficult_level is the level of difficulty of the challenge, and score is the score of the challenge for the difficulty


table 3: 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.


table 4: 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.


Solution:

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

Output:


SQL Script:

USE [HackerRank]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Challenges](
       [challenge_id]
[int] NOT NULL,
       [hacker_id]
[int] NULL,
       [difficulty_level]
[int] NULL,
 CONSTRAINT [PK_Challenges] PRIMARY KEY CLUSTERED
(
       [challenge_id]
ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Difficulty](
       [difficulty_level]
[int] NOT NULL,
       [score]
[int] NULL
) ON [PRIMARY]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Hackers](
       [hacker_id]
[int] NOT NULL,
       [name]
[nvarchar](150) NULL,
 CONSTRAINT [PK_Hackers] PRIMARY KEY CLUSTERED
(
       [hacker_id]
ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Submissions](
       [submission_id]
[int] NOT NULL,
       [hacker_id]
[int] NULL,
       [challenge_id]
[int] NULL,
       [score]
[int] NULL,
 CONSTRAINT [PK_Submissions] PRIMARY KEY CLUSTERED
(
       [submission_id]
ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
GO
INSERT [dbo].[Challenges] ([challenge_id], [hacker_id], [difficulty_level]) VALUES (4810, 77726, 4)
GO
INSERT [dbo].[Challenges] ([challenge_id], [hacker_id], [difficulty_level]) VALUES (21089, 27205, 1)
GO
INSERT [dbo].[Challenges] ([challenge_id], [hacker_id], [difficulty_level]) VALUES (36566, 5580, 7)
GO
INSERT [dbo].[Challenges] ([challenge_id], [hacker_id], [difficulty_level]) VALUES (66730, 52243, 6)
GO
INSERT [dbo].[Challenges] ([challenge_id], [hacker_id], [difficulty_level]) VALUES (71055, 52243, 2)
GO
INSERT [dbo].[Difficulty] ([difficulty_level], [score]) VALUES (1, 20)
GO
INSERT [dbo].[Difficulty] ([difficulty_level], [score]) VALUES (2, 30)
GO
INSERT [dbo].[Difficulty] ([difficulty_level], [score]) VALUES (3, 40)
GO
INSERT [dbo].[Difficulty] ([difficulty_level], [score]) VALUES (4, 60)
GO
INSERT [dbo].[Difficulty] ([difficulty_level], [score]) VALUES (5, 80)
GO
INSERT [dbo].[Difficulty] ([difficulty_level], [score]) VALUES (6, 100)
GO
INSERT [dbo].[Difficulty] ([difficulty_level], [score]) VALUES (7, 120)
GO
INSERT [dbo].[Hackers] ([hacker_id], [name]) VALUES (5580, N'Rose')
GO
INSERT [dbo].[Hackers] ([hacker_id], [name]) VALUES (8439, N'Angela')
GO
INSERT [dbo].[Hackers] ([hacker_id], [name]) VALUES (27205, N'Frank')
GO
INSERT [dbo].[Hackers] ([hacker_id], [name]) VALUES (52243, N'Patrick')
GO
INSERT [dbo].[Hackers] ([hacker_id], [name]) VALUES (52384, N'Lisa')
GO
INSERT [dbo].[Hackers] ([hacker_id], [name]) VALUES (57645, N'Kimberly')
GO
INSERT [dbo].[Hackers] ([hacker_id], [name]) VALUES (77726, N'Bonnie')
GO
INSERT [dbo].[Hackers] ([hacker_id], [name]) VALUES (83082, N'Michael')
GO
INSERT [dbo].[Hackers] ([hacker_id], [name]) VALUES (86870, N'Todd')
GO
INSERT [dbo].[Hackers] ([hacker_id], [name]) VALUES (90411, N'Joe')
GO
INSERT [dbo].[Submissions] ([submission_id], [hacker_id], [challenge_id], [score]) VALUES (523, 5580, 71055, 4)
GO
INSERT [dbo].[Submissions] ([submission_id], [hacker_id], [challenge_id], [score]) VALUES (2721, 8439, 4810, 36)
GO
INSERT [dbo].[Submissions] ([submission_id], [hacker_id], [challenge_id], [score]) VALUES (3924, 8439, 36566, 80)
GO
INSERT [dbo].[Submissions] ([submission_id], [hacker_id], [challenge_id], [score]) VALUES (7344, 8439, 66730, 92)
GO
INSERT [dbo].[Submissions] ([submission_id], [hacker_id], [challenge_id], [score]) VALUES (8941, 27205, 4810, 4)
GO
INSERT [dbo].[Submissions] ([submission_id], [hacker_id], [challenge_id], [score]) VALUES (38355, 27205, 66730, 35)
GO
INSERT [dbo].[Submissions] ([submission_id], [hacker_id], [challenge_id], [score]) VALUES (39784, 27205, 71055, 23)
GO
INSERT [dbo].[Submissions] ([submission_id], [hacker_id], [challenge_id], [score]) VALUES (40326, 52243, 36566, 77)
GO
INSERT [dbo].[Submissions] ([submission_id], [hacker_id], [challenge_id], [score]) VALUES (43353, 52243, 66730, 0)
GO
INSERT [dbo].[Submissions] ([submission_id], [hacker_id], [challenge_id], [score]) VALUES (45788, 52384, 36566, 0)
GO
INSERT [dbo].[Submissions] ([submission_id], [hacker_id], [challenge_id], [score]) VALUES (49105, 52384, 66730, 0)
GO
INSERT [dbo].[Submissions] ([submission_id], [hacker_id], [challenge_id], [score]) VALUES (55385, 52348, 71055, 20)
GO
INSERT [dbo].[Submissions] ([submission_id], [hacker_id], [challenge_id], [score]) VALUES (55877, 57645, 66730, 80)
GO
INSERT [dbo].[Submissions] ([submission_id], [hacker_id], [challenge_id], [score]) VALUES (65300, 77726, 21089, 10)
GO
INSERT [dbo].[Submissions] ([submission_id], [hacker_id], [challenge_id], [score]) VALUES (68626, 77726, 36566, 30)
GO
INSERT [dbo].[Submissions] ([submission_id], [hacker_id], [challenge_id], [score]) VALUES (83554, 77726, 66730, 30)
GO
INSERT [dbo].[Submissions] ([submission_id], [hacker_id], [challenge_id], [score]) VALUES (84162, 83082, 4810, 40)
GO
INSERT [dbo].[Submissions] ([submission_id], [hacker_id], [challenge_id], [score]) VALUES (93058, 86870, 36566, 30)
GO
INSERT [dbo].[Submissions] ([submission_id], [hacker_id], [challenge_id], [score]) VALUES (94613, 86870, 71055, 30)
GO
INSERT [dbo].[Submissions] ([submission_id], [hacker_id], [challenge_id], [score]) VALUES (97397, 90411, 66730, 100)
GO
INSERT [dbo].[Submissions] ([submission_id], [hacker_id], [challenge_id], [score]) VALUES (97431, 90411, 71055, 30)
GO

 


Comments (0)