[SQL][HackerRank]Ollivander's Inventory

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

SQL

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

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


๐ŸŽˆ๋ฌธ์ œ ํ’€์ด

Harry Potter and his friends are at Ollivander's with Ron, finally replacing Charlie's old broken wand.
Hermione decides the best way to choose is by determining the minimum number of gold galleons needed to buy each non-evil wand of high power and age. Write a query to print the id, age, coins_needed, and power of the wands that Ron's interested in, sorted in order of descending power. If more than one wand has same power, sort the result in order of descending age.

ํ•ด๋ฆฌํฌํ„ฐ์™€ ์นœ๊ตฌ๋“ค์€ ์ฐฐ๋ฆฌ์˜ ์˜ค๋ž˜๋˜๊ณ  ๋ถ€์…”์ง„ ์ง€ํŒก์ด๋ฅผ ๊ต์ฒดํ•˜๊ธฐ ์œ„ํ•ด ๋ก ๊ณผ ์˜ฌ๋ฆฌ๋ฒค๋” ์ง€ํŒก์ด ๊ฐ€๊ฒŒ๋ฅผ ๋ฐฉ๋ฌธํ–ˆ๋‹ค.
ํ—ค๋ฅด๋ฏธ์˜จ๋А๋Š” ์ง€ํŒก์ด๋ฅผ ๊ณ ๋ฅด๋Š” ์ตœ์„ ์˜ ๋ฐฉ๋ฒ•์œผ๋กœ์„œ non-evil ์ง€ํŒก์ด์ด๋ฉด์„œ ๋†’์€ power์™€ age ํŠน์„ฑ์„ ๊ฐ–๋Š” ์ง€ํŒก์ด๋ฅผ ์‚ฌ๊ธฐ ์œ„ํ•ด ํ•„์š”ํ•œ ์ตœ์†Œํ•œ์˜ ๋ˆ(gold galleons)์„ ๊ณ ๋ฅด๋Š” ๋ฐฉ๋ฒ•์„ ์ œ์•ˆํ–ˆ๋‹ค. ์ด ๋ฐฉ๋ฒ•์„ ์ ์šฉํ–ˆ์„ ๋•Œ, ๋ก ์ด ๊ด€์‹ฌ์žˆ์–ด ํ•  ๋งŒํ•œ ์ง€ํŒก์ด์˜ id, age, coins_needed, power ๊ฐ’๋“ค์„ ์ถœ๋ ฅํ•ด๋ผ. ์ด ๋•Œ 1์ฐจ์ ์œผ๋กœ ์ •๋ ฌ ๊ธฐ์ค€์€ power ๊ธฐ์ค€์œผ๋กœ ๋‚ด๋ฆผ์ฐจ์ˆœ ์ •๋ ฌํ•˜๋ฉฐ, ๋™์ผํ•œ power ๊ฐ’์ด ์žˆ์„ ๋•Œ๋Š” age ๊ฐ’์„ ๊ธฐ์ค€์œผ๋กœ ๋‚ด๋ฆผ์ฐจ์ˆœ ์ •๋ ฌํ•ด๋ผ.

  1. code์™€ power๋ฅผ ๊ทธ๋ฃนํ™”ํ•˜์—ฌ ์ตœ์†Œ๋น„์šฉ์„ ๊ฐ–๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ์ƒ์„ฑํ•œ๋‹ค.
(SELECT code, MIN(coins_needed) AS coins_needed, power
FROM Wands
GROUP BY code, power) AS M

์ด๋•Œ, code์™€ power๋ฅผ Groupingํ•˜์—ฌ ์ตœ์†Œ ๊ธˆ์•ก์„ ์ถœ๋ ฅํ•˜๋Š”๋ฐ id๊ฐ€ ํฌํ•จ๋˜์–ด ์žˆ์„ ๊ฒฝ์šฐ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•˜๊ธฐ ๋•Œ๋ฌธ์— id๋ฅผ ๋บ€ ์ƒํƒœ๋กœ GROUP BY๋ฅผ ํ•ด์•ผํ•œ๋‹ค.

