File tree Expand file tree Collapse file tree 2 files changed +44
-0
lines changed
Expand file tree Collapse file tree 2 files changed +44
-0
lines changed Original file line number Diff line number Diff line change @@ -46,6 +46,7 @@ Have a good contributing!
46462 . [ Medium] ( ./leetcode/medium/ )
4747 - [ 176. Second Highest Salary] ( ./leetcode/medium/176.%20Second%20Highest%20Salary.sql )
4848 - [ 184. Department Highest Salary] ( ./leetcode/medium/184.%20Department%20Highest%20Salary.sql )
49+ - [ 1045. Customers Who Bought All Products] ( ./leetcode/medium/1045.%20Customers%20Who%20Bought%20All%20Products.sql )
4950 - [ 1070. Product Sales Analysis III] ( ./leetcode/medium/1070.%20Product%20Sales%20Analysis%203.sql )
5051 - [ 1158. Market Analysis 1] ( ./leetcode/medium/1158.%20Market%20Analysis%201.sql )
5152
Original file line number Diff line number Diff line change 1+ /*
2+ Question 1045. Customers Who Bought All Products
3+ Link: https://leetcode.com/problems/customers-who-bought-all-products/description/
4+
5+ Table: Customer
6+
7+ +-------------+---------+
8+ | Column Name | Type |
9+ +-------------+---------+
10+ | customer_id | int |
11+ | product_key | int |
12+ +-------------+---------+
13+ This table may contain duplicates rows.
14+ customer_id is not NULL.
15+ product_key is a foreign key (reference column) to Product table.
16+
17+
18+ Table: Product
19+
20+ +-------------+---------+
21+ | Column Name | Type |
22+ +-------------+---------+
23+ | product_key | int |
24+ +-------------+---------+
25+ product_key is the primary key (column with unique values) for this table.
26+
27+
28+ Write a solution to report the customer ids from the Customer table that bought all the products in the Product table.
29+
30+ Return the result table in any order.
31+ */
32+
33+ WITH Customer_products AS (
34+ SELECT customer_id, COUNT (DISTINCT product_key) as c
35+ FROM Customer
36+ GROUP BY customer_id
37+ )
38+ SELECT customer_id
39+ FROM Customer_products
40+ WHERE c = (
41+ SELECT COUNT (1 )
42+ FROM Product
43+ )
You can’t perform that action at this time.
0 commit comments