File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ 1045 . Customers Who Bought All Products
2+
3+ Table: Customer
4+
5+ + -- -----------+---------+
6+ | Column Name | Type |
7+ + -- -----------+---------+
8+ | customer_id | int |
9+ | product_key | int |
10+ + -- -----------+---------+
11+ This table may contain duplicates rows.
12+ customer_id is not NULL .
13+ product_key is a foreign key (reference column) to Product table.
14+
15+
16+ Table: Product
17+
18+ + -- -----------+---------+
19+ | Column Name | Type |
20+ + -- -----------+---------+
21+ | product_key | int |
22+ + -- -----------+---------+
23+ product_key is the primary key (column with unique values ) for this table.
24+
25+
26+ Write a solution to report the customer ids from the Customer table that bought all the products in the Product table.
27+
28+ Return the result table in any order.
29+
30+ The result format is in the following example.
31+
32+
33+
34+ Example 1 :
35+
36+ Input:
37+ Customer table:
38+ + -- -----------+-------------+
39+ | customer_id | product_key |
40+ + -- -----------+-------------+
41+ | 1 | 5 |
42+ | 2 | 6 |
43+ | 3 | 5 |
44+ | 3 | 6 |
45+ | 1 | 6 |
46+ + -- -----------+-------------+
47+ Product table:
48+ + -- -----------+
49+ | product_key |
50+ + -- -----------+
51+ | 5 |
52+ | 6 |
53+ + -- -----------+
54+ Output:
55+ + -- -----------+
56+ | customer_id |
57+ + -- -----------+
58+ | 1 |
59+ | 3 |
60+ + -- -----------+
61+ Explanation:
62+ The customers who bought all the products (5 and 6 ) are customers with IDs 1 and 3 .
63+
64+
65+
66+ # Write your MySQL query statement below
67+ SELECT customer_id FROM Customer GROUP BY customer_id
68+ HAVING COUNT (distinct product_key) = (SELECT COUNT (product_key) FROM Product);
You can’t perform that action at this time.
0 commit comments