Occupations
The first column is an alphabetically ordered list of Doctor names.
The second column is an alphabetically ordered list of Professor names.
The third column is an alphabetically ordered list of Singer names.
The fourth column is an alphabetically ordered list of Actor names.
The empty cell data for columns with less than the maximum number of names per occupation (in this case, the Professor and Actor columns) are filled with NULL values.
* The output column headers should be Doctor, Professor, Singer, and Actor, respectively.
풀이 코드
- CASE WHEN 문을 통해 피벗테이블을 생성한다.
- 같은 열에 동일 직업을 가진 사람들의 이름을 넣어야 한다. ROW_NUMBER() OVER (PARTITION BY ~ ORDER BY)절을 사용해서 문제를 풀어야 한다. * 서브쿼리의 경우 Alias 명칭을 제대로 부여해야 오류가 나지 않는다.
-
The PADS.
- 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:
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. - There are a total of [occupation_count] [occupation]s.
*
occupations 테이블에 있는 name + occupation의 첫 글자를 함께 출력하고, occupation을 count하여 문장을 출력해주는 문제이다.
풀이 코드
- LEFT(occupation, 1) -> SUBSTRING(occupation, 1, 1) 대체 가능하다.
SUBSTRING(occupation, 1, 1) : occupation의 첫 번째에 위치한 글자부터 1글자만 자른다는 뜻으로 해석할 수 있다.
- CONCAT으로 필요한 문자열과 변수를 연결해줘야 한다.
- 각각의 SELECT문에 서로 다른 정렬 기준을 주어야 한다.
- 두 번째 SELECT문은 COUNT함수를 사용하므로 GROUP BY(occupation) 함수를 사용해야 한다.
*
해커랭크는 문제가 모두 영어라서 문제해석을 정확하게 했느냐...가 관건인 듯하다.
'Data > SQL' 카테고리의 다른 글
[HackerRank] The Report (0) | 2023.02.15 |
---|---|
[HackerRank] SQL Basic 문제 풀이 (MySQL) (0) | 2023.02.08 |
[SQLZOO] JOIN 문제 풀이 (0) | 2023.02.01 |
[프로그래머스] SQL Kit 문제 풀이(2) (0) | 2023.01.29 |
[프로그래머스] SQL Kit 문제 풀이(1) (1) | 2023.01.28 |