Potato
์•ˆ๋…•ํ•˜์„ธ์š”, ๊ฐ์žก๋‹ˆ๋‹ค?๐Ÿฅ” ^___^ ๐Ÿ˜บ github ๋ฐ”๋กœ๊ฐ€๊ธฐ ๐Ÿ‘‰๐Ÿป

Algorithm/SQL Query test

[MySQL] ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค SQL ๊ณ ๋“์  Kit - ์šฐ์œ ์™€ ์š”๊ฑฐํŠธ๊ฐ€ ๋‹ด๊ธด ์žฅ๋ฐ”๊ตฌ๋‹ˆ (level 4)

๊ฐ์ž ๐Ÿฅ” 2021. 5. 3. 14:43
๋ฐ˜์‘ํ˜•

programmers.co.kr/learn/courses/30/lessons/62284

 

์ฝ”๋”ฉํ…Œ์ŠคํŠธ ์—ฐ์Šต - ์šฐ์œ ์™€ ์š”๊ฑฐํŠธ๊ฐ€ ๋‹ด๊ธด ์žฅ๋ฐ”๊ตฌ๋‹ˆ

CART_PRODUCTS ํ…Œ์ด๋ธ”์€ ์žฅ๋ฐ”๊ตฌ๋‹ˆ์— ๋‹ด๊ธด ์ƒํ’ˆ ์ •๋ณด๋ฅผ ๋‹ด์€ ํ…Œ์ด๋ธ”์ž…๋‹ˆ๋‹ค. CART_PRODUCTS ํ…Œ์ด๋ธ”์˜ ๊ตฌ์กฐ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์œผ๋ฉฐ, ID, CART_ID, NAME, PRICE๋Š” ๊ฐ๊ฐ ํ…Œ์ด๋ธ”์˜ ์•„์ด๋””, ์žฅ๋ฐ”๊ตฌ๋‹ˆ์˜ ์•„์ด๋””, ์ƒํ’ˆ ์ข…๋ฅ˜, ๊ฐ€

programmers.co.kr

๋ฌธ์ œ

CART_PRODUCTS ํ…Œ์ด๋ธ”์€ ์žฅ๋ฐ”๊ตฌ๋‹ˆ์— ๋‹ด๊ธด ์ƒํ’ˆ ์ •๋ณด๋ฅผ ๋‹ด์€ ํ…Œ์ด๋ธ”์ž…๋‹ˆ๋‹ค. CART_PRODUCTS ํ…Œ์ด๋ธ”์˜ ๊ตฌ์กฐ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์œผ๋ฉฐ, ID, CART_ID, NAME, PRICE๋Š” ๊ฐ๊ฐ ํ…Œ์ด๋ธ”์˜ ์•„์ด๋””, ์žฅ๋ฐ”๊ตฌ๋‹ˆ์˜ ์•„์ด๋””, ์ƒํ’ˆ ์ข…๋ฅ˜, ๊ฐ€๊ฒฉ์„ ๋‚˜ํƒ€๋ƒ…๋‹ˆ๋‹ค.

๋ฐ์ดํ„ฐ ๋ถ„์„ ํŒ€์—์„œ๋Š” ์šฐ์œ (Milk)์™€ ์š”๊ฑฐํŠธ(Yogurt)๋ฅผ ๋™์‹œ์— ๊ตฌ์ž…ํ•œ ์žฅ๋ฐ”๊ตฌ๋‹ˆ๊ฐ€ ์žˆ๋Š”์ง€ ์•Œ์•„๋ณด๋ ค ํ•ฉ๋‹ˆ๋‹ค. ์šฐ์œ ์™€ ์š”๊ฑฐํŠธ๋ฅผ ๋™์‹œ์— ๊ตฌ์ž…ํ•œ ์žฅ๋ฐ”๊ตฌ๋‹ˆ์˜ ์•„์ด๋””๋ฅผ ์กฐํšŒํ•˜๋Š” SQL ๋ฌธ์„ ์ž‘์„ฑํ•ด์ฃผ์„ธ์š”. ์ด๋•Œ ๊ฒฐ๊ณผ๋Š” ์žฅ๋ฐ”๊ตฌ๋‹ˆ์˜ ์•„์ด๋”” ์ˆœ์œผ๋กœ ๋‚˜์™€์•ผ ํ•ฉ๋‹ˆ๋‹ค.

 

