|
| 1 | +#create table Contests ( contest_id INT, hacker_id INT, name VARCHAR(200) ); |
| 2 | + |
| 3 | +#insert into Contests (contest_id, hacker_id, name) values (66406, 17973, 'Rose'), (66556, 79153, 'Angela'), (94828, 80275, 'Frank'); |
| 4 | + |
| 5 | +#create table Colleges( college_id INT, contest_id INT ); |
| 6 | + |
| 7 | +#insert into Colleges (college_id, contest_id) values (11219, 66406), (32473, 66556), (56685, 94828); |
| 8 | + |
| 9 | +#create table Challenges ( challenge_id INT, college_id INT ); |
| 10 | + |
| 11 | +#insert into Challenges (challenge_id, college_id) values (18765, 11219), (47127, 11219), (60292, 32473), (72974, 56685); |
| 12 | + |
| 13 | +#create table View_Stats ( challenge_id INT, total_views INT, total_unique_views INT ); |
| 14 | + |
| 15 | +#insert into View_Stats (challenge_id, total_views, total_unique_views) values (47127, 26, 19), (47127, 15, 14), (18765, 43, 10), (18765, 72, 13), (75516, 35, 17), (60292, 11, 10), (72974, 41, 15), (75516, 75, 11); |
| 16 | + |
| 17 | +#create table Submission_Stats ( challenge_id INT, total_submissions INT, total_accepted_submissions INT ); |
| 18 | + |
| 19 | +#insert into Submission_Stats (challenge_id, total_submissions, total_accepted_submissions) values (75516, 34, 12), (47127, 27, 10), (47127, 56, 18), (75516, 74, 12), (75516, 83, 8), (72974, 68, 24), (72974, 82, 14), (47127, 28, 11); |
| 20 | + |
| 21 | +/* Question* |
| 22 | +John interviews many candidates from different colleges using coding challenges and contests. |
| 23 | +Write a query which displays the contest_id, hacker_id, name, sums of total_submissions, |
| 24 | +total_accepted_submissions, total_views, and total_unique_views for each contest sorted by contest_id. |
| 25 | +Exclude the contest from the result if all four sums are zero . |
| 26 | +*/ |
| 27 | +select * from Contests; |
| 28 | +select * from Colleges; |
| 29 | +select * from Challenges; |
| 30 | +select * from View_Stats; |
| 31 | +select * from Submission_Stats; |
| 32 | + |
| 33 | +select con.contest_id, |
| 34 | + con.hacker_id, |
| 35 | + con.name, |
| 36 | + sum(total_submissions), #stats table |
| 37 | + sum(total_accepted_submissions), #stats table |
| 38 | + sum(total_views), #from view_stats |
| 39 | + sum(total_unique_views) |
| 40 | +from contests con |
| 41 | +join colleges col on con.contest_id = col.contest_id |
| 42 | +join challenges cha on col.college_id = cha.college_id |
| 43 | +left join |
| 44 | +(select challenge_id, sum(total_views) as total_views, sum(total_unique_views) as total_unique_views |
| 45 | + from view_stats group by challenge_id) vs on cha.challenge_id = vs.challenge_id |
| 46 | +left join |
| 47 | +(select challenge_id, sum(total_submissions) as total_submissions, sum(total_accepted_submissions) as total_accepted_submissions |
| 48 | + from submission_stats group by challenge_id) ss on cha.challenge_id = ss.challenge_id |
| 49 | + group by con.contest_id, con.hacker_id, con.name; |
| 50 | +/* |
| 51 | + having sum(total_submissions)!=0 or |
| 52 | + sum(total_accepted_submissions)!=0 or |
| 53 | + sum(total_views)!=0 or |
| 54 | + sum(total_unique_views)!=0 |
| 55 | + order by contest_id; |
| 56 | +*/ |
0 commit comments