728x90
반응형

Summary

이 문제는 SELECT 의 결과를 어떻게 하나의 문구로 합칠것인가에 대해서 묻는 문제였다.

 

첫 번째 문제로는 이름과 직업의 앞 자리를 가져와 () 괄호 안에 넣어주어 하나로 만들어주어야 한다.
이름은 그대로 가져오면 되었기 때문에 문제가 되지 않았고 () 괄호 안에 직업의 맨 앞 한자리 문자를 가져와 넣어주어야 한다.
LEFT 문법을 사용해 직업의 맨 앞 한자리만 가져온다.
그리고 이름과 괄호를 포함한 문자를 합쳐주어야 한다. 합치기 위해서 CONCAT 문법을 사용해 하나로 합쳐주었다.

 

CONCAT 으로 합쳐질 문구는 다음과 같다.

  • name
  • '('
  • LEFT(occupation, 1) -> occupation 의 왼쪽 1자리의 문자만 가져온다.
  • ')'

 

따라서, CONCAT 을 사용해 합쳐서 출력해줄 수 있었다.

CONCAT(name, '(', LEFT(occupation, 1), ')')

 

 

그리고 다음 문제로는 문자의 빈칸으로 직업의 수와 직업을 넣어주어야 한다.
요구하는 문구는 다음과 같았다.

 

There are a total of [occupation_count] [occupation]s.

 

그리고 또하나, occupation 은 직업의 맨 앞의 문자가 대문자로 되어있다.
하지만 출력을 원하는 건 문제에 빨간색으로 표시해둔 것처럼 소문자를 원하고 있다.

 

[occupation] is the lowercase occupation name. 

 

따라서 LOWER 문법을 사용해 소문자로 변환해주어야 한다.
예시) Doctor -> doctor

 

이 문구를 나누면 다음과 같이 나눌 수 있다.

  • "There are a total of " -> 뒤에 공백을 추가해준다.
  • count(occupation)
  • " " (공백)
  • lower(occupation)
  • s.

 

따라서, CONCAT 을 사용해 합쳐서 출력해준다.

concat('There are a total of ', count(occupation), ' ', lower(occupation), 's.')

 

 

Problem

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.

Sample Input

An OCCUPATIONS table that contains the following records:

Sample Output

Ashely(P)
Christeen(P)
Jane(A)
Jenny(D)
Julia(A)
Ketty(P)
Maria(A)
Meera(S)
Priya(S)
Samantha(D)
There are a total of 2 doctors.
There are a total of 2 singers.
There are a total of 3 actors.
There are a total of 3 professors.

Explanation

The results of the first query are formatted to the problem description's specifications. 
The results of the second query are ascendingly ordered first by number of names corresponding to each profession (2<=2<=3<=3), and then alphabetically by profession (doctor <= singer, and actor <= professor).

 

Submit Code

select concat(name, '(', left(occupation, 1), ')')
from occupations
order by name asc;

select concat('There are a total of ', count(occupation), ' ', lower(occupation), 's.')
from occupations
group by occupation
order by count(occupation) asc

 

Result

728x90
반응형
복사했습니다!