15 Days of Learning in SQL | HackerRank Programming Solutions | HackerRank SQL Solutions

Hello Programmers/Coders, Today we are going to share solutions of Programming problems of HackerRank of Programming Language SQL. At Each Problem with Successful submission with all Test Cases Passed, you will get an score or marks. And after solving maximum problems, you will be getting stars. This will highlight your profile to the recruiters.

In this post, you will find the solution for 15 Days of Learning in SQL-HackerRank Problem. We are providing the correct and tested solutions of coding problems present on HackerRank. If you are not able to solve any problem, then you can take help from our Blog/website.

Use “Ctrl+F” To Find Any Questions Answer. & For Mobile User, You Just Need To Click On Three dots In Your Browser & You Will Get A “Find” Option There. Use These Option to Get Any Random Questions Answer.

Introduction To SQL

SQL stands for Structured Query Language. SQL is used to create, remove, alter the database and database objects in a database management system and to store, retrieve, update the data in a database. SQL is a standard language for creating, accessing, manipulating database management system. SQL works for all modern relational database management systems, like SQL Server, Oracle, MySQL, etc.

  • It is a standard language for Relational Database System. It enables a user to create, read, update and delete relational databases and tables.
  • All the RDBMS like MySQL, Informix, Oracle, MS Access and SQL Server use SQL as their standard database language.
  • SQL allows users to query the database in a number of ways, using English-like statements.

Link for the Problem15 Days of Learning SQL – Hacker Rank Solution

15 Days of Learning SQL – Hacker Rank Solution

Problem:

Julia conducted a  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  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.1458511164 12adec3b8b ScreenShot2016 03 21at3.26.47AM
  • 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. 1458511251 0b534030b9 ScreenShot2016 03 21at3.26.56AM

Sample Input

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

Hackers Table: 1458511957 814a2c7bf2 ScreenShot2016 03 21at3.27.06AM Submissions Table: 1458512015 ff6a708164 ScreenShot2016 03 21at3.27.21AM

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
15 Days of Learning SQL – Hacker Rank Solution
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
;

Leave a Comment

Ads Blocker Image Powered by Code Help Pro
Ads Blocker Detected!!!

We have detected that you are using extensions to block ads. Please support us by disabling these ads blocker.

Refresh