Easy and Simple Solution | Beginner-friendly🚀
CodeIf you found this solution helpful, please give it an upvote! 🌟✨ It really motivates me to share more solutions like this. 😊👍
Happy coding! 💻🐱‍🏍
IntuitionWe
# Code
```mysql []
# Write your MySQL query statement below
select customer_id
from Customer
group by customer_id
having count(distinct product_key) = (select count(product_key) from Product);
```
**If you found this solution helpful, please give it an upvote! 🌟✨ It really motivates me to share more solutions like this. 😊👍
Happy coding! 💻🐱‍🏍**
<img src="https://assets.leetcode.com/users/images/6e8c623a-1111-4d21-9a5b-54e00e7feabb_1738093236.165277.jpeg" width=250px>
# Intuition
We need to find customers who have purchased **all available products** listed in the `Product` table. This means:
1. Counting the number of **distinct products** each customer has purchased.
2. Comparing it with the **total number of products** in the `Product` table.
3. Returning only those `customer_id`s who have bought every product at least once.

# Approach
1. **Count the total number of products**  
   - Use `SELECT COUNT(product_key) FROM Product` to get the total product count.

2. **Count distinct products purchased by each customer**  
   - Use `GROUP BY customer_id` to process each customer separately.
   - Use `HAVING COUNT(DISTINCT product_key) = (total products)` to filter customers who bought every product.

3. **Return only the qualifying `customer_id`s**  
   - The `HAVING` clause ensures we include only customers who have purchased all products.

# Complexity Analysis
- **Counting total products (`SELECT COUNT(product_key) FROM Product`)**  
  - Runs in **O(P)**, where `P` is the number of products.

- **Grouping and counting distinct purchases (`GROUP BY customer_id HAVING COUNT(DISTINCT product_key)`)**  
  - Sorting and grouping operations run in **O(N log C)**, where `C` is the number of unique customers.
  - Counting distinct products per customer runs in **O(N log K)**, where `K` is the number of unique products per customer.

### **Overall Complexity:**
- The dominant factor is **grouping and distinct counting**, leading to an approximate **O(N log C + P) ≈ O(N log N)** complexity in the worst case.
['MySQL']