The PADS in SQL | HackerRank Solution

Hello coders, today we are going to solve The PADS HackerRank Solution in SQL.

The PADS in SQL

Contents

Problem

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

ColumnType
NameString
OccupationString

The OCCUPATIONS table is described as follows: 

Occupation will only contain one of the following values: DoctorProfessorSinger or Actor.

Sample Input

An OCCUPATIONS table that contains the following records:

NameOccupation
SamanthaDoctor
JuliaActor
MariaActor
MeeraSinger
AshleyProfessor
KettyProfessor
ChristeenProfessor
JaneActor
JennyDoctor
PriyaSinger

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 – The PADS in SQL

MySQL

SELECT concat(NAME,concat("(",concat(substr(OCCUPATION,1,1),")"))) FROM OCCUPATIONS ORDER BY NAME ASC;
SELECT "There are a total of ", count(OCCUPATION), concat(lower(occupation),"s.") FROM OCCUPATIONS GROUP BY OCCUPATION ORDER BY count(OCCUPATION), OCCUPATION ASC

Disclaimer: The above Problem (The PADS) is generated by Hacker Rank but the Solution is Provided by CodingBroz. This tutorial is only for Educational and Learning Purpose.

1 thought on “The PADS in SQL | HackerRank Solution”

  1. this works with MSSQL and it does not matter how many occupations

    with x as (
    select occupation, count(*) v from occupations group by occupation
    )

    ,oc as (
    select concat(name ,'(‘,substring(occupation,1,1),’)’) x
    from occupations)

    ,oc2 as
    (
    select *
    ,DENSE_RANK() OVER (ORDER BY x asc) AS R
    from oc
    )

    ,x2 as (
    select
    concat(‘There are a total of ‘, v, ‘ ‘ ,lower(occupation) , ‘s.’) v,
    (select max(r) from oc2) +
    DENSE_RANK() OVER (ORDER BY v,occupation asc) AS R
    from x)

    select x from
    (select x,r from oc2
    union all
    select v,r from x2) x
    order by r

Leave a Comment

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