๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
SQL/LeetCode

[SQL50] #29. Customers Who Bought All Products

by ์ด๋ฎด 2024. 1. 9.
728x90

๐Ÿ’ป ๋ฌธ์ œ ์ฃผ์†Œ : Customers Who Bought All Products - LeetCode

 

Customers Who Bought All Products - LeetCode

Can you solve this real interview question? Customers Who Bought All Products - Table: Customer +-------------+---------+ | Column Name | Type | +-------------+---------+ | customer_id | int | | product_key | int | +-------------+---------+ This table may

leetcode.com

 

๐Ÿ’ป ๋ฌธ์ œ

Write a solution to report the customer ids from the Customer table that bought all the products in the Product table. Return the result table in any order.

Input๊ณผ Output ์˜ˆ์‹œ

 

๐Ÿ’ป ์ฝ”๋“œ ๋ฏธ๋ฆฌ๋ณด๊ธฐ

๋”๋ณด๊ธฐ
select customer_id
from Customer
group by customer_id
having count(distinct product_key) = (select count(product_key) from Product)

 

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

๊ฐœ์ธ์ ์œผ๋กœ ์ด๋ฒˆ ๋ฌธ์ œ๋Š” ๋ฌธ์ œ๋ฅผ ํ•ด์„ํ•˜๊ธฐ๋ณด๋‹ค๋Š” Example์„ ๋ณด๊ณ  ์›๋ฆฌ๋ฅผ ํŒŒ์•…ํ•˜๋Š” ๊ฒŒ ๋” ์‰ฝ๋‹ค ๋А๊ปด์กŒ๋‹ค.

 

์šฐ์„  ์šฐ๋ฆฌ๋Š” ์•„๋ž˜ ๋‚ด์šฉ์„ ๊ตฌํ•ด์•ผ ํ•œ๋‹ค.

1๏ธโƒฃ Product table์— ์žˆ๋Š” ๋ชจ๋“  Products
2๏ธโƒฃ 1๏ธโƒฃ์„ ๊ตฌ๋งคํ•œ ๋ชจ๋“  Customer๋“ค์˜ ID

 

1๏ธโƒฃ Product table์— ์žˆ๋Š” ๋ชจ๋“  Products

๋ฌธ์ œ์—์„œ ๊ตฌํ•˜๊ณ ์ž ํ•˜๋Š” ํ•ต์‹ฌ์€ '๋ชจ๋“  Products๋ฅผ ๊ตฌ๋งคํ•œ ๊ณ ๊ฐ๋“ค์˜ ID ๋ฆฌ์ŠคํŠธ'์ด๊ธฐ ๋•Œ๋ฌธ์— ๊ฐ ๊ณ ๊ฐ๋“ค์ด ๊ตฌ๋งคํ•œ ์ƒํ’ˆ์ด Product ํ…Œ์ด๋ธ” ๋ชจ๋“  ๋ ˆ์ฝ”๋“œ๋ฅผ ์ถฉ์กฑํ•˜๋Š”๊ฐ€๋ฅผ ๋ด์•ผ ํ•œ๋‹ค. ์ฆ‰, Product ํ…Œ์ด๋ธ” ๋‚ด product_key๋Š” ๊ณ ๊ฐ๋“ค์ด ์ƒํ’ˆ์„ ์ƒ€๋Š”๊ฐ€ ์‚ฌ์ง€ ์•Š์•˜๋Š”๊ฐ€์— ๋Œ€ํ•œ '์กฐ๊ฑด'์ด ๋˜๋ฏ€๋กœ where ์ ˆ์— ๋“ค์–ด๊ฐ€๊ฒŒ ๋œ๋‹ค.

select 
from 
where (product_key) in Product table

 

2๏ธโƒฃ 1๏ธโƒฃ์„ ๊ตฌ๋งคํ•œ ๋ชจ๋“  Customer๋“ค์˜ ID

