Advanced NOT & Negation Logic: Interview
Module: Advanced Filtering
Why is NOT IN dangerous with NULL values?
NOT IN returns no rows if the list contains NULL because NULL comparisons return NULL (not true/false). Use NOT EXISTS or filter NULL from NOT IN list.
When should you use NOT EXISTS vs NOT IN?
Use NOT EXISTS for subqueries - it's NULL-safe and often faster. Use NOT IN only for small literal lists without NULL. NOT EXISTS is generally preferred.
Find customers without orders (NULL-safe)
SELECT * FROM customers c WHERE NOT EXISTS (SELECT 1 FROM orders o WHERE o.customer_id = c.id);
NOT EXISTS is NULL-safe and efficient for finding missing relationships.