Skip to content
Onsite

Customers who bought every product

MediumP2Coding

Tables:

product(id int primary key)

purchases(customer_id int, product_id int) (a customer can buy the same product many times; product_id may refer to a product that is no longer in product)

Return the ids of customers who bought every product in product at least once. If product is empty, return no rows.

Output column: customer_id.

Schema

CREATE TABLE product (
  id int PRIMARY KEY
);
CREATE TABLE purchases (
  customer_id int NOT NULL,
  product_id int NOT NULL
);

Example 1

Input
product: 1, 2, 3; purchases: (10, 1), (10, 2), (10, 3), (11, 1), (11, 1), (11, 2)
Output
customer_id: 10
Why
Customer 11 bought product 1 twice but never bought product 3.

Constraints

  • PostgreSQL dialect.

Hints

PostgreSQL
Run checks the 3 visible tests, Submit all 6.