Simple Query ✅ | With Proper Explanation 🤝
IntuitionTo solve this problem, we need to determine which customers have bought all distinct products listed in the Product table.•	First, count the number of
# Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
To solve this problem, we need to determine which customers have bought all distinct products listed in the Product table.

•	First, count the number of distinct products available.

•	Then, for each customer_id, count the number of distinct products they have purchased.

•	If both counts match, that means the customer has purchased all products.

# Approach
<!-- Describe your approach to solving the problem. -->
1.	Count the total number of distinct products available in the Product table

    **SELECT COUNT(DISTINCT product_key) FROM Product**

    •	This gives the total number of unique products.

2.	Group purchases by customer_id and count how many distinct products each customer has bought.

	•	If a customer’s distinct product count equals the total count from step 1, that customer qualifies.

3.	Use HAVING to filter customers who have purchased all products

        SELECT customer_id 
        FROM Customer 
        GROUP BY customer_id 
        HAVING COUNT(DISTINCT product_key) = (SELECT COUNT(DISTINCT product_key) FROM Product)


	•	GROUP BY customer_id: Groups the purchases by customer.

	•	COUNT(DISTINCT product_key): Counts the number of different products each customer has purchased.

	•	HAVING: Filters out customers who haven’t bought all products.
# Complexity
- Time complexity:
<!-- Add your time complexity here, e.g. $$O(n)$$ -->

- Space complexity:
<!-- Add your space complexity here, e.g. $$O(n)$$ -->
If you found my solution helpful, I’d really appreciate it if you could give it an upvote! 🙌 Your support keeps me motivated to share more simple and efficient solutions. Thank you! 😊 

# Code
```postgresql []
-- Write your PostgreSQL query statement below
SELECT customer_id 
FROM Customer 
GROUP BY customer_id 
HAVING COUNT(DISTINCT product_key) = (SELECT COUNT(DISTINCT product_key) FROM Product)
```
['PostgreSQL']