The Pads-Hackerrank
Generating Alphabetically Ordered List and Counting Occupations
In this blog post, we will demonstrate how to generate an alphabetically ordered list of names from the OCCUPATIONS
table, followed by the first letter of each profession in parentheses, and count the occurrences of each occupation. We will show the implementation in MySQL, PostgreSQL, SQL Server, and SQLite.
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 occupation.
Input Format
The OCCUPATIONS table is described as follows:Occupation will only contain one of the following values: Doctor, Professor, Singer or Actor.
Sample Input
An OCCUPATIONS table that contains the following records:
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 (), and then alphabetically by profession (, and ).
SQL Queries
1. MySQL
-- Query 1: Alphabetically ordered list of names followed by the first letter of profession
SELECT CONCAT(name, '(', UPPER(LEFT(occupation, 1)), ')') AS formatted_name
FROM OCCUPATIONS
ORDER BY name;
-- Query 2: Count of each occupation in ascending order
SELECT CONCAT('There are a total of ', COUNT(occupation), ' ', LOWER(occupation), 's.')
FROM OCCUPATIONS
GROUP BY occupation
ORDER BY COUNT(occupation), occupation;
2. PostgreSQL
-- Query 1: Alphabetically ordered list of names followed by the first letter of profession
SELECT CONCAT(name, '(', UPPER(LEFT(occupation, 1)), ')') AS formatted_name
FROM OCCUPATIONS
ORDER BY name;
-- Query 2: Count of each occupation in ascending order
SELECT CONCAT('There are a total of ', COUNT(occupation), ' ', LOWER(occupation), 's.')
FROM OCCUPATIONS
GROUP BY occupation
ORDER BY COUNT(occupation), occupation;
3. SQL Server
-- Query 1: Alphabetically ordered list of names followed by the first letter of profession
SELECT name + '(' + UPPER(LEFT(occupation, 1)) + ')' AS formatted_name
FROM OCCUPATIONS
ORDER BY name;
-- Query 2: Count of each occupation in ascending order
SELECT 'There are a total of ' + CAST(COUNT(occupation) AS VARCHAR) + ' ' + LOWER(occupation) + 's.'
FROM OCCUPATIONS
GROUP BY occupation
ORDER BY COUNT(occupation), occupation;
4. SQLite
-- Query 1: Alphabetically ordered list of names followed by the first letter of profession
SELECT name || '(' || UPPER(SUBSTR(occupation, 1, 1)) || ')' AS formatted_name
FROM OCCUPATIONS
ORDER BY name;
-- Query 2: Count of each occupation in ascending order
SELECT 'There are a total of ' || COUNT(occupation) || ' ' || LOWER(occupation) || 's.'
FROM OCCUPATIONS
GROUP BY occupation
ORDER BY COUNT(occupation), occupation;
Explanation of SQL Queries
Alphabetically Ordered List:
CONCAT/||: This function is used to concatenate strings.
UPPER: Converts the character to uppercase.
LEFT/SUBSTR: Extracts the first character of the string.
Count of Each Occupation:
COUNT: Counts the number of occurrences.
GROUP BY: Groups the results by the
occupation
column.ORDER BY: Orders the results first by the count of occupations and then alphabetically by occupation.
Conclusion
These SQL queries provide a straightforward way to solve the given problem, demonstrating the versatility of SQL across different databases. By using string manipulation functions and aggregate functions, we can format and sort the results as required.
Comments
Post a Comment