Customers Who Bought All Products SQL MySQL leetcode Solution using having

Опубликовано: 17 Ноябрь 2025
на канале: CodeWis Technologies by Nuhman Paramban
162
0

Challenge: Finding Customers Who Bought Everything

Difficulty: Medium

Data:

Two tables store information about customer purchases:

Customer (Primary Key: customer_id):
customer_id (int): Unique identifier for a customer.
product_key (int): Foreign key referencing the Product table (product purchased).
Product (Primary Key: product_key):
product_key (int): Unique identifier for a product.
Objective:

Identify customer IDs from the Customer table who have purchased all products listed in the Product table.

Output:

A table with a single column:

customer_id (int): Unique identifier for a customer who bought all products (same as input table).
Explanation:

We need to find customer_id values in the Customer table where the distinct count of product_key matches the total number of unique products in the Product table. This indicates customers who purchased every product.

SQL MYSQL leetcode Solution with HAVING and DISTINCT COUNT