Skip to main content

Command Palette

Search for a command to run...

LeetCode 1045: Customers Who Bought All Products

LeetCode 1045: Customers Who Bought All Products Solution

Published
1 min read
V

I'm Varchasv, a Data Engineer working on enterprise data integration.

Currently on a 90-problem challenge to level up my technical skills and switch to a more development-focused role.

What I'm doing:

  • Solving 2 Leetcode problems daily (SQL + DSA).
  • Blogging about each problem.
  • Building in public.

My Goal - Land a better data engineering role by mid-2026.

Follow my journey !!

Date: February 12, 2026
Category: SQL
Time Taken: 10 minutes
Difficulty: Medium


Problem Statement

Write a solution to report the customer ids from the Customer table that bought all the products in the Product table.

Link: Customers Who Bought All Products


My Approach:

This solution comes down to checking if the number of distinct products a customer bought is equal to the total number of products in the Product table. We use DISTINCT because a customer can buy a product multiple times, and without it, we would get an incorrect answer.

Solution Code:

SELECT customer_id
FROM Customer
GROUP BY customer_id
HAVING COUNT(DISTINCT product_key) = (
    SELECT COUNT(*)
    FROM Product
)

Pattern: Subquery | Aggregation

Mistakes I Made: I did not use DISTINCT initially and that resulted in incorrect solution.

Series: 90 Days of Data Engineering Progress: 42/90 problems completed

Tags: #DEQuest #LeetCode #SQL #DataEngineering #BuildInPublic

90 Days of Data Engineering

Part 45 of 48

Solving 90 problems over 18 weeks. Daily posts Mon-Fri. Goal: Switch to development role.

Up next

LeetCode 1070: Product Sales Analysis III

Solving LeetCode 1070: Product Sales Analysis III Guide