Find Customers Who Bought All Products in MySQL
Intuition\n Describe your first thoughts on how to solve this problem. \nWe need to find customers who bought all the products in the given table.\n\n# Approach
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nWe need to find customers who bought all the products in the given table.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n1. Find the total number of distinct products in the `Product` table.\n\n    > `SELECT` \u2192 This command retrieves data from a database\n\n    > `COUNT()` \u2192 This function returns the number of rows\n\n    > `DISTINCT` \u2192 This statement returns only distinct (different) values\n\n2. Filter the customers who have bought the same count of products as the total distinct products.\n\n    > `HAVING` \u2192 This clause allows filtering of aggregated results produced by `GROUP BY` clause (`WHERE` keyword cannot be used with aggregate functions)\n\n3. Group the results by customer id to identify customers who bought all the products.\n\n    > `GROUP BY` \u2192 This command groups rows that have the same values into summary rows, typically to perform aggregate functions on them\n\n# Complexity\n- Time complexity: $O(n)$\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n`n` is the number of rows in the table. This is because the query processes each row in the table once to find customers who bought all the products.\n\n# Code\n```\nSELECT customer_id \nFROM Customer\nGROUP BY customer_id\nHAVING COUNT(DISTINCT product_key) = (\n    SELECT COUNT(DISTINCT product_key) \n    FROM Product\n);\n```
['MySQL']