Start learning free

LESSON 24 OF 66 · SQL: INTERVIEW QUESTIONS

Customers Who Never Ordered

Practice

Interviewers love this one: which customers have never placed an order? In one sentence: LEFT JOIN the orders to every customer, which keeps the customers with no match, and keep the rows where the order side is NULL.

For this lesson the check adds one odd row to orders, an order with no customer:

id | customer_id | ordered    | status
8  | NULL        | 2026-05-31 | paid

That NULL breaks the answer most people write first, which is already in your file as not_in_query: WHERE id NOT IN (SELECT customer_id FROM orders). x NOT IN (1, 2, NULL) means x != 1 AND x != 2 AND x != NULL, and a comparison with NULL is unknown, never true. So NOT IN keeps no customer at all.

NOT EXISTS has no such trap: it is true when its subquery finds no row, and the subquery can use the outer query's row. For example, the products nobody has bought:

SELECT name
FROM products
WHERE NOT EXISTS (SELECT 1 FROM order_items WHERE order_items.product_id = products.id)

The check prints all three queries. not_in_query stays as it is; query (LEFT JOIN) and exists_query (NOT EXISTS) must both find Eva Rossi:

name
(0 rows)
name
Eva Rossi
(1 row)
name
Eva Rossi
(1 row)

Your task: in query, select customers.name from customers, LEFT JOIN orders on orders.customer_id = customers.id, and keep the rows where orders.id is NULL. Then in exists_query, select name from customers where no order has their id.


Assignment
Follow the Coding Tutorial to write query with a LEFT JOIN and exists_query with NOT EXISTS, one clause per line.


Hint
Test for NULL with IS NULL: = NULL is never true. Inside the NOT EXISTS subquery, customers.id is the customer the outer query is looking at.


STEP 1 OF 6
    Ctrl + Enter to check