INNER JOIN vs. EXISTS for Existence Checks in MySQL

Posted on Feb 20, 2024

INNER JOIN vs. EXISTS for Existence Checks in MySQL

When checking if a relation exists in MySQL, you might use an INNER JOIN, but sometimes EXISTS (SELECT ..) is much faster. The EXISTS clause stops searching as soon as a match is found, while INNER JOIN may process unnecessary rows.

Example using EXISTS:

SELECT * FROM users u
WHERE EXISTS (SELECT 1 FROM orders o WHERE o.user_id = u.id);

Always test performance before deciding, as the best approach depends on table size, indexing, and query optimization.