SQL, HackerRank, Ollivander's Inventory ํ’€์ด

Journey logยท2022๋…„ 4์›” 13์ผ
0

sql

๋ชฉ๋ก ๋ณด๊ธฐ
1/7

๐Ÿ’ก ๋ฌธ์ œ ๋งํฌ
https://www.hackerrank.com/challenges/harry-potter-and-wands/problem?isFullScreen=true

์š”์•ฝํ•˜๋ฉด

  • id, age, coins_needed, power ํ•„๋“œ ์ถœ๋ ฅ
  • power์™€ age๋ณ„ ์ตœ์†Œ coins_needed์— ํ•ด๋‹นํ•˜๋Š” ํ–‰๋งŒ ์ถœ๋ ฅ
  • is_evil = 0์ธ ํ–‰๋งŒ ์ถœ๋ ฅ
  • code and age is one-one. (code๊ฐ€ ๋‹ค๋ฅด๋ฉด age๋„ ๋‹ค๋ฆ„)

1. ํ’€์ด(1)

SELECT D.ID, AB.AGE, D.COINS_NEEDED, AB.POWER
FROM(
    SELECT A.POWER, B.AGE, MIN(A.COINS_NEEDED) MIN_COINS
    FROM WANDS A
    JOIN WANDS_PROPERTY B ON A.CODE=B.CODE
    GROUP BY 1,2) AB
LEFT JOIN WANDS_PROPERTY C ON AB.AGE = C.AGE
LEFT JOIN WANDS D ON C.CODE=D.CODE
WHERE D.COINS_NEEDED = AB.MIN_COINS
AND C.IS_EVIL=0
ORDER BY 4 DESC, 2 DESC
;
  • ์šฐ์„  power, age ํ•„๋“œ ๊ทธ๋ฃน๋ณ„ ์ตœ์†Œ coins_needed๋ฅผ ๊ตฌํ•˜๊ณ  ์ด ํ…Œ์ด๋ธ”์—
  • WANDS ํ…Œ์ด๋ธ”๊ณผ WANDS_PROPERTY ํ…Œ์ด๋ธ”์„ left joinํ•จ.

1.1. ํ’€์ด ๊ณผ์ • - "group by ์ดํ›„ left join?"

SELECT A.POWER, B.AGE, MIN(A.COINS_NEEDED) MIN_COINS
FROM WANDS A
JOIN WANDS_PROPERTY B ON A.CODE=B.CODE
GROUP BY 1,2

์—ฌ๊ธฐ๊นŒ์ง€๋Š” power, age๋ณ„ ์ตœ์†Œ coins_needed ๊ฐ’์ด ์ž˜ ์ถœ๋ ฅ๋œ๋‹ค.
๋ฌธ์ œ์—์„œ age์™€ code๊ฐ€ one-one ๊ด€๊ณ„๋ผ๊ณ  ์ฃผ์–ด์กŒ์œผ๋‹ˆ left join์„ ์ฐจ๋ก€๋Œ€๋กœ ํ–ˆ๋Š”๋ฐ ๊ฒฐ๊ณผ๋กœ power, age ๋ณ„ ์ค‘๋ณต ํ–‰์ด ์ƒ๊ฒผ๋‹ค.

-- ์‹คํŒจํ•œ ๋‹ต์•ˆ
SELECT D.ID, AB.AGE, AB.MIN_COINS, AB.POWER
FROM(
    SELECT A.POWER, B.AGE, MIN(A.COINS_NEEDED) MIN_COINS
    FROM WANDS A
    JOIN WANDS_PROPERTY B ON A.CODE=B.CODE
    GROUP BY 1,2) AB
LEFT JOIN WANDS_PROPERTY C ON AB.AGE = C.AGE
LEFT JOIN WANDS D ON C.CODE=D.CODE
WHERE C.IS_EVIL=0
ORDER BY 4 DESC, 2 DESC
;

๊ฒฐ๊ตญ SELECT ๋ฌธ์—์„œ AB.MIN_COINS ๋Œ€์‹  D.COINS_NEEDED๋กœ ์ˆ˜์ •ํ•˜๊ณ  ์กฐ๊ฑด๋ฌธ์œผ๋กœ WHERE D.COINS_NEEDED = AB.MIN_COINS ๋ฅผ ์ถ”๊ฐ€ํ•˜๋‹ˆ ์ œ๋Œ€๋กœ ๋Œ์•„๊ฐ”๋‹ค.

