https://www.hackerrank.com/challenges/the-pads/problem?isFullScreen=true
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.
Input Format
The OCCUPATIONS table is described as follows:
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.
SELECT CONCAT(name, '(',SUBSTRING(occupation,1,1),')') FROM OCCUPATIONS ORDER BY name;
SELECT CONCAT('There are a total of ', COUNT(occupation) ,' ',LOWER(occupation),'s.')FROM OCCUPATIONS GROUP BY occupation ORDER BY COUNT(occupation);
CONCAT()
: 둘 이상의 문자열이나 컬럼 값을 하나의 문자열로 합치는 함수. 파라미터 개수는 2개를 넘어도 된다.
SUBSTRING(string_expression, start,length)
: 문자열에 대해 start 지점(인덱스)부터 length만큼 자른다. (start는 양수 또는 음수. 양수인 경우 1부터 시작해서 끝까지, 음수 인경우 뒤에서부터 자름)
LOWER()
: 소문자로 바꾸는 함수.