LEFT JOIN vs. INNER JOIN performance
Compare LEFT JOIN vs. INNER JOIN performance in PostgreSQL using runnable queries, result checks, execution plans, and a repeatable method for fair timing.

Neither LEFT JOIN nor INNER JOIN is always faster: LEFT JOIN vs. INNER JOIN performance depends on the required result, the data, and the database's execution plan. INNER JOIN keeps matching rows; LEFT JOIN also preserves unmatched left-side rows, so replacing one with the other can change the answer. Choose the correct result first, then compare equivalent queries using PostgreSQL execution plans and measured execution times.
Performance without a universal winner
Join keywords describe which rows belong in the result, not exactly how PostgreSQL must find them. A physical join algorithm is the database's matching method, such as a hash join, merge join, or nested loop. Different SQL statements can use the same algorithm and perform similar work.
Indexes, filters, table sizes, repeated keys, and planner statistics all influence the execution plan. Planner statistics summarize the data so PostgreSQL can estimate how many rows an operation will produce. Inaccurate estimates can lead to poor choices even when the join keyword is appropriate.
Don't assume putting the smaller table first makes a query faster. PostgreSQL can reorder many inner joins. Outer joins impose additional constraints because preserving unmatched rows limits which transformations keep the same meaning.
A result check before timing
The first check is whether each query preserves the customers your report requires. Run the following fictional exercise in a fresh PostgreSQL session, and keep that session open for the next example. NULL represents a missing or unknown value; here, PostgreSQL supplies NULL for the unmatched order.
-- Target: preserve every customer, including customers without orders.
-- Starting data: three customers and three orders.
CREATE TEMP TABLE join_customers (
customer_id integer PRIMARY KEY
);
CREATE TEMP TABLE join_orders (
order_id integer PRIMARY KEY,
customer_id integer
);
INSERT INTO join_customers VALUES (1), (2), (3);
INSERT INTO join_orders VALUES (11, 1), (12, 1), (13, 2);
SELECT c.customer_id, o.order_id
FROM join_customers AS c
LEFT JOIN join_orders AS o
ON o.customer_id = c.customer_id
ORDER BY c.customer_id, o.order_id;
-- Expected rows:
-- customer_id | order_id
-- 1 | 11
-- 1 | 12
-- 2 | 13
-- 3 | NULL
-- Exact change: replace LEFT JOIN with INNER JOIN.
SELECT c.customer_id, o.order_id
FROM join_customers AS c
INNER JOIN join_orders AS o
ON o.customer_id = c.customer_id
ORDER BY c.customer_id, o.order_id;
-- Expected rows:
-- customer_id | order_id
-- 1 | 11
-- 1 | 12
-- 2 | 13
-- Model check:
-- Customer 1 contributes two matching rows.
-- Customer 2 contributes one matching row.
-- Customer 3 has no matching order.
-- LEFT JOIN: 2 + 1 + 1 = 4 rows.
-- INNER JOIN: 2 + 1 = 3 rows.
-- Only LEFT JOIN meets the original target.
Try the distinction yourself with LEFT JOIN: keep rows without a match, then INNER JOIN: combine matching rows. The Work Gym offers browser-based practice checked automatically with a hint that names the mistake.
Equivalent queries under the microscope
An equivalent comparison becomes possible when the requirement changes to returning only matching customer-order pairs. In the same PostgreSQL session, add the same non-null order filter to both variants, as shown below. The order identifier is a primary key, so a matched order can't have a NULL identifier.
-- Diagnostic target: return only matching customer-order pairs.
-- Starting state: the temporary tables from the preceding exercise.
-- Exact change: add WHERE o.order_id IS NOT NULL to both variants.
-- Expected rows for each: (1, 11), (1, 12), (2, 13).
SELECT c.customer_id, o.order_id
FROM join_customers AS c
LEFT JOIN join_orders AS o
ON o.customer_id = c.customer_id
WHERE o.order_id IS NOT NULL
ORDER BY c.customer_id, o.order_id;
SELECT c.customer_id, o.order_id
FROM join_customers AS c
INNER JOIN join_orders AS o
ON o.customer_id = c.customer_id
WHERE o.order_id IS NOT NULL
ORDER BY c.customer_id, o.order_id;
-- Refresh planner statistics before inspecting execution.
ANALYZE join_customers;
ANALYZE join_orders;
-- For each diagnostic query, remove ORDER BY and add the plan prefix.
-- Both timed variants now have the same columns and no ordering.
explain (ANALYZE, BUFFERS)
SELECT c.customer_id, o.order_id
FROM join_customers AS c
LEFT JOIN join_orders AS o
ON o.customer_id = c.customer_id
WHERE o.order_id IS NOT NULL;
explain (ANALYZE, BUFFERS)
SELECT c.customer_id, o.order_id
FROM join_customers AS c
INNER JOIN join_orders AS o
ON o.customer_id = c.customer_id
WHERE o.order_id IS NOT NULL;
-- Expected check: the top plan node reports three actual rows.
-- Plan operations, buffer counts, and execution times vary.
The filter rejects unmatched rows because their right-side order identifier is NULL. PostgreSQL can therefore simplify the filtered LEFT JOIN into an inner join. Different SQL wording may produce the same physical plan, so don't expect a speed difference just because the keywords differ.
An execution plan describes database operations as a tree. The ANALYZE option executes the statement and adds runtime measurements; use care with statements that modify data. For these SELECT queries, read the plan using these checks:
- Estimated rows versus actual rows: large differences suggest PostgreSQL misunderstood how many rows an operation would produce.
- Loops: an operation may run repeatedly. Actual rows and times are reported as per-execution averages when loops exceed one.
- Buffer activity: hits indicate requested blocks were already in PostgreSQL buffers; reads don't necessarily mean physical disk access because operating-system caching can intervene.
- Execution time: compare server-side execution under similar conditions, rather than treating one measurement as a verdict.
A defensible timing comparison
A useful timing comparison holds the required output and testing conditions constant. Check selected columns, filters, ordering, and repeated rows before measuring. Equal row counts alone don't establish equivalence: two queries can return the same number of different rows.
Use representative table sizes, proportions of matching rows, and repeated join keys. Refresh planner statistics after substantial data changes, then inspect estimated versus actual row counts. An estimate problem deserves investigation before a join-keyword rewrite.
Run both variants repeatedly in alternating order. Record whether data was already cached, keep other database activity as consistent as practical, and compare the spread of execution times. Separate server execution from application fetching and display time, which can dominate the experience of a large result.
The tiny exercise proves result behavior, not production speed. A sequential scan, which reads a table rather than locating rows through an index, can be sensible for a small table. Also, the primary key on the order identifier doesn't create an index on the customer identifier used to match orders.
Before trying another optimization, practice Performance rewrites with the same meaning. Keep correctness checks separate from measurements of your own workload.
Practice methods and selection criteria
Automatic feedback helps you catch mistakes, but measuring a workload requires control over the database and data.
| Approach | Feedback | Data control | Plan inspection | Best use and limitation |
|---|---|---|---|---|
| Checked hands-on exercises | Automatic correctness feedback | Usually the exercise's starting data | Depends on the exercise environment | Practice result requirements; completion isn't a production benchmark. |
| Local PostgreSQL experiments | Your result checks and measurements | You choose schemas, data, and indexes | Direct access to execution plans | Test workload behavior; setup and realistic data take effort. |
| Video courses | Demonstrations and explanations | The presenter's data unless you reproduce it | Observe demonstrated plans | Build an initial mental model; watching doesn't test your query. |
| Documentation or books | Rules and explanations to apply yourself | You supply the test environment | Learn terminology and interpretation | Resolve specific behavior; reading alone doesn't verify a rewrite. |
Use checked practice to strengthen correctness habits, local PostgreSQL experiments to measure your queries, and the PostgreSQL documentation's planner and performance sections to resolve unfamiliar plan behavior. No single approach replaces all three kinds of evidence.
Related performance distinctions
Join performance also depends on whether an alternative expression asks for the same result. A subquery, a query nested inside another query, isn't inherently slower or faster than a join; PostgreSQL can transform different expressions into similar plans.
A semi join expresses existence: each qualifying left-side row appears once regardless of how many right-side matches exist. A regular LEFT JOIN can repeat a left-side row for multiple matches. Replacing one with the other without checking duplication changes the question.
Inner joins retain matches. Outer joins also preserve unmatched rows from the left side, right side, or both sides, depending on the join type. CROSS JOIN produces combinations rather than requiring a matching condition. Choose among those meanings before discussing speed.
A right-side index doesn't guarantee a faster join. Its usefulness depends on the indexed columns, table sizes, the fraction of rows needed, and the chosen access method. The defensible conclusion is specific: the query preserves the required result and performs better under the conditions you measured.
Common questions
Is INNER JOIN always faster than LEFT JOIN? No. INNER JOIN and LEFT JOIN can require different results, and PostgreSQL may use similar physical operations for either. Compare performance only after confirming that both queries satisfy the same requirement.
Can PostgreSQL turn a LEFT JOIN into an INNER JOIN? Yes, when the transformation preserves the result. In the example, filtering for a non-null right-side primary key rejects unmatched rows, allowing PostgreSQL to simplify the outer join.
Is a join faster than a subquery? Neither form has a universal advantage. PostgreSQL can transform some joins and subqueries into similar execution plans, so compare equivalent results and runtime measurements on representative data.
Practice what this post teaches
How does a left semi join differ from a LEFT JOIN? A left semi join keeps each qualifying left-side row once when at least one right-side match exists. LEFT JOIN preserves unmatched left-side rows and can repeat a left-side row when multiple right-side rows match. The two operations aren't interchangeable.