์˜ˆ๋ฅผ ๋“ค์–ด CART_PRODUCTS ํ…Œ์ด๋ธ”์ด ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค๋ฉด

  • 83๋ฒˆ ์žฅ๋ฐ”๊ตฌ๋‹ˆ์—๋Š” Milk์™€ Yogurt๊ฐ€ ๋ชจ๋‘ ์—†์Šต๋‹ˆ๋‹ค.
  • 286๋ฒˆ ์žฅ๋ฐ”๊ตฌ๋‹ˆ์—๋Š” Milk์™€ Yogurt๊ฐ€ ๋ชจ๋‘ ์žˆ์Šต๋‹ˆ๋‹ค.
  • 448๋ฒˆ ์žฅ๋ฐ”๊ตฌ๋‹ˆ์—๋Š” Milk์™€ Yogurt๊ฐ€ ๋ชจ๋‘ ์žˆ์Šต๋‹ˆ๋‹ค.
  • 1034๋ฒˆ ์žฅ๋ฐ”๊ตฌ๋‹ˆ์—๋Š” Milk๋Š” ์—†๊ณ  Yogurt๋งŒ ์žˆ์Šต๋‹ˆ๋‹ค.

๋”ฐ๋ผ์„œ SQL ๋ฌธ์„ ์‹คํ–‰ํ•˜๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋‚˜์™€์•ผ ํ•ฉ๋‹ˆ๋‹ค.

 

๋‚ด๊ฐ€ ํ•œ ํ’€์ด

1. ์šฐ์„  Milk์™€ Yogurt๋ฅผ ๋‹ด์€ ์žฅ๋ฐ”๊ตฌ๋‹ˆ๊ฐ€ ๋ฌด์—‡์ด ์žˆ๋Š”์ง€ ์•Œ์•„๋ณด๊ธฐ ์œ„ํ•ด ์ฟผ๋ฆฌ๋ฅผ ์ž‘์„ฑํ–ˆ๋‹ค.

SELECT DISTINCT CART_ID
, NAME
FROM CART_PRODUCTS
WHERE (NAME LIKE 'Milk' OR NAME LIKE 'Yogurt')

NAME์— 'Milk' ๋˜๋Š” 'Yogurt'๊ฐ€ ๋“ค์–ด๊ฐ„๋Š” ์žฅ๋ฐ”๊ตฌ๋‹ˆ์˜ CART_ID์™€ NAME์„ ์ถœ๋ ฅํ•˜๋Š” ์ฟผ๋ฆฌ๋ฅผ ์ž‘์„ฑํ–ˆ๋‹ค.
๋˜ํ•œ, MILK ํ˜น์€ YOGURT ๋ฅผ ๋‹ด์€ ๋™์ผํ•œ ์žฅ๋ฐ”๊ตฌ๋‹ˆ๊ฐ€ ์—ฌ๋Ÿฌ๊ฐœ๊ฐ€ ์ถœ๋ ฅ๋˜๋Š” ๊ฒƒ์„ ๋ฐฉ์ง€ํ•˜๊ธฐ ์œ„ํ•ด DISTINCT๋ฅผ ๊ฑธ์–ด์ฃผ์—ˆ๋‹ค.
์ด์— ๋Œ€ํ•œ ๊ฒฐ๊ณผ๋Š” ์•„๋ž˜ <๊ทธ๋ฆผ 1>๊ณผ ๊ฐ™์ด ์ถœ๋ ฅ๋œ๋‹ค.

<๊ทธ๋ฆผ 1> ์ดํ•˜ ์ƒ๋žต

2. ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š”, ๋™์ผํ•œ CART_ID ๋‚ด์— MILK, YOGURT๊ฐ€ ๋ชจ๋‘ ๋“ค์–ด๊ฐ€ ์žˆ์–ด์•ผ ํ•œ๋‹ค. ๋”ฐ๋ผ์„œ ๋‚˜๋Š” <๊ทธ๋ฆผ 1>์˜ ํ…Œ์ด๋ธ”์„ ํ™œ์šฉํ•˜๋ฉด ๋œ๋‹ค๊ณ  ํŒ๋‹จํ–ˆ๋‹ค.

<๊ทธ๋ฆผ 1> ํ…Œ์ด๋ธ”์„ CART_ID ๊ธฐ์ค€์œผ๋กœ GROUP BY ํ•ด์„œ COUNT ํ•ด์ฃผ๋ฉด, MILK์™€ YOGURT๊ฐ€ ๋ชจ๋‘ ๋“ค์–ด์žˆ์œผ๋ฉด 2, ๋‘˜ ์ค‘ ํ•˜๋‚˜๋งŒ ๋“ค์–ด์žˆ์œผ๋ฉด 1์ด ์ถœ๋ ฅ๋  ๊ฒƒ์ด๋‹ค.

