The PADS [HackerRank]

dasd412·2022년 3월 22일
0

SQL

목록 보기
1/9
post-thumbnail

Problem Link

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.

답안 (MySQL)

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() : 소문자로 바꾸는 함수.


참고 자료

https://www.w3schools.com/sql/func_mysql_substring.asp

profile
아키텍쳐 설계와 테스트 코드에 관심이 많음.

0개의 댓글