상세 컨텐츠

본문 제목

[오늘의 코드 128] [HackerRank] The PADS

코드 공부

by eun_00 2024. 10. 23. 00:16

본문

 
 

[문제]

  • 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.
    Note: There will be at least two entries in the table for each type of occupation.

문제설명

1. 알파벳 순으로 OCCUPATIONS 테이블에서 이름 정렬,

각 이름 뒤에 해당 직업의 첫글자를 괄호 안에 포함

2. OCCUPATIONS 테이블에서 각 직업 빈도 수 추출, 빈도수 별 오름차순 정렬

[💡 정답]

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), Occupation;
 

 ✔️ 알게된 것 

- 쿼리 2가지에 대한 값이 모두 나와야한다.

1. 알파벳 순으로 OCCUPATIONS 테이블에서 이름 정렬,

각 이름 뒤에 해당 직업의 첫글자를 괄호 안에 포함

-> CONCAT()으로 괄호와 값 연결

SUBSTRING(값, 시작위치, 시작으로부터 몇 번째) 로 첫 번쨰 글자만 추출

2. OCCUPATIONS 테이블에서 각 직업 빈도 수 추출, 빈도수 별 오름차순 정렬

-> CONCAT() 마찬가지로 output 값과 연결

직업 별 그룹화하고, 빈도수 오름차순 정렬

관련글 더보기