Leetcode Problem 1045. Customers Who Bought All Products

1045. Customers Who Bought All Products

Leetcode Solutions

Counting Distinct Products Bought by Each Customer

Algorithm

  1. Select the customer_id from the Customer table.
  2. Group the results by customer_id to prepare for counting the distinct products each customer bought.
  3. Count the distinct product_key for each grouped customer_id.
  4. Use the HAVING clause to filter only those customers whose count of distinct product_key is equal to the total number of products in the Product table. This total is obtained by a subquery that counts all entries in the Product table.
  5. Return the customer_id of customers who meet the criteria.

erDiagram
    Customer {
        int customer_id
        int product_key
    }

    Product {
        int product_key
    }

    Customer }|--|| Product : has

Using Nested Subquery with Cartesian Product

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...