-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathHackerRank Contest Leaderboard.sql
More file actions
76 lines (59 loc) · 2.02 KB
/
HackerRank Contest Leaderboard.sql
File metadata and controls
76 lines (59 loc) · 2.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
set search_path to data_sci
-- add Challenge_id and score columns to submissions
alter TABLE submissions
add column challenge_id INTEGER,
add column score INTEGER
select * from submissions limit 20
select count(*) from submissions
-- 400
delete from submissions
where challenge_id is not NULL
update submissions SET
(challenge_id, score) =
(select 100+ num%10, trunc(random()*500)
from generate_series(1,400,1) as num
where sub_id = num)
select * from hackers
-- The total score of a hacker is the sum of their maximum scores for all of the challenges.
-- Write a query to print the hacker_id, name, and total score of the hackers ordered by the
-- descending score. If more than one hacker achieved the same total score, then sort the
-- result by ascending hacker_id. Exclude all hackers with a total score of 0 from your result.
-- For each hacker, find max score on each challenge
-- If they did not make any submissions for a challenge, score is 0
-- Don't care about submission date
-- After finding hacker_id, join to hackers to get name
-- Sort as specified, but exclude hackers with score of 0
-- For every combination of hackers and challenges, find max score
with cte_max as(
select hacker_id, challenge_id, max(score) as max_scores
from submissions
group by hacker_id, challenge_id
ORDER by hacker_id, challenge_id
)
select c.hacker_id, h.name, sum(max_scores) as total_score
from cte_max c
inner join hackers h on h.hacker_id = c.hacker_id
group by c.hacker_id, h.name
having sum(max_scores) > 0
order by 3 desc, 1 asc
-- 3 "Name3" 4162
-- 5 "Name5" 3959
-- 1 "Name1" 3899
select * from
submissions
where hacker_id=1 and challenge_id = 101
-- Make hacker_id 1 have same score as Hacker_id 5
update submissions
set score = 311
where sub_id = 151
-- Add a hacker with 0 score and null score to confirm they are excluded
insert into submissions
(hacker_id, submission_date, challenge_id,score)
values
(11,'2016-03-09',101,0),
(12,'2016-03-09',101,NULL)
insert into hackers
(hacker_id,name)
values
(11, 'Name11'),
(12, 'Name12')