SQL · Level 2 Joins

SQL NOT EXISTS

Find the customers who never ordered with NOT EXISTS, and see why NOT IN returns nothing as soon as one NULL shows up.

  • NOT EXISTS (subquery)
  • Correlated subqueries
  • The NOT IN + NULL trap

To find rows with no match, like customers who never ordered, use NOT EXISTS. Trap: NOT IN (subquery) returns no rows at all if the subquery contains a single NULL, such as a guest order with no customer.

SELECT p.name
FROM products AS p
WHERE NOT EXISTS (
  SELECT 1 FROM order_items AS i WHERE i.product_id = p.id
);   -- products never sold

Return the name of every customer who has never placed an order, ordered by name. In the database below, one order is a guest checkout with no customer.

Sample data from the first test. The other tests use different rows, so write a query, not a list of answers.

customers 4 rows
idINTEGERnameTEXT
1Ana
2Bo
3Cy
4Di
orders 3 rows
idINTEGERcustomer_idINTEGER
101
113
12NULL
solution.sql
Test Suites Pending
Output