SQL Challenge: Find Customers Who Bought All Products

🗓️ SQL Challenge Day #28: Customers Who Bought All Products 🔹 Find customers who purchased every single product! 🛒 🔹 Problem:   Report customers who bought ALL products:   ✅ Handle duplicate purchases gracefully   ✅ Match total distinct products  🔹 Solution:  SELECT customer_id   FROM Customer   GROUP BY customer_id   HAVING COUNT(DISTINCT product_key) = (SELECT COUNT(*) FROM Product);  ✅ Result: Accepted  💡 Key Takeaway:   **COUNT(DISTINCT) + scalar subquery** is the gold standard for "bought all" problems!   ⚠️ Why DISTINCT? Customer might buy same product multiple times – we only care about unique products owned.   ✅ Subquery `(SELECT COUNT(*) FROM Product)` gives total product universe – clean and maintainable.  👇 Your turn:   Have you used this pattern for "completed all courses" or "visited all locations" type problems? Share your use case!  #SQL #LeetCode #DataEngineering #ProblemSolving #Coding #LearningInPublic #Database #DataAnalytics

  • graphical user interface, text, application, chat or text message

To view or add a comment, sign in

Explore content categories