Find customers who bought every product
Return customers who purchased every product in the catalog at least once. Ignore purchases of unknown products. If the catalog is empty, every customer qualifies. Return only the requested columns and follow the specified ordering. This question probes double not exists expresses relational division: there is no catalog product missing from this customer, including vacuous truth for an empty catalog.
Input
Setup script defining purchases(id INT, customer_id INT, product_id INT); products(id INT, name VARCHAR(30)); customers(id INT, name VARCHAR(30)).
Output
id, name; ascending customer ID.
Constraints
0–10000 rows per table. IDs are unique and non-NULL unless stated otherwise; foreign keys may be NULL. Numeric amounts are integers from 0 to 1000000. Dates are valid YYYY-MM-DD text. Text uses case-sensitive comparison; test data has consistent case. Duplicate business values and NULLs are allowed unless excluded by the task.
Example
Tables and sample rows
CREATE TABLE purchases(id INT, customer_id INT, product_id INT); INSERT INTO purchases VALUES (1,1,1),(2,1,2),(3,1,2),(4,2,1),(5,3,99); CREATE TABLE products(id INT, name VARCHAR(30)); INSERT INTO products VALUES (1,'A'),(2,'B'); CREATE TABLE customers(id INT, name VARCHAR(30)); INSERT INTO customers VALUES (1,'Ana'),(2,'Ben'),(3,'Cara');
Expected result
id | name 1 | Ana
Another dataset (1)
CREATE TABLE purchases(id INT, customer_id INT, product_id INT); CREATE TABLE products(id INT, name VARCHAR(30)); CREATE TABLE customers(id INT, name VARCHAR(30)); INSERT INTO customers VALUES (1,'Solo'),(2,'None');
Expected result
id | name 1 | Solo 2 | None
Covers missing matches, ties, or NULL values without relying on insertion order.
Hints (2)
- Look for catalog products the customer has not bought.
- Accept the customer only when no such product exists.
Target complexity: time O(c * p * b) · space O(1)