<๊ทธ๋ฆผ 1>์—์„œ๋„ ๋ณด๋ฉด 286 - YOGURT , 286 - MILK => 286์€ ๋‘๋ฒˆ ๋“ฑ์žฅํ–ˆ๊ณ , 287 - MILK ๋Š” ํ•œ ๋ฒˆ๋งŒ ๋“ฑ์žฅํ–ˆ๋‹ค. ์•ž์„œ DISTINCT๋ฅผ ๊ฑธ์–ด์ฃผ์–ด ์ค‘๋ณต์„ ๋ง‰์•˜๊ธฐ ๋•Œ๋ฌธ์—,  MILK๋ฅผ ๋‘๋ฒˆ๋‹ด์•„์„œ COUNT = 2 ๊ฐ€ ๋˜๋ฉด ์–ด์ฉŒ์ง€? ํ•˜๋Š” ๊ฑฑ์ •์€ ์•ˆํ•ด๋„ ๋œ๋‹ค. 

๊ฒฐ๋ก ์ ์œผ๋กœ, <๊ทธ๋ฆผ 1>์—์„œ CART_ID ๋ฅผ COUNT ํ•ด์ฃผ๊ณ , 2 ์ด์ƒ์˜ CART_ID๋ฅผ ์ถœ๋ ฅํ•ด์ฃผ๋ฉด ๋œ๋‹ค. ๋‚˜๋Š” ์ด๊ฒƒ์„ ์„œ๋ธŒ์ฟผ๋ฆฌ๋ฅผ ํ™œ์šฉํ•ด์„œ ์ถœ๋ ฅํ•ด๋ณด์•˜๋‹ค.

<< ์ •๋‹ต >>

SELECT A.CART_ID
FROM (SELECT DISTINCT CART_ID
        , NAME
        FROM CART_PRODUCTS
        WHERE (NAME LIKE 'Milk' OR NAME LIKE 'Yogurt')) A
GROUP BY A.CART_ID
HAVING COUNT(A.CART_ID) > 1
ORDER BY A.CART_ID

์•ž์„œ <๊ทธ๋ฆผ 1>์„ ์ถœ๋ ฅํ–ˆ๋˜ ์ฟผ๋ฆฌ๋ฅผ FROM์ ˆ์— ์„œ๋ธŒ์ฟผ๋ฆฌ๋กœ ๋„ฃ์—ˆ๋‹ค. ๋”ฐ๋ผ์„œ ์ด ์ฝ”๋“œ๋ฅผ ํ•ด์„ํ•ด๋ณด๋ฉด,
<๊ทธ๋ฆผ 1>์—์„œ CART_ID ๊ธฐ์ค€์œผ๋กœ GROUPํ™” ํ•œ ํ›„,  COUNT(CART_ID) ๊ฐ€ 1์ด์ƒ์ด ๋˜๋Š” CART_ID๋ฅผ SELECTํ•œ๋‹ค๋Š” ์˜๋ฏธ๊ฐ€ ๋œ๋‹ค. ๋งˆ์ง€๋ง‰์œผ๋กœ CART_ID ์ˆœ์œผ๋กœ ์ •๋ ฌํ•˜๋ฉด์„œ ๋๋งบ์œผ๋ฉด ๋œ๋‹ค.

LEVEL 4๋กœ ๊ฐ€์žฅ ์ ์€ ์ธ์›์ด ํ•ด๋‹น ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•œ ๊ฒƒ์œผ๋กœ ๋ณด์ธ๋‹ค. ํ•˜์ง€๋งŒ ์„œ๋ธŒ์ฟผ๋ฆฌ์— ๋Œ€ํ•œ ์ดํ•ด์™€, ํ…Œ์ด๋ธ”์„ ๋ถ„๋ฆฌํ•ด์„œ ์ƒ๊ฐํ•  ์ˆ˜ ์žˆ๋Š” ๋Šฅ๋ ฅ์ด ๊ฐ–์ถฐ์ ธ ์žˆ๋‹ค๋ฉด ์‰ฝ๊ณ  ๋น ๋ฅด๊ฒŒ ํ’€ ์ˆ˜ ์žˆ์„ ๊ฒƒ์ด๋‹ค!

๋ฐ˜์‘ํ˜•