Scrivio Interview library
ConceptFoundations7 min read · updated 12 November 2026

Is a JOIN always faster than a subquery? Explain how modern optimizers treat IN, EXISTS and joins, and how you would decide in practice.

No. Modern optimizers rewrite most IN and EXISTS subqueries into semi-joins, so they often get exactly the same plan as the equivalent join. The real differences are semantic, a join can multiply rows while EXISTS returns each outer row once, and practical, a correlated scalar subquery in the select list may run once per row if the planner cannot decorrelate it. The 'joins are faster' rule comes largely from MySQL 5.5 and earlier, which executed IN subqueries poorly; in practice we decide by comparing EXPLAIN ANALYZE output on realistic data.

Why this question is asked

'Always use joins instead of subqueries' is one of the most repeated pieces of SQL advice, and like most rules of thumb it was once true somewhere and is now mostly misleading. Following it blindly leads to joins with DISTINCT where EXISTS was clearer and faster. Interviewers ask this to see whether we know how optimizers actually treat subqueries, understand that joins and subqueries can mean different things, and have a practical way to decide. In this answer, we will recap the kinds of subqueries, see how modern optimizers treat IN and EXISTS, look at where the semantics differ, find the case that really can be slow, and explain where the folklore came from.

1A Quick Recap of the Kinds of Subqueries

A subquery is a SELECT inside another statement. Three kinds matter here. A filtering subquery in WHERE, such as id IN (SELECT ...) or EXISTS (SELECT ...), decides which outer rows to keep. A scalar subquery in the select list returns one value per outer row, such as the latest order date of each customer. A derived table is a subquery in FROM that acts like a table. Any of them can be correlated, meaning it refers to the outer row, or uncorrelated, meaning it can be computed once on its own.

2How Modern Optimizers Treat IN and EXISTS

PostgreSQL, MySQL 8, Oracle and SQL Server all rewrite filtering IN and EXISTS subqueries into semi-joins, a join that keeps each outer row at most once and stops at the first match. Once rewritten, the planner chooses among the same algorithms it uses for joins: nested loop with an index, hash or merge. The result is that a subquery and the equivalent join often produce the same plan.

Java
EXPLAIN SELECT c.* FROM customers c WHERE c.id IN (SELECT o.customer_id FROM orders o WHERE o.total > 500);
-- Hash Semi Join
-- Hash Cond: (c.id = o.customer_id)
-- -> Seq Scan on customers c
-- -> Hash -> Seq Scan on orders o Filter: (total > 500)
EXPLAIN SELECT c.* FROM customers c WHERE EXISTS (SELECT 1 FROM orders o WHERE o.customer_id = c.id AND o.total > 500);
-- Hash Semi Join (same plan)

Notice that the IN version and the EXISTS version produce the same Hash Semi Join. A join version would produce a Hash Join followed by a step to remove duplicates, which is more work, not less. So for filtering, the subquery is not only as fast as the join, it is often faster, because it asks for less.

3Where Joins and Subqueries Mean Different Things

The bigger difference is semantic. A join returns one row for each matching pair. A customer with five orders above 500 appears five times in the join and once in the IN or EXISTS version. To make the join return the same result as the subquery, we must add DISTINCT, which sorts or hashes the joined rows, and which also merges rows that were genuinely different if the customer table had duplicates.

SQL
-- customers: (1, 'Ada'), (2, 'Ben'); orders over 500: Ada x 5, Ben x 1
SELECT c.name FROM customers c JOIN orders o ON o.customer_id = c.id WHERE o.total > 500;
-- Ada, Ada, Ada, Ada, Ada, Ben -- 6 rows
SELECT c.name FROM customers c WHERE EXISTS
(SELECT 1 FROM orders o WHERE o.customer_id = c.id AND o.total > 500);
-- Ada, Ben -- 2 rows
Join or subquery?
NeedNatural formWhy
Columns from both tablesJOINOne row per matching pair is the point
Filter by existence of a matchEXISTS / INSemi-join: each outer row once, no DISTINCT
Filter by absence of a matchNOT EXISTSAnti-join; NOT IN breaks on NULLs
One value per row from another tableJoin to grouped data, or scalar subquery + indexAvoid per-row work on large sets

So comparing their speed only makes sense once they return the same thing. When we need columns from orders, a join is the natural tool. When orders only filters customers, a semi-join, written as EXISTS or IN, is the correct one. NOT IN is a special case with its own trap: if the subquery returns any NULL, it returns no rows, so NOT EXISTS is the safe anti-join form.

4The Case That Really Can Be Slow

Scalar subqueries in the select list are where the rule of thumb has some truth. A correlated scalar subquery logically runs once per outer row. If the optimizer cannot decorrelate it, turn it into a join against grouped data, it does exactly that. PostgreSQL runs such subqueries as a SubPlan per row; MySQL 8.0 has an optional transformation for some of them (check your version).

