HackerRank: [SQL Advanced Select] (2/5) The PADS | concat, left, group by in SQL

HackerRank: [Advanced Select - 2/5] The PADS |  CONCAT, LEFT, GROUP BY in SQL
I started studying SQL from a very famous site - HackerRank. Here I will try to provide multiple approaches & solutions to the same problem. It will help you learn and understand SQL in a better way.

Please make use of my blog posts for learning purpose only and feel free to ask your questions in the comment box below in case of any doubt.

Click Here for the previous blog-post in the series.


SQL Problem Statement:

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:

OCCUPATIONS Columns

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

Sample Input:
An OCCUPATIONS table that contains the following records:
OCCUPATIONS Sample Data
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: Using CONCAT & LEFT function (MySQL Query):

SELECT CONCAT (NAME, '(', LEFT (OCCUPATION, 1), ')') as col1
FROM OCCUPATIONS
ORDER BY NAME;

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

NOTE: 
  1. CONCAT function is used to concatenate multiple strings.
  2. LEFT function is used to take the substring from left, '1' is passed parameter to select only 1 character from the left. 


Sample Output:

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


--------------------------------------------------------------------------------
Click here to see solutions for all Machine Learning Coursera Assignments.
&
Click here to see more codes for Raspberry Pi 3 and similar Family.
&
Click here to see more codes for NodeMCU ESP8266 and similar Family.
&
Click here to see more codes for Arduino Mega (ATMega 2560) and similar Family.
Feel free to ask doubts in the comment section. I will try my best to answer it.
If you find this helpful by any mean like, comment and share the post.
This is the simplest way to encourage me to keep doing such work.

Thanks & Regards,
-Akshay P Daga
إرسال تعليق (0)
أحدث أقدم