Interview Question

Find customers without orders

Use NOT EXISTS to find parent rows with no child row.

💡 Concept ✅ Quick Revision 🗃️ SQL

Answer

Customers without orders can be found with NOT EXISTS. • The correlated subquery searches for an order belonging to each customer. • NOT EXISTS keeps the customer only when no match exists. • This avoids the NULL behavior that can surprise NOT IN queries.

💡 SQL Example

SELECT c.customer_id, c.name FROM customers c WHERE NOT EXISTS (SELECT 1 FROM orders o WHERE o.customer_id = c.customer_id);

Result

Customers with no matching order

⚡ Quick Revision

Use NOT EXISTS to find parent rows with no child row.