© Anton Dolganin 2025
This looks fine:
SELECT * FROM orders
WHERE customer_id NOT IN (SELECT id FROM banned_customers);
But in production, it can drag performance — or break logic.
Why? If the subquery returns even one NULL, NOT IN stops working properly. PostgreSQL can't optimize it well.
Better:
SELECT * FROM orders o
LEFT JOIN banned_customers b ON o.customer_id = b.id
WHERE b.id IS NULL;
Faster. Safer. Null-proof.
© Anton Dolganin 2025