We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
- Prepare
- SQL
- Advanced Join
- Interviews
- Discussions
Interviews
Interviews
Sort by
recency
|
1332 Discussions
|
Please Login in order to post a comment
This isn't really a SQL problem, its more of a data obersvation problem. If the problem is testing working with malformed data tables... just say that. Bad problem design, hackrank L
with challenge_data as (
),
views as (
)
/Main combining select/
select c.contest_id,
from contests c join challenge_data cd on cd.contest_id = c.contest_id join views v on v.contest_id = c.contest_id order by c.contest_id
/*contest_id, hacker_id, who made the contest name, name of hacker who made the contest sum of total submissions, for that contest total_accepted_submissions total_views, total_unique_views order by contest_id */
with** challenge_data** as ( select clg.contest_id, sum(total_submissions) ts, sum(total_accepted_submissions) tas from colleges clg join challenges c on clg.college_id = c.college_id join submission_stats st on st.challenge_id = c.challenge_id group by clg.contest_id ),
views as ( select clg.contest_id, sum(total_views) tv, sum(total_unique_views) tuv from view_stats vt join challenges c on c.challenge_id = vt.challenge_id join colleges clg on clg.college_id = c.college_id group by clg.contest_id )
/* Main combining select*/ select c.contest_id, c.hacker_id, c.name, cd.ts, cd.tas, v.tv, v.tuv from contests c join challenge_data cd on cd.contest_id = c.contest_id join views v on v.contest_id = c.contest_id order by c.contest_id
from contests c join challenge_data cd on cd.contest_id = c.contest_id join views v on v.contest_id = c.contest_id order by c.contest_id
To anyone who is wondering why simple inner join and performing sum() using group by does not work: One of the reason is, the set of challenge_ids in View_Stats does not equal to the set of challenge_ids in Submission_Stats. Therefore, using simple inner join to join stats data with the contest table will lead to missing records. It's not your fault.
MySQL SERVER