cs

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:

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

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


[My Answer]

SELECT 
CONCAT(name, "(", LEFT(occupation, 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), occupation;

뭔가 까다로웠다. 어려운 게 아니고 뭐... 저 문장 마지막에 s. 을 입력해야 한다던가 혹은 칼럼 값의 첫 번째 단어 가지고 오거나 이런 것들.. 

(어떤 회사에서 이렇게까지 데이터 추출을 요청하시나?..) 

'Code Problems > SQL_ HackerRank' 카테고리의 다른 글

[HackerRank] Binary Tree Nodes  (0) 2021.11.06
[HackerRank] Occupations  (0) 2021.10.23
[HackerRank] Type of Triangle  (0) 2021.10.03
[HackerRank] Employee Salaries  (0) 2021.09.25
[HackerRank] Employee Names  (0) 2021.07.30

+ Recent posts