Java
SELECT c.id, (SELECT count(*) FROM orders o WHERE o.customer_id = c.id) AS order_count
FROM customers c;
-- Seq Scan on customers c (rows=1000000)
-- SubPlan 1 (loops=1000000) -> Aggregate -> Index Only Scan on orders_customer_id_idx
SELECT c.id, coalesce(t.order_count, 0) AS order_count
FROM customers c
LEFT JOIN (SELECT customer_id, count(*) AS order_count FROM orders GROUP BY customer_id) t
ON t.customer_id = c.id;
-- Hash Right Join -> HashAggregate over orders, one pass

For a million customers, the first query runs a million small index scans, while the second reads orders once and aggregates. Here the join to a derived table is usually faster. But even this is not a rule: for a page of 20 customers, the 20 index probes of the scalar subquery are cheaper than aggregating the whole orders table. It depends on how many outer rows there are and on the indexes.

5Where the Folklore Came From, and How to Decide

The 'joins are faster' advice comes mostly from MySQL before 5.6. MySQL 5.5 and earlier often executed IN (SELECT ...) as a dependent subquery, re-running it for every outer row even when it was not correlated, which made such queries dramatically slower than the equivalent join. MySQL 5.6 introduced semi-join optimisations and subquery materialisation, and MySQL 8.0 added anti-joins and hash joins, so the original reason is long gone. Developers who learned on those old versions passed the rule on.

So how do we decide in practice? We write the query that expresses the question most clearly, EXISTS for existence, a join when we need the other table's columns, and then check with EXPLAIN ANALYZE on realistic data. If the plan is good, we are done. If it is not, we compare alternatives on the same data and keep the faster one, with the measurements in the code review. That replaces a rule of thumb with evidence.

6Follow-up questions

Are uncorrelated subqueries ever executed once per row?

Not in modern engines. An uncorrelated subquery does not depend on the outer row, so it is either turned into a semi-join or computed once, shown in PostgreSQL as an InitPlan or a hashed SubPlan. Re-running it per row was the old MySQL behaviour that created the folklore.

Is IN with a subquery the same as EXISTS?

For positive filtering with non-NULL columns, yes: both become semi-joins and usually get the same plan. They differ in NULL handling only for the negative forms: NOT IN returns nothing if the subquery yields a NULL, while NOT EXISTS is unaffected. That is why NOT EXISTS is the recommended form for anti-joins.

Should I prefer CTEs over subqueries for readability?

For long queries, often yes, because named steps are easier to follow. In PostgreSQL 12 and later, a non-recursive CTE used once is inlined, so it performs like a subquery; on older versions it was always materialised. Readability is a fine reason to choose a form, as long as we check the plan of the queries that matter.

7Conclusion

In this answer, we saw that a join is not always faster than a subquery. Modern optimizers turn IN and EXISTS into semi-joins that often share a plan with the equivalent join, while a join can multiply rows and then needs DISTINCT. Correlated scalar subqueries in the select list can run per row and may be slower than a join to grouped data on large sets. The 'joins are faster' rule comes from MySQL 5.5 and earlier, and in practice we decide with EXPLAIN ANALYZE.

What to say in the interviewAbout 90 seconds aloud

Open with

No, not in modern databases. PostgreSQL, MySQL 8, Oracle and SQL Server rewrite IN and EXISTS subqueries into semi-joins, so the planner picks a nested loop, hash or merge strategy just as for a join, and the plan is often identical to the join version, or better, since a semi-join stops at the first match and returns each outer row once.

Then, if they want more

The more important difference is meaning. A join returns one row per matching pair, so a customer with five matching orders comes back five times, and making it equal to EXISTS needs DISTINCT, which is an extra sort or hash.

When I only filter by another table I use EXISTS, for absence NOT EXISTS, since NOT IN breaks on NULLs, and I use a join when I need columns from both tables.

The case with some truth is a correlated scalar subquery in the select list, like a count of orders per customer: PostgreSQL runs it as a SubPlan once per outer row, so for a million customers a join to a grouped derived table is usually faster, though for a page of 20 rows the subquery with an index is fine.

The folklore comes mainly from MySQL 5.5 and earlier, which ran IN subqueries as dependent subqueries per row; 5.6 and 8.0 fixed that. So in practice I write the clearest query and confirm with EXPLAIN ANALYZE on realistic data.

About this answer

Last updated
12 November 2026
Versions it names
MySQL 5.5, MySQL 8.0, MySQL 5.6, MySQL 8
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "Is a JOIN always faster than a subquery? Explain how modern optimizers treat IN, EXISTS...": https://scrivio-interview-library.netlify.app/databases/sql-joins-and-subqueries/626a0e5ce8/