Skip to content

Commit 6d7fd10

Browse files
committed
task: 1045
1 parent 8e2d2af commit 6d7fd10

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ Have a good contributing!
4646
2. [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

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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+
)

0 commit comments

Comments
 (0)