Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ Have a good contributing!
2. [Medium](./leetcode/medium/)
- [176. Second Highest Salary](./leetcode/medium/176.%20Second%20Highest%20Salary.sql)
- [184. Department Highest Salary](./leetcode/medium/184.%20Department%20Highest%20Salary.sql)
- [1045. Customers Who Bought All Products](./leetcode/medium/1045.%20Customers%20Who%20Bought%20All%20Products.sql)
- [1070. Product Sales Analysis III](./leetcode/medium/1070.%20Product%20Sales%20Analysis%203.sql)
- [1158. Market Analysis 1](./leetcode/medium/1158.%20Market%20Analysis%201.sql)

Expand Down
46 changes: 46 additions & 0 deletions leetcode/medium/1045. Customers Who Bought All Products.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
Question 1045. Customers Who Bought All Products
Link: https://leetcode.com/problems/customers-who-bought-all-products/description/

Table: Customer

+-------------+---------+
| Column Name | Type |
+-------------+---------+
| customer_id | int |
| product_key | int |
+-------------+---------+
This table may contain duplicates rows.
customer_id is not NULL.
product_key is a foreign key (reference column) to Product table.


Table: Product

+-------------+---------+
| Column Name | Type |
+-------------+---------+
| product_key | int |
+-------------+---------+
product_key is the primary key (column with unique values) for this table.


Write a solution to report the customer ids from the Customer table that bought all the products in the Product table.

Return the result table in any order.
*/

WITH Customer_products AS (
SELECT
customer_id,
COUNT(DISTINCT product_key) AS c
FROM Customer
GROUP BY customer_id
)

SELECT customer_id
FROM Customer_products
WHERE c = (
SELECT COUNT(1)
FROM Product
)