๐Ÿ‘‰ ํ•ด๋‹น ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•˜๋Š” ์ด์œ ๋Š” ๋ฐ”๋กœ GROUP BY๊ฐ€ ํ•˜๊ณ ์‹ถ์€ "๋ฐ์ดํ„ฐ์˜ ๊ทธ๋ฃนํ™”"๋ฅผ ํ•˜์ง€ ๋ชปํ•˜๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. ์ฆ‰, ํ˜„์žฌ ์ฟผ๋ฆฌ๋ฌธ์ด ๊ทธ๋ฃนํ™”๋ฅผ ํ•  ์ˆ˜ ์—†๋Š” ์ƒํƒœ๋ผ๋Š” ์ด์•ผ๊ธฐ์ด๋‹ค.

  1. id ์ปฌ๋Ÿผ์„ ์ถ”๊ฐ€ํ•˜๊ธฐ ์œ„ํ•ด Wands ํ…Œ์ด๋ธ”๊ณผ joinํ•œ๋‹ค.
(SELECT code, MIN(coins_needed) AS coins_needed, power
      FROM Wands
      GROUP BY code, power) AS M
INNER JOIN Wands AS W ON M.code=W.code AND M.power=W.power AND M.coins_needed=W.coins_needed
  1. age, is_evil ์ปฌ๋Ÿผ์„ ์ถ”๊ฐ€ํ•˜๊ธฐ ์œ„ํ•ด Wands_Property ํ…Œ์ด๋ธ”๊ณผ joinํ•œ๋‹ค.
(SELECT code, MIN(coins_needed) AS coins_needed, power
      FROM Wands
      GROUP BY code, power) AS M
INNER JOIN Wands AS W ON M.code=W.code AND M.power=W.power AND M.coins_needed=W.coins_needed
INNER JOIN Wands_Property AS P ON P.code=W.code
  1. 'non-evil์ง€ํŒก์ด'๋ผ๋Š” ์กฐ๊ฑด์„ ๋งŒ์กฑ์‹œํ‚ค๊ธฐ ์œ„ํ•ด ์กฐ๊ฑด์ ˆ์„ ์ถ”๊ฐ€ํ•œ๋‹ค.
WHERE P.is_evil=0
  1. ๋ฌธ์ œ์— ์ œ์‹œ๋˜์–ด ์žˆ๋Š” ์ •๋ ฌ์ˆœ์„œ์— ๋งž๊ฒŒ ์ •๋ ฌํ•œ๋‹ค.
ORDER BY W.power DESC, P.age DESC
  1. ๋ก ์ด ๊ด€์‹ฌ์žˆ์–ด ํ•  ๋งŒํ•œ ์ง€ํŒก์ด์˜ id, age, coins_needed, power ๊ฐ’๋“ค์„ ์ถœ๋ ฅํ•œ๋‹ค.
SELECT W.id, P.age, W.coins_needed, W.power
  1. ์ตœ์ข…์ฝ”๋“œ
SELECT W.id, P.age, W.coins_needed, W.power
FROM (SELECT code, MIN(coins_needed) AS coins_needed, power
      FROM Wands
      GROUP BY code, power) AS M
INNER JOIN Wands AS W ON M.code=W.code AND M.power=W.power AND M.coins_needed=W.coins_needed
INNER JOIN Wands_Property AS P ON P.code=W.code
WHERE P.is_evil=0
ORDER BY W.power DESC, P.age DESC;

๐ŸŽˆ์˜๊ฒฌ
์ด ๋ฌธ์ œ๋ฅผ ๋‹ค์†Œ ๋งŽ์€ ์‹œ๊ฐ„์ด ์†Œ์š”๋˜์—ˆ๊ณ , ๋‚˜์—๊ฒŒ ์–ด๋ ต๊ฒŒ ๋А๊ปด์ง„ ๋ฌธ์ œ์ด๋‹ค. ๊ณผ์ • 1์—์„œ id๋ฅผ ํฌํ•จํ•˜์˜€์„ ๊ฒฝ์šฐ, GROUP BY ์˜ค๋ฅ˜๊ฐ€ ์™œ ๋ฐœ์ƒํ•˜๋Š”์ง€์— ๋Œ€ํ•œ ์ •ํ™•ํ•œ ์ด์œ ๋ฅผ ๋ชฐ๋ž์—ˆ๊ธฐ ๋•Œ๋ฌธ์— ๋ฌธ์ œ๋ฅผ ํ‘ธ๋Š” ๊ณผ์ •์—์„œ ๋งŽ์ด ํ—ค๋งธ๋‹ค. GROUP BY ์˜ค๋ฅ˜๋ฅผ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•ด ๊ณผ์ • 2์ฒ˜๋Ÿผ join์„ ์‚ฌ์šฉํ•˜๋ฉด ๋œ๋‹ค๋ผ๋Š” ์‚ฌ์‹ค์„ ์•Œ๊ฒŒ๋˜์—ˆ๋‹ค.

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