Skip to main content

LeetCode 1045. Customers Who Bought All Products SQL Solution

Problem

LeetCode SQL Problem

  1. Customers Who Bought All Products

Customer table

customer_idproduct_key
15
26
35
36
16

Product table

product_key
5
6

Solution

Customers Who Bought All Products
SELECT C.customer_id
FROM Customer AS C
GROUP BY C.customer_id
-- Only show ids of customers who bought all the products
HAVING count(DISTINCT C.product_key) = (
-- Count all the products in the Product table
SELECT count(*)
FROM Product
)

Query Output

customer_id
1
3