๐ป ๋ฌธ์ ์ฃผ์ : The PADS | HackerRank
The PADS | HackerRank
Query the name and abbreviated occupation for each person in OCCUPATIONS.
www.hackerrank.com
๐ป ๋ฌธ์
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 occurrences of each occupation in OCCUPATIONS. Sort the occurrences in asceding order, and output them in the following format: There are a total of [occupationcount] [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.
Noted: There will be at least two entries in the table for each tupe of occupation.
Column name | Type |
Name | String |
Occupation | String |
๐ป ์ฝ๋ ๋ฏธ๋ฆฌ๋ณด๊ธฐ
* ๋ณธ ๋ฌธ์ ๋ MYSQL๋ก ์์ฑํ์ต๋๋ค.
-- Name[Occupation]
select concat(name, "(", substr(occupation, 1, 1), ")")
from OCCUPATIONS
order by name;
-- There are a total of [Occupation_count] [Occupation]s.
select concat("There are a total of ", count(occupation), " ", lower(occupation), "s.")
from OCCUPATIONS
group by occupation
order by count(occupation), lower(occupation);
๐ป ๋ฌธ์ ํ์ด
๋ฌธ์ ์์ ์๊ตฌํ๋ ์กฐ๊ฑด์ ์๋์ ๊ฐ๋ค.
1๏ธโฃ Query an ordered list of all names in OCCUPATIONS, followed by the first letter of Occupation
→ i.g., Ashley(P)
2๏ธโฃ Query the number of occurrences of each occupation in OCCUPATIONS, followed by the occurrences(asc), and the first letter of Occupation
→ i.g., There are a total of [Occupation_count] [Occupation]s.
→ It should be printed using 'lowercase'
1๏ธโฃ Query an ordered list of all names in OCCUPATIONS, followed by the first letter of Occupation
์ด๋ฒ ๋ฌธ์ ์ ํต์ฌ์ '์ถ๋ ฅ ํํ'๋ฅผ ๋ง๋ค์ด์ค์ผ ํ๋ค๋ ๊ฒ์ด๋ค.
์ ์์์ฒ๋ผ ์ฒซ ๋ฒ์งธ ์ฟผ๋ฆฌ์์๋ Name(์ด๋ฆ) ์์ ๊ดํธ๋ฅผ ๋ง๋ค๊ณ , ๊ทธ ์์ Occupation(์ง์ )์ ์ ๊ธ์๋ฅผ ๋ฐ ๋ฃ์ด์ค์ผ ํ๋ค. ์ด์ ๊น์ง HackerRank์์ ์ด๋ฐ ๋ฌธ์์ด ๋ฌธ์ ๋ ๋ณดํต Type์ด Varchar ํํ์์ผ๋, ์ด๋ฒ ๋ฌธ์ ์์๋ ์ด๋ฆ๊ณผ ์ง์ ๋ชจ๋ STRING ํํ์ธ ์ ์ ์ ์ํด์ผ ํ๋ค.
Mysql์์ String ํ์ ์ ๋ณ์ ๋ด ๋ฌธ์๋ฅผ ์ถ์ถํ๊ณ ์ ํ ๋ Substr() (๋๋ Substring() ํจ์๋ฅผ ์ด๋ค. Substr() ํจ์๋, String ํ์ ๋ฌธ์์ด ๋ด์์ ์ํ๋ ๊ธธ์ด๋งํผ ๋ฌธ์๋ฅผ ์ถ์ถํ๋ ๊ฒ์ผ๋ก, ์๋์ ๊ฐ์ ํ์์ ๋ฐ๋ฅธ๋ค.
--. Substr(๋ฌธ์์ด, ์์ ์์น, ์ถ์ถํ ๊ธธ์ด)
์ด๋ '์ถ์ถํ ๊ธธ์ด'์ ๋ณ๋ค๋ฅธ ๋งค๊ฐ๋ณ์๋ฅผ ๋ฃ์ง ์์ ๊ฒฝ์ฐ, ์์ ์์น๋ก๋ถํฐ ๋ฌธ์์ด ๋๊น์ง ๋ค ์ถ์ถํ๋ค. ์ฐ๋ฆฌ๋ Occupation์ ์ ๊ธ์๋ง ํ์ํ๋ ์๋์ ๊ฐ์ด ์ถ์ถํด์ผ ํ๋ค.
select substr(occupation, 1)
๋ค๋ง ์ด๋ ๊ฒ ๋๋ฉด Name(์ด๋ฆ)๊ณผ ๊ดํธ, ์ฆ ๋ฌธ์ ์์ ์๊ตฌํ๋ ์ถ๋ ฅ ํํ๋ฅผ ๊ฐ์ถ์ง ๋ชปํ๊ธฐ์ ์๋ก์ด ๋ฌธ์์ด์ ๋ง๋ค์ด์ค์ผ ํ๋ค. ์ด๋ฅผ ์ํด Concat() ํจ์๋ฅผ ์ฐ๋ฉด ํธํ๋ค.
-. Concat(๋ฌธ์์ด1, ๋ฌธ์์ด2, ๋ฌธ์์ด3,,,)
์ ํํ์์ concat()์ ๋ ์ด์์ ๋ฌธ์์ด์ ํฉ์ณ์ฃผ๋ ํจ์์ด๋ค. ์ฐ๋ฆฌ๋ "name(์ด๋ฆ)+"("+Occupation(์๊ธ์)+")"" ํํ๊ฐ ํ์ํ๊ธฐ์ ์ด๋ฅผ ๊ทธ๋๋ก concat ํจ์ ์์ ๋ฃ์ด์ฃผ๋ฉด 1๏ธโฃ ๋ฌธ์ ๋ ๋๋๋ค.
select concat(name, "(", substr(occupation, 1, 1), ")") from OCCUPATIONS order by name;
2๏ธโฃ Query the number of occurrences of each occupation in OCCUPATIONS, followed by the occurrences(asc), and the first letter of Occupation
์ด์ ๋ฌธ์ ๋ 2๏ธโฃ ์ด๋ค.
๋ฌธ์ ์ ์ํ๋ฉด ๋ ๋ฒ์งธ ์ฟผ๋ฆฌ์์๋ (1) occupation(์ง์ )๋ณ๋ก ๋ช ๋ช ์ด ์๋์ง ๊ตฌํด์ผ ํ๋ฉฐ (2) ์ถ๋ ฅ ํํ๋ "There are a total of (๋ช ๋ช ) (occupation, ์ง์ )s."์ด๊ณ , ๋ง์ง๋ง์ผ๋ก (3) ์ง์ ๋ณ ์นด์ดํธ ์ซ์์ ์ํ๋ฒณ์์ผ๋ก ์ ๋ ฌํด์ผ ํ๋ค. ๋ํ (2) ์ถ๋ ฅ ํํ์์ occupation(์ง์ ) ๋ฌธ์์ด์ ๋ชจ๋ ์๋ฌธ์๋ก ์ถ๋ ฅํด์ผ ํ๋ค๋ ์กฐ๊ฑด์ด ๋ถ์๋ค.
์ฐจ๊ทผ์ฐจ๊ทผ ์ ๊ทผํด๋ณด์. (1) occupation(์ง์ )๋ณ๋ก ๋ช ๋ช ์ด ์๋์ง ๊ตฌํด์ผ ํ๋๋ฐ, ์ด๋ group by๋ฅผ ์ฐ๊ณ ๋ ํ count๋ก occupation์ ์ธ๋ฉด ๋๋ค.
select count(occupation)
from OCCUPATIONS
group by occupation
(2) ์ถ๋ ฅ ํํ๋ฅผ ์ 1๏ธโฃ ์์ ๊ตฌํ ๊ฒ์ฒ๋ผ ์๋ก์ด ๋ฌธ์์ด์ ๋ง๋ค์ด์ฃผ๋ฉด ๋๋๋ฐ, ์ด๋ ์ฃผ์ํด์ผ ํ ๊ฒ์ 'occupation ๋ฌธ์์ด์ ๋ชจ๋ ์๋ฌธ์'๋ก ์ถ๋ ฅํด์ผ ํ๋ค๋ ๊ฒ์ด๋ค. ํ์ฌ occupation ๋ฐ์ดํฐ๋ ์๊ธ์๊ฐ ๋ชจ๋ ๋๋ฌธ์๋ก ๋์ด ์๊ธฐ์ ์ด๋ฅผ ๊ฐ์ ๋ก ์๋ฌธ์๋ก ๋ง๋ค๊ธฐ ์ํด์๋ lower() ํจ์๋ฅผ ์จ์ผ ํ๋ค.
select concat("There are a total of ", count(occupation), " ", lower(occupation), "s.")
from OCCUPATIONS
group by occupation
์ด์ ๋ง์ง๋ง, (3) ์ ๋ ฌ๋ง ์ ๊ฒฝ์ฐ๋ฉด ๋๋ค. ์ฐ๋ฆฌ๋ ์ง์ ๋ณ ์นด์ดํธ ์ซ์์ ์ง์ ์ ์ํ๋ฒณ ์์ผ๋ก ์ ๋ ฌํ๋ฉด ๋๊ธฐ ๋๋ฌธ์ ์ด๋ฅผ ๊ฐ๋จํ๊ฒ order by์ ์ฐจ๊ทผ์ฐจ๊ทผ ์ฎ๊ฒจ ์ ์ผ๋ฉด ๋๋ค.
select concat("There are a total of ", count(occupation), " ", lower(occupation), "s.")
from OCCUPATIONS
group by occupation
order by count(occupation), lower(occupation);
๊ฐ์ธ์ ์ผ๋ก ์ด๋ฒ ๋ฌธ์ ์์ ์ ์ผ ์ด๋ ค์ ๋ ์ ์ occupation ์ฒ ์๋ฅผ ์๊พธ๋ง occupatation์ด๋ผ๊ณ ์ ์ด์ ์๋ฌ๊ฐ ๋ฌ๋ค๋ ๊ฒ์ด๋ค๐ ์ง์ ์ Job์ ๋ง์ด ์ฐ๋ ์ค ์์๋๋ฐ Occupation์ ์ฐ๊ธฐ๋ ํ๋ค๋ ๊ฑธ ๋ฌธ์ ํ๋ฉด์ ์๊ฒ ๋๋ค. ์์ด ํํ ์นดํ ๊ณ ๋ฆฌ๋ ์ฌ ์ถ๊ฐํด์ผ ํ๋๋ฐ ์ธ์ ํ์ง..
'SQL > HackerRank' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[SQL] Weather Observation Station 18 (0) | 2024.05.03 |
---|---|
[SQL] Weather Observation Station 5 (0) | 2024.04.24 |
[SQL] Weather Observation Station 3 (1) | 2024.03.12 |
[SQL] Revising the Select Query I (0) | 2024.03.11 |