Anusha Murali

Logo

Please see github.com/anusha-murali for all of my repositories.

View GitHub Profile

42. The PADS


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

The OCCUPATIONS table is described as follows:

42_1

Each row in the table denotes the lengths of each of a triangle’s three sides.

Sample Input

42_2

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 \leq 2 \leq 3 \leq 3)$, and then alphabetically by profession (doctor $\leq$ singer, and actor $\leq$ professor).

solution_image5

We could use the implicit concatanation operator, ||, to construct the query as follows:

SELECT NAME || '(' || SUBSTR(OCCUPATION, 1, 1) || ')' 
FROM OCCUPATIONS
ORDER BY NAME;

SELECT 'There are a total of ' || COUNT(*) || ' ' || LOWER(OCCUPATION) || 's.'
FROM OCCUPATIONS
GROUP BY OCCUPATION
ORDER BY COUNT(*), OCCUPATION;

Instead of using the implicit concatanation operator, we can also use the explicit concatanation function, CONCAT() as follows:

SELECT CONCAT(CONCAT(CONCAT(NAME, '('), SUBSTR(OCCUPATION, 1, 1)), ')')
FROM OCCUPATIONS
ORDER BY NAME;

SELECT 'There are a total of ', COUNT(OCCUPATION), CONCAT(LOWER(occupation),'s.')
FROM OCCUPATIONS
GROUP BY OCCUPATION
ORDER BY COUNT(OCCUPATION), OCCUPATION;

Back to problems


anusha-murali.github.io