|
| 1 | +Here’s the updated README.md, including **Pandas and Window SQL solutions** along with the original **MySQL solution**. |
| 2 | + |
| 3 | +--- |
| 4 | + |
| 5 | +# **1045. Customers Who Bought All Products** |
| 6 | + |
| 7 | +## **Problem Statement** |
| 8 | +You are given two tables: |
| 9 | +- `Customer` (contains `customer_id` and `product_key`) |
| 10 | +- `Product` (contains all available `product_key`s) |
| 11 | + |
| 12 | +Each `product_key` in `Customer` is a **foreign key** referring to the `Product` table. |
| 13 | + |
| 14 | +### **Customer Table** |
| 15 | +``` |
| 16 | ++-------------+-------------+ |
| 17 | +| Column Name | Type | |
| 18 | ++-------------+-------------+ |
| 19 | +| customer_id | int | |
| 20 | +| product_key | int | |
| 21 | ++-------------+-------------+ |
| 22 | +``` |
| 23 | +- The table may contain **duplicate rows**. |
| 24 | +- `customer_id` is **not NULL**. |
| 25 | +- `product_key` refers to the `Product` table. |
| 26 | + |
| 27 | +### **Product Table** |
| 28 | +``` |
| 29 | ++-------------+ |
| 30 | +| product_key | |
| 31 | ++-------------+ |
| 32 | +| int | |
| 33 | ++-------------+ |
| 34 | +``` |
| 35 | +- `product_key` is the **primary key** (unique values) of this table. |
| 36 | + |
| 37 | +### **Task:** |
| 38 | +Find **all customer IDs** who bought **every product** listed in the `Product` table. |
| 39 | + |
| 40 | +--- |
| 41 | + |
| 42 | +## **Example 1:** |
| 43 | + |
| 44 | +### **Input:** |
| 45 | +**Customer Table** |
| 46 | +``` |
| 47 | ++-------------+-------------+ |
| 48 | +| customer_id | product_key | |
| 49 | ++-------------+-------------+ |
| 50 | +| 1 | 5 | |
| 51 | +| 2 | 6 | |
| 52 | +| 3 | 5 | |
| 53 | +| 3 | 6 | |
| 54 | +| 1 | 6 | |
| 55 | ++-------------+-------------+ |
| 56 | +``` |
| 57 | + |
| 58 | +**Product Table** |
| 59 | +``` |
| 60 | ++-------------+ |
| 61 | +| product_key | |
| 62 | ++-------------+ |
| 63 | +| 5 | |
| 64 | +| 6 | |
| 65 | ++-------------+ |
| 66 | +``` |
| 67 | + |
| 68 | +### **Output:** |
| 69 | +``` |
| 70 | ++-------------+ |
| 71 | +| customer_id | |
| 72 | ++-------------+ |
| 73 | +| 1 | |
| 74 | +| 3 | |
| 75 | ++-------------+ |
| 76 | +``` |
| 77 | + |
| 78 | +### **Explanation:** |
| 79 | +- There are **two products** (5 and 6). |
| 80 | +- Customers who bought **both** products: |
| 81 | + - **Customer 1**: Bought `5, 6` ✅ |
| 82 | + - **Customer 2**: Bought `6` ❌ (missing `5`) |
| 83 | + - **Customer 3**: Bought `5, 6` ✅ |
| 84 | +- So, **customers 1 and 3** are returned. |
| 85 | + |
| 86 | +--- |
| 87 | + |
| 88 | +## **SQL Solutions** |
| 89 | + |
| 90 | +### **1️⃣ Standard MySQL Solution** |
| 91 | +```sql |
| 92 | +SELECT customer_id |
| 93 | +FROM Customer |
| 94 | +GROUP BY customer_id |
| 95 | +HAVING COUNT(DISTINCT product_key) = (SELECT COUNT(product_key) FROM Product); |
| 96 | +``` |
| 97 | +#### **Explanation:** |
| 98 | +1. **GROUP BY `customer_id`** → Group purchases per customer. |
| 99 | +2. **COUNT(DISTINCT product_key)** → Count unique products each customer bought. |
| 100 | +3. **Compare with total products:** |
| 101 | + - `(SELECT COUNT(product_key) FROM Product)` counts all available products. |
| 102 | + - Only customers with `COUNT(DISTINCT product_key) = total products` are included. |
| 103 | +4. **HAVING** ensures we return only those who bought **all products**. |
| 104 | + |
| 105 | +--- |
| 106 | + |
| 107 | +### **2️⃣ Window Function (SQL) Solution** |
| 108 | +```sql |
| 109 | +WITH product_count AS ( |
| 110 | + SELECT COUNT(*) AS total_products FROM Product |
| 111 | +), |
| 112 | +customer_purchase AS ( |
| 113 | + SELECT customer_id, COUNT(DISTINCT product_key) AS purchased_count |
| 114 | + FROM Customer |
| 115 | + GROUP BY customer_id |
| 116 | +) |
| 117 | +SELECT customer_id |
| 118 | +FROM customer_purchase, product_count |
| 119 | +WHERE customer_purchase.purchased_count = product_count.total_products; |
| 120 | +``` |
| 121 | +#### **Explanation:** |
| 122 | +1. **CTE `product_count`** → Stores total number of products in `Product` table. |
| 123 | +2. **CTE `customer_purchase`** → Groups purchases per customer and counts distinct products. |
| 124 | +3. **Final SELECT query** → Compares each customer's purchase count with `total_products` and returns only those who match. |
| 125 | + |
| 126 | +--- |
| 127 | + |
| 128 | +## **Pandas Solution (Python)** |
| 129 | +```python |
| 130 | +import pandas as pd |
| 131 | + |
| 132 | +# Sample data |
| 133 | +customer_data = {'customer_id': [1, 2, 3, 3, 1], |
| 134 | + 'product_key': [5, 6, 5, 6, 6]} |
| 135 | +product_data = {'product_key': [5, 6]} |
| 136 | + |
| 137 | +# Create DataFrames |
| 138 | +customer_df = pd.DataFrame(customer_data) |
| 139 | +product_df = pd.DataFrame(product_data) |
| 140 | + |
| 141 | +# Get the total number of products |
| 142 | +total_products = product_df['product_key'].nunique() |
| 143 | + |
| 144 | +# Count distinct products per customer |
| 145 | +customer_purchase = customer_df.groupby('customer_id')['product_key'].nunique() |
| 146 | + |
| 147 | +# Filter customers who bought all products |
| 148 | +result = customer_purchase[customer_purchase == total_products].reset_index() |
| 149 | + |
| 150 | +print(result) |
| 151 | +``` |
| 152 | + |
| 153 | +### **Explanation:** |
| 154 | +1. **Create DataFrames** → Convert customer and product tables into Pandas DataFrames. |
| 155 | +2. **Get total unique products** → `product_df['product_key'].nunique()` |
| 156 | +3. **Count distinct products per customer** → `.groupby('customer_id')['product_key'].nunique()` |
| 157 | +4. **Filter customers who match total products** → Customers with `purchased_count == total_products` |
| 158 | +5. **Return final result**. |
| 159 | + |
| 160 | +--- |
| 161 | + |
| 162 | +## **File Structure** |
| 163 | +``` |
| 164 | +LeetCode1045/ |
| 165 | +├── problem_statement.md # Contains the problem description and constraints. |
| 166 | +├── sql_solution.sql # Contains the SQL solutions (Standard + Window Functions). |
| 167 | +├── pandas_solution.py # Contains the Pandas solution. |
| 168 | +├── README.md # Overview of the problem and available solutions. |
| 169 | +``` |
| 170 | + |
| 171 | +--- |
| 172 | + |
| 173 | +## **Useful Links** |
| 174 | +- [LeetCode Problem 1045](https://leetcode.com/problems/customers-who-bought-all-products/) |
| 175 | +- [SQL GROUP BY Documentation](https://www.w3schools.com/sql/sql_groupby.asp) |
| 176 | +- [SQL HAVING Clause](https://www.w3schools.com/sql/sql_having.asp) |
| 177 | +- [Pandas GroupBy Documentation](https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.groupby.html) |
| 178 | + |
| 179 | +---ture & Useful Links** |
| 180 | + |
| 181 | +🚀 **Now it's a complete guide!** 🚀 |
0 commit comments