Skip to the page

SQL querying · Build it

Customers who never placed an order

The interviewer asks

Tables customers(id, name, city) and orders(id, customer_id, amount, created_at). Write a query for customers who have never placed an order. Give me two different ways.

Then

Follow-ups the interviewer asks next

  1. Write it with a LEFT JOIN, then with NOT EXISTS.
  2. A teammate writes NOT IN (SELECT customer_id FROM orders). When does that return zero rows?
  3. Now find customers with no order in the last 90 days. Where does the date condition go?
  4. Which version is fastest, and what index helps?
  5. Count those customers per city.

The answer

What a strong answer covers

  • SELECT c.* FROM customers c LEFT JOIN orders o ON o.customer_id = c.id WHERE o.id IS NULL; checks a column of orders that can never be NULL, like its primary key.
  • SELECT c.* FROM customers c WHERE NOT EXISTS (SELECT 1 FROM orders o WHERE o.customer_id = c.id); is clear and NULL-safe. Planners turn both into an anti-join.
  • NOT IN returns nothing if any orders.customer_id is NULL.
  • 90 days: the date condition goes in the ON clause of the LEFT JOIN, or inside NOT EXISTS. In WHERE it removes the very customers you want.
  • An index on orders(customer_id), or (customer_id, created_at) for the 90-day version.

The red pen

Common traps

Avoid:putting o.created_at > … in WHERE after a LEFT JOIN; NOT IN over a nullable column; an inner join with COUNT(*) = 0.

Practise this out loud

Say your answer out loud to an AI interviewer that follows up, and get a score for it.

Practise by voice

Practise questions like this out loud

Sign up and start with a 15-minute interview with the full report, and an English conversation. No card needed.

Start your free interview