๊ทธ๋ฆฌ๊ณ  ์ด์ œ 2๏ธโƒฃ์™€ ์œ„ Example Input&Output์„ ๊ฐ™์ด ์‚ดํŽด๋ณด๋„๋ก ํ•˜์ž.

customer_id product_key
1 5
2 6
3 5
3 6
1 6

 

Product_key๊ฐ€ 5, 6์ธ ์ƒํ’ˆ๋“ค ์ค‘ ์ด ๋ชจ๋“  ์ƒํ’ˆ์„ ๊ตฌ๋งคํ•œ ๊ณ ๊ฐ์€ 1, 3, ์ด ๋‘˜์ด๋‹ค. ์—ฌ๊ธฐ์—์„œ ํ•œ ๊ฐ€์ง€ ์ฃผ๋ชฉํ•ด์•ผ ํ•  ์ ์€ ๊ฐ ๊ณ ๊ฐ๋ณ„๋กœ ํ•œ ๊ฐ€์ง€ ์ƒํ’ˆ์„ ํ•œ ๋ฒˆ ์ƒ€๊ฑด ๋‘ ๋ฒˆ ์ƒ€๊ฑด ๊ฒฐ๊ตญ ๋ชจ๋“  ์ƒํ’ˆ์„ ๊ตฌ๋งคํ•œ ์†๋‹˜์˜ ๊ฒฝ์šฐ, ์ด ๊ตฌ๋งคํ•œ (๊ณ ์œ ) ์ƒํ’ˆ์˜ ์ˆ˜๋Š” Product ํ…Œ์ด๋ธ” ๋‚ด ๋ชจ๋“  product_key ์ˆ˜์™€ ๊ฐ™๋‹ค๋Š” ๊ฒƒ์ด๋‹ค. ์ด๊ฒŒ ๋ฐ”๋กœ ์ด๋ฒˆ ๋ฌธ์ œ์˜ ํ•ต์‹ฌ์ด๋‹ค.

 

์ฆ‰, ๋‹ค์‹œ ๋งํ•ด ๊ฐ ๊ณ ๊ฐ๋ณ„๋กœ group by function์„ ํ†ตํ•ด ์ง‘๊ณ„ํ•˜๊ณ , ์ดํ›„ ๊ฐ ๊ณ ๊ฐ์ด ๊ตฌ๋งคํ•œ ์ด ์ƒํ’ˆ์˜ ์ˆ˜๊ฐ€ Product ํ…Œ์ด๋ธ”์˜ ๋ชจ๋“  product_key ์ˆ˜์™€ ๊ฐ™๋‹ค๋ฉด ๊ทธ ๊ณ ๊ฐ์€ ๋ชจ๋“  ์ƒํ’ˆ์„ ๊ตฌ๋งคํ–ˆ๋‹ค๊ณ  ๋ณผ ์ˆ˜ ์žˆ๋Š” ๊ฒƒ์ด๋‹ค. ์ด๋•Œ ์šฐ๋ฆฌ๋Š” group by function์„ ์“ธ ๊ฒƒ์ด๊ธฐ ๋•Œ๋ฌธ์— 1๏ธโƒฃ์—์„œ ์–˜๊ธฐํ•œ '์กฐ๊ฑด'์€ where์ด ์•„๋‹Œ having ์ ˆ๋กœ ๋“ค์–ด๊ฐ€๊ฒŒ ๋œ๋‹ค.

select customer_id
from Customer
group by customer_id
having count(distinct product_key) = (select count(product_key) from Product)

 

728x90
๋ฐ˜์‘ํ˜•

'SQL > LeetCode' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

[SQL50] #28. Biggest Single Number  (1) 2024.01.07
[SQL50] #27. Find Followers Count  (2) 2024.01.06
[SQL50] #26. Classes More Than 5 Students  (1) 2024.01.05
[LeetCode][SQL50] #25. Product Sales Analysis III  (1) 2024.01.04
[LeetCode][SQL50] #24. User Activity for the Past 30 Days I  (1) 2024.01.04