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)
, andASingerName(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 the concat() and count() functions and the query is devided into the two parts. part 1 is for generating the first result set and 2nd part is to generate 2nd result set..
select case when Occupation='Doctor' then concat(concat(concat(Name,'('),'D'),')') when Occupation='Actor' then concat(concat(concat(Name,'('),'A'),')') when Occupation='Professor' then concat(concat(concat(Name,'('),'P'),')') when Occupation='Singer' then concat(concat(concat(Name,'('),'S'),')')end as name
from OCCUPATIONS
order by name asc;
select concat(concat(concat(concat('There are a total of ',count(Occupation)),' '),lower(Occupation)),'s.')
from OCCUPATIONS
group by Occupation
order by Count(Occupation) asc,lower(Occupation);
_______________________________The End__________________________________
Hoping this would be helpful to you, if not, post your problem in the comment section and i will try to get back to you as soon as possible.
tell me, how do you like this solution? and what is there that i can improve...
follow me on hackerrank: www.hackerrank.com/satyansh_sagar
from OCCUPATIONS
order by name asc;
select concat(concat(concat(concat('There are a total of ',count(Occupation)),' '),lower(Occupation)),'s.')
from OCCUPATIONS
group by Occupation
order by Count(Occupation) asc,lower(Occupation);
_______________________________The End__________________________________
Hoping this would be helpful to you, if not, post your problem in the comment section and i will try to get back to you as soon as possible.
tell me, how do you like this solution? and what is there that i can improve...
follow me on hackerrank: www.hackerrank.com/satyansh_sagar
Comments