InterviewPrepKit

Home / SQL / Joins

Left Join to Find Customers With No Orders

easy
Solving tips
  • To find rows on the left with no match on the right, LEFT JOIN then filter WHERE right_key IS NULL.
  • Test the IS NULL on a right-side column that can never legitimately be NULL, such as the right table's primary key.
  • This anti-join pattern answers who is missing, unlike an INNER JOIN which answers who matches.

Find the customers who have never placed an order.

Schema

CREATE TABLE customers (
    id   INT PRIMARY KEY,
    name VARCHAR(50) NOT NULL
);

CREATE TABLE orders (
    id          INT PRIMARY KEY,
    customer_id INT REFERENCES customers(id),
    amount      NUMERIC(10,2) NOT NULL
);

customers

idname
1Alice
2Bob
3Carol
4Dave
5Eve

orders

idcustomer_idamount
101150.00
102130.00
103320.00
104480.00

Task

Return the customers who appear in no order, with columns customer_id (the customer’s id) and name. Order by customer_id ascending.

Expected output

customer_idname
2Bob
5Eve
Your workspace Not runnable by design — this is your interview scratchpad. Saved on this device.