Posts

Showing posts with the label SQL

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 i...

TOP COMPETITORS: HACKERRANK

Problem Statement:  Julia just finished conducting a coding contest, and she needs your help assembling the leaderboard! Write a query to print the respective hacker_id and name of hackers who achieved full scores for more than one challenge. Order your output in descending order by the total number of challenges in which the hacker earned a full score. If more than one hacker received full scores in same number of challenges, then sort them by ascending hacker_id . Tables: 1. Hackers: The hacker_id is the id of the hacker, and name is the name of the hacker.   2. Difficulty: The difficult_level is the level of difficulty of the challenge, and score is the score of the challenge for the difficulty level. 3. Challenges: The challenge_id is the id of the challenge, the hacker_id is the id of the hacker who created the challenge, and difficulty_level is the level of difficulty of the challenge. 4. Submissions: The submission_id is the id of the su...

The Occupation: hackerrank problem

Problem Statement: Pivot the Occupation column in OCCUPATIONS so that each Name is sorted alphabetically and displayed underneath its corresponding Occupation . The output column headers should be Doctor , Professor , Singer , and Actor , respectively. Note: Print NULL when there are no more names corresponding to an occupation. Table:   'OCCUPATIONS' table contain the column 'Name' and 'Occupation' Solution: select doc,prof,singer,act from ( select row_number() over(partition by Occupation order by Name) ron,Name as n,Occupation as o from OCCUPATIONS ) pro pivot (max(n)  for o in ('Doctor' as doc,'Actor' as act,'Professor' as prof,'Singer' as singer))  order by ron asc; Explanation: in the above query statement i am using row_number() function with the 'partition by' clause to append one column that contains row_number for each row in the subquery. in the subquery while assigning row_number() we...

Hackerrank: The Pads

Problem Statement: Generate the following two result sets: Query an alphabetically ordered list of all names in OCCUPATIONS , immediately followed by the first letter of each profession as a parenthetical (i.e.: enclosed in parentheses). For example: AnActorName(A) , ADoctorName(D) , AProfessorName(P) , and ASingerName(S) . Query the number of ocurrences of each occupation in OCCUPATIONS . Sort the occurrences in ascending order , and output them in the following format: There are a total of [occupation_count] [occupation]s. where [occupation_count] is the number of occurrences of an occupation in OCCUPATIONS and [occupation] is the lowercase occupation name. If more than one Occupation has the same [occupation_count] , they should be ordered alphabetically. Note: There will be at least two entries in the table for each type of occupations. SOLUTION: here is what you have been looking for.. the query format is for the oracle SQL. The problem uses...