← Amazon Interview Insights

Amazon·Software Engineer·Technical Phone Screen·Intermediate

Intermediate
Apr 2026

Summary

Amazon BI Engineer interview with a SQL question that looks easy until you actually think about it. The relational division problem is a classic but I hadn't practiced it in a while and it showed.

Questions Asked (1)

Q1

Given a purchases table and a product catalog table, write a SQL query to find all customers who have purchased every product in the catalog.

Algorithms & Data StructuresData Modeling
Author's notes

I went straight for a GROUP BY and COUNT approach, comparing the number of distinct products each customer bought against the total rows in the catalog table.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Use a GROUP BY with HAVING COUNT(DISTINCT product_id) equal to the total number of products in the catalog. This assumes each customer-product pair is unique in the purchases table; if not, use COUNT(DISTINCT) to avoid duplicates. Alternatively, use a NOT EXISTS subquery to find customers who have no missing products.

Pro tip: Clarify whether the purchases table can have duplicate entries for the same customer and product; if so, use COUNT(DISTINCT product_id) to ensure accurate counting. Also, consider performance implications: the GROUP BY approach is often more efficient than multiple subqueries, but indexing on customer_id and product_id can further optimize.

1. Understand the schema and requirements

Identify the relevant columns: customer_id and product_id in purchases, and product_id in catalog. Confirm that 'every product' means all products currently in the catalog.

2. Choose an approach

Decide between aggregation (GROUP BY with HAVING) or set-based (NOT EXISTS) methods. Consider data size, duplicates, and performance.

3. Write the query

For aggregation: SELECT customer_id FROM purchases GROUP BY customer_id HAVING COUNT(DISTINCT product_id) = (SELECT COUNT(*) FROM catalog). For NOT EXISTS: SELECT customer_id FROM purchases p WHERE NOT EXISTS (SELECT 1 FROM catalog c WHERE NOT EXISTS (SELECT 1 FROM purchases p2 WHERE p2.customer_id = p.customer_id AND p2.product_id = c.product_id)).

4. Handle edge cases

Consider empty catalog (should return no customers or all? clarify), customers with no purchases, and duplicate purchase records. Use DISTINCT or appropriate joins.

5. Optimize and test

Add indexes on customer_id and product_id if needed. Test with sample data to verify correctness, especially for customers who bought all products and those who missed some.

Key Points to Mention

  • Use of COUNT(DISTINCT product_id) to handle duplicate purchases.
  • Comparison with total product count from catalog table.
  • Alternative NOT EXISTS approach for set-based thinking.
  • Performance considerations: indexing, avoiding unnecessary subqueries.
  • Edge cases: empty catalog, customers with no purchases, products with no purchases.
  • Assumption that catalog is static during query execution.

AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.