15 Days of Learning SQL in SQL | HackeRank Solution

Hello coders, today we are going to solve 15 Days of Learning SQL HackerRank Solution in SQL.

15 Days of Learning SQL


Julia conducted a 15 days of learning SQL contest. The start date of the contest was March 01, 2016 and the end date was March 15, 2016.

Write a query to print total number of unique hackers who made at least 1 submission each day (starting on the first day of the contest), and find the hacker_id and name of the hacker who made maximum number of submissions each day. If more than one such hacker has a maximum number of submissions, print the lowest hacker_id. The query should print this information for each day of the contest, sorted by the date.

Input Format

The following tables hold contest data:

  • Hackers: The hacker_id is the id of the hacker, and name is the name of the hacker.
  • Submissions: The submission_date is the date of the submission, submission_id is the id of the submission, hacker_id is the id of the hacker who made the submission, and score is the score of the submission. 

Sample Input

For the following sample input, assume that the end date of the contest was March 06, 2016.

Hackers Table:  Submissions Table: 

Sample Output

2016-03-01 4 20703 Angela
2016-03-02 2 79722 Michael
2016-03-03 2 20703 Angela
2016-03-04 2 20703 Angela
2016-03-05 1 36396 Frank
2016-03-06 1 20703 Angela

Solution – 15 Days of Learning SQL


SELECT t1.submission_date, hkr_cnt, t2.hacker_id, name
FROM (SELECT p1.submission_date, 
             COUNT(DISTINCT p1.hacker_id) AS hkr_cnt
      FROM (SELECT submission_date, hacker_id, 
                   @h_rnk := CASE WHEN @h_grp != hacker_id THEN 1 ELSE @h_rnk+1 END AS hacker_rank,
                   @h_grp := hacker_id AS hacker_group
            FROM (SELECT DISTINCT submission_date, hacker_id 
                  FROM submissions
                  ORDER BY hacker_id, submission_date) AS a, 
                 (SELECT @h_rnk := 1, @h_grp := 0) AS r) AS p1
      JOIN (SELECT submission_date, 
                   @d_rnk := @d_rnk + 1 AS date_rank
            FROM (SELECT DISTINCT submission_date
                  FROM submissions 
                  ORDER BY submission_date) AS b, 
                 (SELECT @d_rnk := 0) r) AS p2
      ON p1.submission_date = p2.submission_date 
         AND hacker_rank = date_rank
      GROUP BY p1.submission_Date) AS t1
JOIN (SELECT submission_date, hacker_id, sub_cnt,
             @s_rnk := CASE WHEN @d_grp != submission_date THEN 1 ELSE @s_rnk+1 END AS max_rnk,
             @d_grp := submission_date AS date_group
      FROM (SELECT submission_date, hacker_id, COUNT(*) AS sub_cnt
            FROM submissions AS s
            GROUP BY submission_date, hacker_id
            ORDER BY submission_date, sub_cnt DESC, hacker_id) AS c,
           (SELECT @s_rnk := 1, @d_grp := 0) AS r) AS t2                            
ON t1.submission_date = t2.submission_date AND max_rnk = 1
JOIN hackers AS h ON h.hacker_id = t2.hacker_id            
ORDER BY t1.submission_date

Disclaimer: The above Problem (15 Days of Learning SQL) is generated by Hacker Rank but the Solution is Provided by CodingBroz. This tutorial is only for Educational and Learning Purpose.

2 thoughts on “15 Days of Learning SQL in SQL | HackeRank Solution”

  1. Here is my solution in SQL Server using recursion:

    ;with cte1 as (
    Select s.hacker_id
    , submission_date
    from submissions s
    ), Recursion1 as (
    Select Hacker_id, submission_date
    from cte1
    where submission_date = ‘20160301’
    union all
    Select s.Hacker_id, s.Submission_date
    from cte1 c
    inner join submissions s on s.submission_date = dateadd(day,1,c.submission_date) and s.hacker_id = c.hacker_id
    where s.submission_date between ‘20160301’ and ‘20160315’
    ), Recursion2 as (
    Select Submission_date, Hacker_ID
    from Recursion1
    where Submission_date = ‘20160301’
    union all
    Select r1.Submission_date, r1.Hacker_ID
    from Recursion2 r
    inner join Recursion1 r1 on r1.submission_date = dateadd(day,1,r.submission_date) and r.hacker_id = r1.hacker_id
    ), MinHackerAndName as (
    Select dc.Submission_date, dc.[Hacker_SubmissionCount], dc.Hacker_id
    ,row_number() over (partition by dc.submission_date order by dc.Submission_date asc, dc.Hacker_SubmissionCount desc, dc.Hacker_id asc) [RN]
    from (
    Select s.Submission_date, s.hacker_id, count(s.Hacker_id)[Hacker_SubmissionCount]
    from Submissions s
    group by s.submission_date, s.hacker_id) dc
    Select r.Submission_date, Count(distinct r.Hacker_id) [HCount]
    , h.Hacker_id, h.Name
    from Recursion2 r
    inner join MinHackerAndName mhn on mhn.submission_date = r.submission_date and mhn.rn = 1
    inner join Hackers h on h.Hacker_id = mhn.hacker_id
    group by r.Submission_date , h.Hacker_id, h.Name
    Order by r.Submission_date asc

Leave a Comment

Your email address will not be published. Required fields are marked *