๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
SQL/HackerRank

[SQL] The PADS

by ์ด๋ฎด 2024. 4. 30.
728x90

๐Ÿ’ป ๋ฌธ์ œ ์ฃผ์†Œ : 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์„ ์“ฐ๊ธฐ๋„ ํ•œ๋‹ค๋Š” ๊ฑธ ๋ฌธ์ œ ํ’€๋ฉด์„œ ์•Œ๊ฒŒ ๋๋‹ค. ์˜์–ด ํ‘œํ˜„ ์นดํ…Œ๊ณ ๋ฆฌ๋„ ์Šฌ ์ถ”๊ฐ€ํ•ด์•ผ ํ•˜๋Š”๋ฐ ์–ธ์ œ ํ•˜์ง€..

 

728x90
๋ฐ˜์‘ํ˜•

'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