cswork / libraryOpen practice workspace

Coding questions · Amazon

Identify Customers Purchasing Every Product in Catalog

The question evaluates a candidate's ability to perform data manipulation in SQL or Python, focusing on aggregation, grouping, and set-coverage concepts (relational division) to determine which customers have purchased every product in a catalog.

X_PURCHASES

+-------------+---------+
| customer_id | pro_key |
+-------------+---------+
| 1           | A       |
| 1           | B       |
| 2           | A       |
+-------------+---------+

PRODUCT_CATALOG_Y

+---------+
| pro_key |
+---------+
| A       |
| B       |
+---------+
Scenario

E-commerce team wants to identify customers who have purchased every product currently in the catalog.

Question

Using SQL (or Python), return the customer_id values who have at least one purchase in X_PURCHASES for every pro_key that appears in PRODUCT_CATALOG_Y.

Hints

Group by customer and compare counts, or use NOT EXISTS to filter missing products.

Customers who purchased all catalog products

You are given two tables: X_PURCHASES, which records product purchases by customers, and PRODUCT_CATALOG_Y, which lists all products currently in the catalog. Using SQL, return the customer_id values for customers who have at least one purchase in X_PURCHASES for every pro_key that appears in PRODUCT_CATALOG_Y.

Table X_PURCHASES

columntypeconstraints
customer_idINTEGER
pro_keyVARCHAR(10)

Sample data:

customer_idpro_key
1A
1B
2A

Table PRODUCT_CATALOG_Y

columntypeconstraints
pro_keyVARCHAR(10)

Sample data:

pro_key
A
B

Expected output

Return one row per customer_id where the customer has purchased all products listed in PRODUCT_CATALOG_Y at least once.

customer_id
1

Hints

  1. Join purchases to the product catalog and group by customer_id.
  2. In the HAVING clause, compare the count of distinct purchased products to the total count of products in PRODUCT_CATALOG_Y.
  3. Alternatively, use NOT EXISTS to filter out customers missing any catalog product.