1.2 ๋ฌด์—‡์ด ์ž˜๋ชป๋๋‚˜

LEFT JOIN์„ ์ž˜๋ชป ์ดํ•ดํ•œ ํ’€์ด์˜€๋‹ค. LEFT JOIN์€
1) ์™ผ์ชฝ ํ…Œ์ด๋ธ”์˜ ๋ชจ๋“  ๋ ˆ์ฝ”๋“œ๋ฅผ ๋ฆฌํ„ดํ•˜๊ณ 
2) ์˜ค๋ฅธ์ชฝ์˜ ๋ ˆ์ฝ”๋“œ ์ค‘ ์™ผ์ชฝ ๋ ˆ์ฝ”๋“œ์™€ ์ผ์น˜ํ•˜๋Š” ๊ฒฝ์šฐ๋งŒ ์ฑ„์›Œ์ง„ ์ƒํƒœ๋กœ ๋ฆฌํ„ด๋œ๋‹ค.
์—ฌ๊ธฐ์„  ์™ผ์ชฝ ๋ ˆ์ฝ”๋“œ๊ฐ€ (POWER, AGE) ๊ทธ๋ฃน๋ณ„ ์ตœ์†Œ coin ๋ ˆ์ฝ”๋“œ๊ฐ€ 1๊ฐœ์ด์ง€๋งŒ ์˜ค๋ฅธ์ชฝ ํ…Œ์ด๋ธ”๋“ค์—๋Š” (POWER, AGE)์— ํ•ด๋‹นํ•˜๋Š” ๋ ˆ์ฝ”๋“œ๊ฐ€ ์—ฌ๋Ÿฌ๊ฐœ์ด๋‹ค. ๋”ฐ๋ผ์„œ ์ค‘๋ณตํ–‰์ด ์ƒ๊ธฐ๋Š” ๊ฒƒ์ด๋‹ค.

๋Œ€์‹  ์กฐ๊ฑด๋ฌธ์„ ์ด์šฉํ•˜์—ฌ (POWER, AGE)๋ณ„ ์ตœ์†Œ COIN ๊ฐœ์ˆ˜๋ฅผ ์ฐพ๋Š” ๋ฐฉ๋ฒ•์œผ๋กœ ๋” ๊น”๋”ํ•œ ํ’€์ด๋ฅผ ์ฐพ์•˜๋‹ค.

2. ํ’€์ด(2)

์ถœ์ฒ˜ : https://techblog-history-younghunjo1.tistory.com/165

SELECT A.ID, B.AGE, A.COINS_NEEDED, A.POWER
FROM WANDS A
JOIN WANDS_PROPERTY B ON A.CODE = B.CODE
WHERE B.IS_EVIL = 0
AND A.COINS_NEEDED = (
    SELECT MIN(COINS_NEEDED)
    FROM WANDS W1
    INNER JOIN WANDS_PROPERTY P1 ON W1.CODE = P1.CODE
    WHERE P1.IS_EVIL = 0
    AND W1.POWER = A.POWER
    AND P1.AGE = B.AGE
)
ORDER BY 4 DESC, 2 DESC
;

์„œ๋ธŒ ์ฟผ๋ฆฌ์˜ WHERE ๊ตฌ๋ฌธ์—์„œ ๋ฉ”์ธ ์ฟผ๋ฆฌ์˜ ์นผ๋Ÿผ์„ ์ด์šฉํ•˜๋Š” ์˜ˆ์‹œ๋Š” ์ฒ˜์Œ ๋ดค๋Š”๋ฐ ํ›จ์”ฌ ๊น”๋”ํ•œ ๊ฒƒ ๊ฐ™๋‹ค.

  • WHERE A.COINS_NEEDED = (์„œ๋ธŒ์ฟผ๋ฆฌ) ํ˜•์‹
  • ์„œ๋ธŒ์ฟผ๋ฆฌ ์กฐ๊ฑด๋ฌธ์— ๋ฉ”์ธ์ฟผ๋ฆฌ์˜ ์นผ๋Ÿผ ์ด์šฉ. (power, age) ๋ณ„ ์ตœ์†Œ coin๊ฐ’์— ํ•ด๋‹นํ•˜๋Š” ํ–‰ ๋ฝ‘๊ธฐ.
profile
DEEP DIVER

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