SQL

[Hackerrrank] The PADS

seonghojang 2024. 1. 17. 20:49
 

The PADS | HackerRank

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

www.hackerrank.com

 

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:

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.

 

SELECT CONCAT(name, '(', SUBSTRING(occupation, 1, 1), ')')
FROM occupations
ORDER BY name, SUBSTRING(occupation, 1, 1);

SELECT CONCAT('There are a total of ', COUNT(*), ' ', LOWER(occupation), 's.')
FROM occupations
GROUP BY occupation
ORDER BY count(*), occupation;

 

요구사항대로 풀면 되어서 어렵지 않은 문제긴 했는데,

두번째 쿼리에 대문자, 문장부호, s 등 빠뜨린게 많아서 계속 틀렸던 문제였다.

CONCAT 함수의 사용법과 문자열 다루는 문제에서는 좀더 집중해야한다는 것을 배운 문제..