Customers Who Bought All Products
MediumLeetCodeSQL: Subqueries & CTEs
Hint and exampleShowHide
This is relational division: group by the person and keep the groups whose COUNT(DISTINCT item) equals the total number of items, computed in a subquery.
Example: courses(code) and completed(student, code)
courses: C1, C2 completed
student | code
Asha | C1
Asha | C2
Asha | C2
Ravi | C1
SELECT student
FROM completed
GROUP BY student
HAVING COUNT(DISTINCT code) = (SELECT COUNT(*) FROM courses);
-- returns Asha
More in SQL: Subqueries & CTEs
| # | Problem | Platform | Difficulty | Open | 1st1st Visit | 2nd2nd Visit | RevRevise |
|---|---|---|---|---|---|---|---|
| 1 | Customers Who Never Order | LeetCode | Easy | Solve Customers Who Never Order on LeetCode (opens in a new tab) | |||
| 2 | Second Highest Salary | LeetCode | Medium | Solve Second Highest Salary on LeetCode (opens in a new tab) | |||
| 3 | Department Highest Salary | LeetCode | Medium | Solve Department Highest Salary on LeetCode (opens in a new tab) | |||
| 4 | Customers Who Bought All Products | LeetCode | Medium | Solve Customers Who Bought All Products on LeetCode (opens in a new tab) | |||
| 5 | Investments in 2016 | LeetCode | Medium | Solve Investments in 2016 on LeetCode (opens in a new tab) |