[SQL][HackerRank]The PADS

Yewon Kimยท2021๋…„ 12์›” 10์ผ
0

SQL

๋ชฉ๋ก ๋ณด๊ธฐ
1/6
post-thumbnail

๐Ÿ”Š๋ณธ ํฌ์ŠคํŒ…์—์„œ ์‚ฌ์šฉ๋˜๋Š” ํ…Œ์ด๋ธ”์˜ ์ž๋ฃŒ์™€ ์ถœ์ฒ˜๋Š” HackerRank ์ž„์„ ๋ฐํž™๋‹ˆ๋‹ค.
https://www.hackerrank.com/challenges/the-pads/problem?isFullScreen=false


๐ŸŽˆ์กฐ๊ฑด 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).

OCCUPATIONS ํ…Œ์ด๋ธ”์˜ ๋ชจ๋“  Name๋“ค์„ ์•ŒํŒŒ๋ฒณ์ˆœ์œผ๋กœ ์ •๋ ฌ๋œ list๋ฅผ ์ฟผ๋ฆฌํ•œ ๋‹ค์Œ, Name ๋’ค์— ๊ฐ Name์— ํ•ด๋‹นํ•˜๋Š” Occupation์˜ ์ฒซ ๋ฒˆ์งธ ๊ธ€์ž๋ฅผ ํ‘œ์‹œํ•ด๋ผ.

SELECT CONCAT(Name, '(', LEFT(Occupation,1), ')')
FROM OCCUPATIONS
ORDER BY Name ASC;

๐ŸŽˆ์กฐ๊ฑด 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.

OCCUPATIONS ํ…Œ์ด๋ธ”์—์„œ ๊ฐ Occupation์˜ ๋ฐœ์ƒ ํšŸ์ˆ˜๋ฅผ ์ฟผ๋ฆฌํ•ด๋ผ. ๋ฐœ์ƒ ํšŸ์ˆ˜๋ฅผ ๊ธฐ์ค€์œผ๋กœ ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌํ•˜๊ณ , ๋‹ค์Œ ํ˜•์‹์œผ๋กœ ์ถœ๋ ฅํ•ด๋ผ.

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

[occupation_count]๋Š” OCCUPATIONS ํ…Œ์ด๋ธ”์—์„œ ๊ฐ Occupation์˜ ๋ฐœ์ƒ ํšŸ์ˆ˜๋ฅผ ์˜๋ฏธํ•˜๊ณ , [occupations]๋ฅผ ์†Œ๋ฌธ์ž๋กœ ์ถœ๋ ฅํ•ด๋ผ.
๋งŒ์•ฝ occupation_count๊ฐ€ ๊ฐ™์€ Occupation์ด ํ•˜๋‚˜ ์ด์ƒ ์กด์žฌํ•  ๊ฒฝ์šฐ, Occupation์„ ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌํ•ด๋ผ.

SELECT CONCAT('There are a total of ', count(*), ' ', Lower(Occupation),'s.')
FROM OCCUPATIONS
GROUP BY Occupation
ORDER BY count(*) ASC, Occupation ASC;

๐ŸŽˆ๋ฌธ์ œํ•ด๊ฒฐ์„ ์œ„ํ•œ ์•„์ด๋””์–ด

  • ์—ฌ๋Ÿฌ ๋ฌธ์ž์—ด์„ ํ•˜๋‚˜๋กœ ํ•ฉ์น˜๊ธฐ ์œ„ํ•ด CONCAT ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•จ.
CONCAT(str1,str2,...)
  • Occupation์˜ ์ฒซ ๋ฒˆ์งธ ๊ธ€์ž๋ฅผ ํ‘œ์‹œํ•˜๊ธฐ ์œ„ํ•ด LEFT ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•จ.
LEFT(๋ฌธ์ž์—ด,๊ธธ์ด)
  • Occupation์„ ์†Œ๋ฌธ์ž๋กœ ์ถœ๋ ฅํ•˜๊ธฐ ์œ„ํ•ด LOWER ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•จ.

0๊ฐœ์˜ ๋Œ“๊ธ€