The Customers Who Bought Products A and B but Not C interview question is a precise filtering task in SQL. You are typically given a table of orders or customers and products. The objective is to identify customers who have an "inclusive" history with specific products (A and B) while having an "exclusive" relationship with another (C). This Customers Who Bought Products A and B but Not C coding problem tests your ability to handle multiple conditions that must be true across different rows for the same user.
Meta and other social media or e-commerce companies ask this to evaluate how you handle relational sets. It’s not a simple WHERE product IN (A, B) because that would return anyone who bought either. This requires ensuring both A and B exist for the user and then verifying the absence of C. It tests proficiency with subqueries, self-joins, or advanced aggregation with HAVING.
This follows the Database interview pattern of "Set Intersection and Difference."
Imagine three customers:
Whenever you need to check for the presence of some rows and the absence of others for the same ID, the "Aggregation with Boolean Flags" (using MAX(CASE...)) is usually the most readable and efficient approach in SQL.
| Title | Difficulty | Topics | LeetCode |
|---|---|---|---|
| Apples & Oranges | Medium | Solve | |
| Get Highest Answer Rate Question | Medium | Solve | |
| Page Recommendations | Medium | Solve | |
| Project Employees III | Medium | Solve | |
| Reported Posts II | Medium | Solve |