HackerRank SQL – The PADS Solution with Explanation (MySQL, MS SQL Server)

To solve this HackerRank SQL question (The PADS), you should have a good understanding of concepts like concatenation, group by, and union.
If you know these concepts well, this question will be quite easy. So, let’s get started and solve this question.

Problem (The PADS)

Generate the following two result sets:

  1. 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).
  2. 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 occupation.

Input Format

The OCCUPATIONS table is described as follows:

ColumnType
NameString
OccupationString

Occupation will only contain one of the following values: Doctor, Professor, Singer or Actor.

Sample Input

An OCCUPATIONS table that contains the following records:

NameOccupation
SamanthaDoctor
JuliaActor
MariaActor
MeeraSinger
AshelyProfessor
KettyProfessor
ChristeenProfessor
JaneActor
JennyDoctor
Priya Singer

Sample Output

Ashely(P)
Christeen(P)
Jane(A)
Jenny(D)
Julia(A)
Ketty(P)
Maria(A)
Meera(S)
Priya(S)
Samantha(D)
There are a total of 2 doctors.
There are a total of 2 singers.
There are a total of 3 actors.
There are a total of 3 professors.

Explanation

The results of the first query are formatted to the problem description’s specifications.
The results of the second query are ascendingly ordered first by number of names corresponding to each profession (2<=2<=3<=3), and then alphabetically by profession (doctor<=Singer, and actor<=professor).

Solution (MySQL, MS SQL Server)

Here is the Query Output that we will provide as Solution.

SELECT CONCAT(name,"(", LEFT(occupation, 1), ")") AS x FROM occupations
UNION
SELECT CONCAT("There are a total of ",COUNT(occupation)," ", LOWER(occupation),"s.")
FROM occupations
GROUP BY occupation
order by x;

Expected Output

Aamina(D)
Ashley(P)
Belvet(P)
Britney(P)
Christeen(S)
Eve(A)
Jane(S)
Jennifer(A)
Jenny(S)
Julia(D)
Ketty(A)
Kristeen(S)
Maria(P)
Meera(P)
Naomi(P)
Priya(D)
Priyanka(P)
Samantha(A)
There are a total of 3 doctors.
There are a total of 4 actors.
There are a total of 4 singers.
There are a total of 7 professors.

Explanation

  • The query combines two sets of information from the “occupations” table.
  • The first part creates a list of names with their first letter of occupation in parentheses. (Used CONCAT)
  • The second part counts each occupation and forms a sentence. (Used CONCAT, GROUP BY, COUNT)
  • The “UNION” combines these results into one set.
  • The final result is ordered alphabetically by the concatenated names.

Look This

Conclusion

This Question helps us to learn SQL String concatenation function. I hope you will learned it very well. don’t forget to visit this website again for more such Questions.

Leave a Comment

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

Scroll to Top