SQL/문제풀이

[LeetCode] customers-who-bought-all-products

응엉잉 2024. 3. 4. 13:48

https://leetcode.com/problems/customers-who-bought-all-products/description/?envType=study-plan-v2&envId=top-sql-50

 

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

-- 유저별 구매 상품의 고유 갯수 = 상품table에 등록된 상품 수
SELECT customer_id
FROM customer
GROUP BY customer_id
HAVING COUNT(DISTINCT product_key) = (
    SELECT COUNT(DISTINCT product_key)
    FROM product
)

GROUP BY 이후의 조건절은 HAVING

GROUP BY 사용시 꼭 SELECT문에 집계함수를 쓰지 않아도됨. HAVING절에서 사용 가능 !