Interview Questions/SQL/Customers Who Bought All Products

Customers Who Bought All Products

Preview mode. Log in to edit, run, submit, and save progress.

Medium

Description

You have two tables: Customer and Product. Each row in Customer records a purchase of a product by a customer. Write a SQL query to report the IDs of all customers who have bought all the products listed in the Product table. Return the result in any order.

Database Schema

Customer

Column NameTypeDescription
customer_idINTCustomer identifier (not unique)
product_keyINTFK to Product table

Product

Column NameTypeDescription
product_keyINTPrimary key

Example

Customer

customer_idproduct_key
15
26
35
36
16

Product

product_key
5
6

Output

customer_id
1
3

Explanation:

Customers 1 and 3 both purchased products 5 and 6 - all available products. Customer 2 only bought product 6.

Approach hint

Start with a simple approach, explain the trade-off, then move toward a cleaner or more scalable solution.

Common mistake

Skipping assumptions, edge cases, or trade-offs can make an otherwise good answer feel incomplete.

SQL Editor
Loading...

Customer

customer_idproduct_key
15
26
35
36
16

Product

product_key
5
6

Output

customer_id
1
3