[HackerRank] The PADS

당당·2023년 7월 13일
0

HackerRank

목록 보기
4/27
post-thumbnail

https://www.hackerrank.com/challenges/the-pads/problem

📔문제

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.

The OCCUPATIONS table is described as follows:

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


📝예시

An OCCUPATIONS table that contains the following records:

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.

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


🧮분야

  • SELECT

📃SQL 코드

select name||'('||substr(occupation,1,1)||')'
from occupations
order by name;

select 'There are a total of '||count(*)||' '||lower(occupation)||'s.'
from occupations
group by occupation
order by count(*),occupation;

📰출력 결과


📂고찰

처음에는 union all을 사용해서 두 쿼리를 합쳐서 실행해야 하나 했는데, 그럴 필요가 없었다.

||결합연산자를 사용해서 처음에 occupations의 첫글자를 이름 뒤에 붙여주는 쿼리를 출력한다.

그 다음 group by 를 통해서 occupation 별로 묶어서 수를 세고 정렬을 했다.

profile
MySQL DBA 신입

0개의 댓글