Customers Who Never Order
EasyLeetCodeSQL: Subqueries & CTEs
Hint and exampleShowHide
Return the rows of one table that have no match in another with NOT EXISTS (or LEFT JOIN ... IS NULL), and avoid NOT IN when the subquery can contain NULL.
Example: authors(id, name) and books(id, author_id)
authors books
id | name id | author_id
1 | Asha 10 | 1
2 | Ravi 11 | 1
3 | Meera
SELECT a.name
FROM authors a
WHERE NOT EXISTS (SELECT 1 FROM books b WHERE b.author_id = a.id);
-- returns Ravi, Meera
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) |