Repository navigation
Expand file tree
/
Copy pathHackerrank-sql
More file actions
144 lines (119 loc) · 5.4 KB
/
Copy pathHackerrank-sql
File metadata and controls
144 lines (119 loc) · 5.4 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
995 New Companies:
select C.company_code,
C.founder,
count(distinct L.lead_manager_code),
count(distinct S.senior_manager_code),
count(distinct M.manager_code),
count(distinct E.employee_code)
from Company as C,
Lead_Manager as L,
Senior_Manager as S,
Manager as M,
Employee as E
WHERE E.manager_code = M.manager_code
AND M.senior_manager_code = S.senior_manager_code
AND L.lead_manager_code = S.lead_manager_code
AND C.company_code = L.company_code
group by C.company_code, C.founder
order by C.company_code
-----------------------------OR -----------------------------
select c.company_code , c.founder,
count(distinct l.lead_manager_code),
count(distinct s.senior_manager_code),
count(distinct m.manager_code),
count(distinct e.employee_code)
from company c , lead_manager l , senior_manager s ,manager m , employee e
where c.company_code = l.company_code
and l.lead_manager_code = s.lead_manager_code
and s.senior_manager_code = m.senior_manager_code
and m.manager_code = e.manager_code
group by c.company_code,c.founder
order by c.company_code
Weather Observation Station 20:
select round(s.lat_n, 4) from station as s where ( (select count(lat_n) from station where s.lat_n >= lat_n) - (select count(lat_n) % 2 from station) = (select count(lat_n) from station where s.lat_n < lat_n) )
SQL Project Planning :
SET sql_mode = '';
select start_date , end_date from
(select start_date from projects where start_date not in
(select end_date from projects)) a ,
(select end_date from projects where end_date not in
(select start_date from projects)) b
where start_date < end_date
group by start_date
order by datediff(end_date,start_date),start_date
Contest Leaderboard:
select h.hacker_id, name, sum(score) as total_score
from
hackers as h inner join
/* find max_score*/
(select hacker_id, max(score) as score from submissions group by challenge_id, hacker_id) max_score
on h.hacker_id=max_score.hacker_id
group by h.hacker_id, name
/* don't accept hackers with total_score=0 */
having total_score > 0
/* finally order as required */
order by total_score desc, h.hacker_id
;
create table challenges(challenge_id integer, hacker_id integer);
insert into challenges values(61654, 5077);
insert into challenges values(58302, 21283);
insert into challenges values(40587, 88255);
insert into challenges values(29477, 5077);
insert into challenges values(1220, 21283);
insert into challenges values(69514, 21283);
insert into challenges values(46561, 62743);
insert into challenges values(58077, 62743);
insert into challenges values(18483, 88255);
insert into challenges values(76766, 21283);
insert into challenges values(52382, 5077);
insert into challenges values(74467, 21283);
insert into challenges values(33625, 96196);
insert into challenges values(26053, 88255);
insert into challenges values(42665, 62743);
insert into challenges values(12859, 62743);
insert into challenges values(70094, 21283);
insert into challenges values(34599, 88255);
insert into challenges values(54680, 88255);
insert into challenges values(61881, 5077);
create table hackers(hacker_id integer, name varchar(50));
INSERT INTO `hackers` (`hacker_id`,`name`) VALUES (5077,'Rose');
INSERT INTO `hackers` (`hacker_id`,`name`) VALUES (21283,'Angela');
INSERT INTO `hackers` (`hacker_id`,`name`) VALUES (62743,'Frank');
INSERT INTO `hackers` (`hacker_id`,`name`) VALUES (88255,'Patrick');
INSERT INTO `hackers` (`hacker_id`,`name`) VALUES (96196,'Lisa');
/* count total submissions of challenges of each user */
WITH data AS
(
SELECT c.hacker_id as id, h.name as name, count(c.hacker_id) as counter
FROM Hackers h
JOIN Challenges c on c.hacker_id = h.hacker_id
GROUP BY c.hacker_id, h.name
)
/* select records from above */
SELECT id,name,counter
FROM data
WHERE
counter=(SELECT max(counter) FROM data) /*select user that has max count submission*/
OR
counter in (SELECT counter FROM data
GROUP BY counter
HAVING count(counter)=1 ) /*filter out the submission count which is unique*/
ORDER BY counter desc, id
SELECT C.HACKER_ID, H.NAME, COUNT(C.HACKER_ID) AS CREATED
FROM HACKERS AS H
JOIN CHALLENGES AS C
ON H.HACKER_ID = C.HACKER_ID
/* This one line created the difference */
GROUP BY C.HACKER_ID, H.NAME
/* As soon as I included H.NAME in the group by statement, everything started running properly */
HAVING CREATED = (SELECT COUNT(C1.CHALLENGE_ID) AS CREATED1
FROM CHALLENGES AS C1
GROUP BY C1.HACKER_ID
ORDER BY CREATED1 DESC
LIMIT 1)
OR
CREATED NOT IN (SELECT COUNT(C2.CHALLENGE_ID)
FROM CHALLENGES AS C2
GROUP BY C2.HACKER_ID
HAVING C.HACKER_ID != C2.HACKER_ID)
ORDER BY CREATED DESC, C.HACKER_ID