SQL querying · Build it
Customers who never placed an order
Practise this out loud
Say your answer out loud to an AI interviewer that follows up, and get a score for it.
Practise by voiceThe 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
- Write it with a LEFT JOIN, then with NOT EXISTS.
- A teammate writes
NOT IN (SELECT customer_id FROM orders). When does that return zero rows? - Now find customers with no order in the last 90 days. Where does the date condition go?
- Which version is fastest, and what index helps?
- 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 ofordersthat 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 INreturns nothing if anyorders.customer_idis 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 voicePractise 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.