Back to Practice
#0377
Customers Bought A but Not B
MediumSQL24 min20 XP
Problem
Return customer_id values for customers who bought product A but never bought product B.
Why This Matters
SQL interviews usually test whether you can turn a business question into the right rows, grouping level, join type, and edge-case behavior.
Schema
purchases(customer_id INTEGER, product TEXT)
Examples
Example 1
Inputcustomers 2 and 4 bought A only
Output2 and 4
NOT EXISTS excludes customers with product B purchases.
Constraints
- Return only the requested columns.
- Use deterministic ORDER BY when multiple rows are returned.
- Assume SQLite-compatible SQL unless the prompt states otherwise.
CodeSQL
Visible browser tests run here when available.
Testcases1 visible / 3 hidden categories
Core SQL case
Inputcustomers 2 and 4 bought A only
Expected[[2],[4]]
NOT EXISTS excludes customers with product B purchases.
Hidden Test Categories
Empty or missing matchesDuplicate rows or tiesNULL values where the pattern commonly breaks