Joins & Relationships
Subqueries vs JOINs: Performance & Readability: Mistakes
-- Using IN when EXISTS would be better SELECT customer_name FROM customers WHERE customer_id IN ( SELECT customer_id FROM orders WHERE total_amount > 1000 ); -
-- Using IN when EXISTS would be better
SELECT customer_name FROM customers
WHERE customer_id IN (
SELECT customer_id FROM orders WHERE total_amount > 1000
);
-- Use EXISTS for better performance
SELECT customer_name FROM customers c
WHERE EXISTS (
SELECT 1 FROM orders o
WHERE o.customer_id = c.customer_id AND o.total_amount > 1000
);
EXISTS stops at first match. IN must build complete result set. EXISTS handles NULLs better.
IN can be slower than EXISTS for large datasets