Customers Who Purchased From Every Category
SQL coding challenge · Difficulty: medium · Topic: Aggregation · +100 XP
Problem
Topic: Aggregation · COUNT(DISTINCT ...) · comparing with a total (relational division)
The marketing team wants to reward true all-rounders: customers who have bought from every category the store sells.
Write a query that returns those customers.
- Every product belongs to exactly one
category. The available categories are all the different categories inproducts, whether or not anyone bought from them. - A customer qualifies when they bought at least one product from every available category.
- Several purchases from the same category count once; buying the same product again adds nothing.
- A purchase of a product that is not in
productsbelongs to no category and does not count. - Return each qualifying customer once.
Return the column customer_id (the grader checks the rows, not their order).
Schema
Table: products
| Column | Type | Meaning | | --- | --- | --- | | product_id | INT | Unique id of the product | | category | VARCHAR | The product's category |
Table: purchases
| Column | Type | Meaning | | --- | --- | --- | | customer_id | INT | The customer who bought | | product_id | INT | The product bought (one row per purchase) |
Example 1
Input: products
| product_id | category | | --- | --- | | 101 | Electronics | | 102 | Electronics | | 103 | Books | | 104 | Books | | 105 | Clothing |
Input: purchases
| customer_id | product_id | | --- | --- | | 1 | 101 | | 1 | 103 | | 1 | 105 | | 2 | 101 | | 2 | 102 | | 2 | 103 | | 3 | 103 | | 3 | 104 | | 3 | 105 | | 3 | 101 | | 4 | 105 | | 4 | 105 | | 4 | 105 | | 5 | 102 | | 5 | 104 |
Output:
| customer_id | | --- | | 1 | | 3 |
Explanation:
There are 3 available categories: Electronics (101, 102), Books (103, 104) and Clothing (105).
- Customer 1 bought 101 (Electronics), 103 (Books) and 105 (Clothing): all 3 categories, included.
- Customer 2 bought 3 different products, but 101 and 102 are both Electronics: only 2 categories, excluded.
- Customer 3 bought 4 products covering Books, Clothing and Electronics: all 3, included.
- Customer 4 bought product 105 three times: 3 purchases, 1 category, excluded.
- Customer 5 bought Electronics and Books only: 2 categories, excluded.
Constraints
products.product_idis unique andcategoryis never null.- A customer appears in
purchasesonly if they bought something; customers with no purchases never qualify. - If some category has no purchases at all, no customer can qualify, and the correct result is empty.
- The query runs on MySQL 8.
What this SQL challenge teaches you
“Customers Who Purchased From Every Category” is a medium-level SQL challenge focused on Aggregation. Working through it gives you hands-on practice with COUNT DISTINCT, HAVING, Subquery, Relational division, E-commerce — the kind of transformation you are asked to write in real data engineering work and in technical interviews. You can solve it directly in the browser: the dataset is pre-loaded, so you write the query or DataFrame code, run it, and compare your output against the expected result immediately.
Concepts covered
- COUNT DISTINCT
- HAVING
- Subquery
- Relational division
- E-commerce
How to approach it
If you get stuck, work through these steps in order before looking at a full solution — each one narrows the problem down:
- First count the available categories: SELECT COUNT(DISTINCT category) FROM products.
- Join purchases to products to know each purchase's category, then GROUP BY customer_id.
- COUNT(DISTINCT category) per customer ignores repeated purchases and repeated categories.
- Keep the customers whose count equals the total (HAVING ... = (SELECT COUNT(DISTINCT category) FROM products)).
Interview practice
This problem is good practice for data engineering interviews at companies such as Wipro, Cognizant, Capgemini. Interviewers use problems like it to check whether you can express the logic cleanly and reason about correctness on edge cases such as ties, nulls and empty groups.
How to practise it on PySpark.in
Open the challenge, write your SQL query in the editor and press Run to execute it against the sample dataset. Submitting checks your output against every test case, including hidden ones, so you find out straight away whether your logic holds up. You can retry as often as you like, and each solved challenge adds to your XP.
Related SQL challenges
- Find Duplicate Emails
- HR: Average Salary by Department
- Logistics: Count Shipments by Status
- Count Total Orders Placed by Each Customer
- Find Average Order Amount for Each Customer
- Find Customers Who Placed More Than 5 Orders (HAVING)
Helpful resources
Frequently asked questions
Do I need to install Spark or a database to solve this?
No. The SQL environment runs in your browser with the sample data already loaded, so there is nothing to install or configure.
Is this challenge free?
Yes - the problem, the sample dataset, the hints and unlimited test runs are free.
What level is it?
It is rated medium and covers Aggregation.