[HackerRank] The PADS

생각하는 마리오네트·2021년 10월 27일
0

SQL

목록 보기
22/39

Q.문제

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.

Input Format

The OCCUPATIONS table is described as follows:

ColumnType
NameString
OccupationString

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.

시행착오 및 새롭게 배운것

SQL에서의 CONCAT사용법: 이번문제의 경우 전혀 어려운 문제가 아니다. 오히려 훨씬 난이도가 낮은 문제였다. 하지만 CONCAT의 활용을 몰라서 30분정도의 시간이 걸리게 되었고, CONCAT을 사용하게되면 쉽게 풀리게 된다.
SQL에서의 CONCAT은 함수안에 여러개의 문자열 혹은 식을 쉼표단위로 넣어주면된다.


정답.

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

너무 쉬운문제라서 따로 풀이는 남기지 않겠다. CONCAT만 활용하면 금방 풀 수 있다.

profile
문제를해결하는도구로서의"데이터"

0개의 댓글