[HackerRank/SQL] Advanced Select - The PADS
Problem. Advanced Select - The PADS
Link.
The PADS | HackerRank
Query the name and abbreviated occupation for each person in OCCUPATIONS.
www.hackerrank.com
Description.
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.
다음 두 결과 set를 반환하는 SQL문을 작성하시오.
Key Point.
CONCAT, 여러 문자열 혹은 컬럼 값을 합쳐서 반환하는 함수
- ex) CONCAT('Hi', ' ', 'I am', ' ', 'Joy') → ('Hi I am Joy)
SUBSTRING, 문자열을 추출하는 함수
- SUBSTRING(원본 분자열, 시작 위치 값, 가져올 문자열의 길이 값)
- ex) SUBSTRING('Doctor', 1, 1) → 'D'
LOWER, 소문자 변경 함수
- LOWER(문자열)
- ex) LOWER('AbcD') → 'abcd'
UPPER, 소문자 변경 함수
- UPPER(문자열)
- ex) UPPER('abCd') → 'ABCD'
My Answer.
SELECT CONCAT(NAME, '(', Substring(OCCUPATION, 1, 1), ')') as Name
FROM OCCUPATIONS
ORDER BY NAME;
SELECT CONCAT('There are a total of', ' ', COUNT(OCCUPATION), ' ', LOWER(OCCUPATION),'s.') as TOTAL
FROM OCCUPATIONS
GROUP BY OCCUPATION
ORDER BY TOTAL;
References.
CONCAT: https://extbrain.tistory.com/52
[MySQL] 여러 문자열를 하나의 문자열로 합치기 (CONCAT 함수)
▶MySQL 여러 문자열를 하나의 문자열로 합치기 (CONCAT 함수) ▶설명 간혹 여러 문자열 혹은 컬럼 값을 합쳐서 가져와야 하는 경우가 있습니다. 이 때 사용하는 함수가 CONCAT 함수입니다. CONCAT 함수
extbrain.tistory.com