Posts

Showing posts from July, 2018

Placements: SQL Advance join (Hackerrank Problem)

Image
Problem: You are given three tables:  Students , Friends and Packages.   Students contains two columns: ID  and Name . Friends contains two columns: ID and Friend_ID ( ID of the ONLY best friend). Packages  contains two columns: ID and Salary (offered salary in $ thousands per month). Write a query to output the names of those students whose best friends got offered a higher salary than them. Names must be ordered by the salary amount offered to the best friends. It is guaranteed that no two students got same salary offer. Solution: select temp1.sn from (select S.ID si,S.Name sn,P.Salary ps from Students S join Packages P on S.ID=P.ID) temp1 join (select FF.ID fi,FF.Friend_ID fd,PP.Salary pps from Friends FF join Packages PP on FF.Friend_ID=pp.ID) temp2 on temp1.si=temp2.fi and temp1.ps<temp2.pps order by temp2.pps asc ; Explanation: As the problem states that we have to select the name of the friend whose salary is lesser than what his/her friend is ha

Contest Leaderboard: hackerrank problem (Level medium)

Problem Statement: You did such a great job helping Julia with her last coding contest challenge that she wants you to work on this one, too! The total score of a hacker is the sum of their maximum scores for all of the challenges. Write a query to print the hacker_id , name , and total score of the hackers ordered by the descending score. If more than one hacker achieved the same total score, then sort the result by ascending hacker_id . Exclude all hackers with a total score of from your result. Tables: Hackers: The hacker_id is the id of the hacker, and name is the name of the hacker.  Submissions: The submission_id is the id of the submission, hacker_id is the id of the hacker who made the submission, challenge_id is the id of the challenge for which the submission belongs to, and score is the score of the submission.   Solutions: select h.hacker_id,h.name,sum(sscore) from Hackers h inner join (select s.hacker_id,max(score) as sscore from Submissions s g