Left, Right and Inner Joins

Which join is faster in SQL?

No join type is faster by default. Speed depends on indexes, table sizes and the query plan, so test your own queries with EXPLAIN before you choose.

On this page
  1. The short answer
  2. Why an inner join often looks faster
  3. LEFT and RIGHT joins usually get the same plan
  4. What actually changes the speed
  5. Outer joins can do more work
  6. How to measure it yourself
  7. A quick checklist

The short answer

No join type is faster by default. An inner join isn’t automatically quicker than an outer join, and a left join isn’t automatically quicker than a right join. Speed depends on the indexes you have, how many rows each table holds, which filters apply, and the plan the database’s optimizer chooses.

Pick the join that returns the rows you need, then tune it. Choose for correctness first, using when to use left, right, or inner join as your guide, and worry about speed once the results are right.

Why an inner join often looks faster

An inner join keeps only the rows that match on both sides, so it can return fewer rows than an outer join on the same tables. Less output can mean less work later in the query, such as sorting, grouping or sending results to your application.

Imagine a customers table with 100,000 rows, where only 2,000 customers have orders. A left join from customers returns at least 100,000 rows, because every customer appears. An inner join returns only the 2,000 matched customers. That difference in output can matter.

The word to notice is “can.” Fewer output rows isn’t a guarantee. If both joins read the same tables and do the same matching work, the time difference may be small.

LEFT and RIGHT joins usually get the same plan

A LEFT JOIN and a RIGHT JOIN are the same operation with the sides swapped, so their speed is usually close. The MySQL manual says a RIGHT JOIN is converted into an equivalent LEFT JOIN, so the engine handles both in the same way. Other optimizers often do something similar.

Choosing one direction over the other rarely fixes a slow query. The table sizes, indexes and filters matter more, and they can change the plan no matter which direction you write.

What actually changes the speed

  • Indexes on the join columns. A condition such as o.customer_id = c.customer_id can use an index to find matching rows without reading the whole table:
CREATE INDEX idx_orders_customer_id ON orders (customer_id);
  • Table sizes and statistics. The optimizer chooses which table to read first, and it estimates row counts using statistics. Out-of-date statistics can lead to a poor plan, so keep them current.
  • Filters. A condition in WHERE that reduces one table early means fewer rows to join. Filters on the optional side of an outer join belong in ON, or they change the results.
  • Columns you select. Selecting only the columns you need means less data to read and send.
  • Functions and type mismatches on join columns. Wrapping a join column in a function, or comparing columns of different types, can stop the database from using an index on it.

Outer joins can do more work

An outer join has to keep every row from the side it preserves. That can limit how the optimizer reorders the tables, so in some plans an outer join does more work than an equivalent inner join.

The reverse can also happen. If a WHERE condition on the optional table would discard the NULL rows anyway, many optimizers treat the outer join as an inner join, and the speed becomes the same.

How to measure it yourself

Compare the two queries on data of realistic size, using your database’s plan tool. In MySQL and PostgreSQL, EXPLAIN shows the plan the database intends to use, and EXPLAIN ANALYZE runs the query and shows actual timings:

EXPLAIN ANALYZE
SELECT c.name, o.order_id
FROM customers AS c
INNER JOIN orders AS o
    ON o.customer_id = c.customer_id;

Run the same query with LEFT JOIN and compare the two plans. Look at row counts and time at the join step. Run each query a few times, because the first run can be slower while caches warm up. SQL Server has an equivalent in its execution plans.

A quick checklist

  • Index the columns used in ON conditions, especially foreign keys.
  • Put conditions on the optional table in ON, and conditions on the kept table in WHERE.
  • Select only the columns you need.
  • Avoid functions on join columns where you can.
  • Compare execution plans on realistic data instead of guessing.

Speed matters, but a fast query that returns the wrong rows is still wrong. Settle the join type first, then measure.

The complete guide to left, right and inner joins

Left Outer Join vs Right Outer Join in SQL
Read the guide

Keep exploring

Guides from other topics

All topics →