๐ŸœSQL๋ฌธ์ œ ํ’€๊ธฐ1

0

๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค

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

  • ๋ฌธ์ œ๋ฅผ ํ’€๊ธฐ์œ„ํ•ด์„œ dataset์€ instacart์—์„œ ๊ฐ€์ ธ์™”์Šต๋‹ˆ๋‹ค
  • ํ…Œ์ด๋ธ”์˜ ๋ชฉ๋ก

  • ํ…Œ์ด๋ธ”๋“ค์˜ ์†์„ฑ

1. ์ „์ฒด ์ฃผ๋ฌธ ๊ฑด์ˆ˜

SELECT COUNT(order_id) FROM orders;

->์ฃผ๋ฌธ๊ฑด์ˆ˜๊ฐ€ ๊ณ ๊ฐ์˜ ์ค‘๋ณต์ด ์žˆ์„์ˆ˜ ์žˆ์ง€๋งŒ ์ „์ฒด ์ฃผ๋ฌธ ๊ฑด์ˆ˜์ด๊ธฐ ๋•Œ๋ฌธ์— ๊ณ ๋ คํ•˜์ง€ ์•Š๊ฒ ์Šต๋‹ˆ๋‹ค


2. ๊ตฌ๋งค์ž์˜ ์ˆ˜

SELECT COUNT(DISTINCT(user_id)) FROM orders;

->1๋ฒˆ ๋ฌธ์ œ์—์„œ ๊ณ ๋ คํ•˜์ง€ ์•Š์•˜๋˜ ๊ฒƒ์„ ๊ณ ๋ คํ•˜์—ฌ ์ค‘๋ณต์„ ์ œ๊ฑฐํ›„ ์ˆ˜๋ฅผ ์นด์šดํŠธ ํ•˜์˜€์Šต๋‹ˆ๋‹ค.


3. ์ƒํ’ˆ๋ณ„ ์ฃผ๋ฌธ ๊ฑด์ˆ˜

ON์„ ์‚ฌ์šฉํ•ด์„œ join

SELECT B.product_name, COUNT(DISTINCT A.order_id) F
FROM order_products__prior A 
LEFT JOIN 
products B 
ON A.product_id = B.product_id
GROUP BY 1;

USING์„ ์‚ฌ์šฉํ•ด์„œ join

SELECT B.product_name, COUNT(DISTINCT A.order_id) F
FROM order_products__prior A 
LEFT JOIN 
products B 
USING (product_id)
GROUP BY 1;
  • USING๊ณผ JOIN์˜ ์ฐจ์ด๋Š” ๋ฌด์—‡์ผ๊นŒ์š”?

    ๋‘ ํ…Œ์ด๋ธ”๊ฐ„์˜ ํ•„๋“œ์˜ ์ด๋ฆ„์ด ๊ฐ™์„๋•Œ ์‚ฌ์šฉ์ด ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.
    ๋”ฐ๋ผ์„œ ๋‘ ํ…Œ์ด๋ธ”์˜ product_id๊ฐ€ ๊ฐ™๊ธฐ ๋•Œ๋ฌธ์— USING์„ ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.

4. ์นดํŠธ์— ๊ฐ€์žฅ ๋จผ์ € ๋„ฃ๋Š” ์ƒํ’ˆ 10๊ฐœ

SELECT * FROM 
(SELECT *, ROW_NUMBER() OVER(ORDER BY F_1st DESC) rnk
FROM 
(SELECT product_id, SUM(case when add_to_cart_order = 1 then 1 ELSE 0 END) F_1st
FROM order_products__prior 
GROUP BY product_id) A LIMIT 10) B
LEFT JOIN products C
ON B.product_id = C.product_id
ORDER BY rnk ;
  • ์„ค๋ช…
    ์œ„์˜ ์ฟผ๋ฆฌ๋Š” ํฌ๊ฒŒ 3๊ฐœ๋กœ ๋‚˜๋ˆŒ์ˆ˜ ์žˆ๋‹ค๊ณ  ์ƒ๊ฐํ•ฉ๋‹ˆ๋‹ค.
SELECT * FROM 
(SELECT *, ROW_NUMBER() OVER(ORDER BY F_1st DESC) rnk
FROM 
(SELECT product_id, SUM(case when add_to_cart_order = 1 then 1 ELSE 0 END) F_1st
FROM order_products__prior 
GROUP BY product_id) A LIMIT 10) B

์œ„์˜ ์ฟผ๋ฆฌ๋Š” product_id ์—ด๊ณผ add_to_cart_order ์—ด์„ ์„ ํƒํ•˜๊ณ  product_id๋ฅผ ๊ธฐ์ค€์œผ๋กœ ์ƒ์œ„ 10๊ฐœ๋งŒ ๊ฐ€์ง€๊ณ  ์˜ค๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.

LEFT JOIN products C
ON B.product_id = C.product_id

1๋ฒˆ์„ ํ†ตํ•ด์„œ ๋‚˜์˜จ ํ…Œ์ด๋ธ”๊ณผ productsํ…Œ์ด๋ธ”์„ product_id๋ฅผ ํ†ตํ•ด์„œ JOIN์„ ํ•ฉ๋‹ˆ๋‹ค.

ORDER BY rnk 

์ •๋ ฌ์€ rnk๊ธฐ์ค€์œผ๋กœ ํ•ฉ๋‹ˆ๋‹ค.

  • ROW_NUMBER()

    ๊ทธ๋ฃน๋ณ„๋กœ ํ–‰ ๋ฒˆํ˜ธ๋ฅผ ํ• ๋‹นํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ๋˜๋Š” ์ฐฝ ํ•จ์ˆ˜์ž…๋‹ˆ๋‹ค. ORDER BY ์ ˆ์„ ์‚ฌ์šฉํ•˜์—ฌ ํ–‰์„ ์ •๋ ฌํ•œ ๋‹ค์Œ ๊ฐ ํ–‰์— ๊ณ ์œ ํ•œ ๋ฒˆํ˜ธ๋ฅผ ํ• ๋‹นํ•ฉ๋‹ˆ๋‹ค.

5. ์‹œ๊ฐ„๋ณ„ ์ฃผ๋ฌธ ๊ฑด์ˆ˜

SELECT order_hour_of_day, COUNT(DISTINCT order_id) F
FROM orders 
GROUP BY order_hour_of_day
ORDER BY F DESC;

orders ํ…Œ์ด๋ธ”์—์„œ order_hour_of_day๋ฅผ ๊ทธ๋ฃน์œผ๋กœ ํ•˜๊ณ 
order_id์—์„œ ์ค‘๋ณต์„ ์ œ๊ฑฐํ•œ ์ˆ˜๋ฅผ ๋‚ด๋ฆผ์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌ ํ•ฉ๋‹ˆ๋‹ค.

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