Find customers who bought from every category
SQL
Hard
7 views
Problem Description
Customers who have at least one purchase in each product category.
Sample Test Case
Input:
Customers, Orders, OrderItems, Products
Constraints
Use division pattern with counts
Official Solution
SELECT c.customer_id, c.name FROM Customers c WHERE (SELECT COUNT(DISTINCT p.category) FROM Orders o JOIN OrderItems oi ON oi.order_id = o.order_id JOIN Products p ON p.product_id = oi.product_id WHERE o.customer_id = c.customer_id) = (SELECT COUNT(DISTINCT category) FROM Products);
Solutions (0)
No solutions submitted yet. Be the first!
No comments yet. Start the discussion!