Simple & Best Solution, Easy To Understand
IntuitionThe problem is to identify which customers purchased all the products listed in the Product table. A customer is considered to have bought all products
# Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
The problem is to identify which customers purchased all the products listed in the Product table. A customer is considered to have bought all products if, for that customer, the number of distinct products they bought matches the total number of products available in the Product table.

The goal is achieved by:
- Counting how many unique products each customer has bought.
- Comparing that count to the total number of products in the Product table.
- Returning the customer IDs where the counts match.

# Approach
<!-- Describe your approach to solving the problem. -->
- Count Products per Customer:
Group the Customer table by customer_id and count the distinct product_key each customer purchased.

- Get Total Products:
Use a subquery to count the total number of products in the Product table.

- Filter Customers:
Use a HAVING clause to compare the count of distinct products per customer with the total product count. Only customers who bought all products are selected.

- Return Results:
Output the customer_id of customers meeting the condition.

# Complexity
- Time complexity: O(n+m)
<!-- Add your time complexity here, e.g. $$O(n)$$ -->

- Space complexity: O(k)
<!-- Add your space complexity here, e.g. $$O(n)$$ -->

# Code
```mysql []
# Write your MySQL query statement below
SELECT customer_id
FROM Customer
GROUP BY 1
HAVING COUNT(DISTINCT product_key) = (
    SELECT COUNT(*) FROM Product
  );
```
['Math', 'Database', 'MySQL']