Milky's note

[HackerRank](M) The PADS 본문

SQL/SQL 코딩 테스트-HackerRank

[HackerRank](M) The PADS

밀뿌 2022. 3. 18. 02:26

https://www.hackerrank.com/challenges/the-pads/problem?isFullScreen=true 

 

The PADS | HackerRank

Query the name and abbreviated occupation for each person in OCCUPATIONS.

www.hackerrank.com

 

[문제]

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:
    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.
  3. There are a total of [occupation_count] [occupation]s.

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.

[답]

-mysql

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을 사용해서 출력해주었다.

다음으론 직업의 수를 조회하기 위해 count을 사용했고, concat을 이용해서 문장에 출력해주었다.

출력 예시가 직업이 소문자로 되어있어서 lower를 해주었고,

정렬 순서가 직업의 갯수와 알파벳 순이어서 다음과 같이 정렬을 해주었다.

Comments