What is the difference between "INNER JOIN" and "OUTER JOIN"?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
INNER JOIN and OUTER JOIN differ in how they treat rows that do not have a match on the other side of the join condition. That one rule changes both correctness and row counts, which is why join bugs often show up as mysteriously missing records or inflated aggregates.
INNER JOIN Keeps Only Matching Rows
An INNER JOIN returns rows only when the join condition matches on both sides.
If a customer has no order, that customer does not appear in the result. This is the right join when unmatched rows are irrelevant to the question being asked.
For example, if the question is "show all orders with customer names," then an inner join is natural because the result is supposed to contain only real order-to-customer matches.
OUTER JOIN Preserves Unmatched Rows
An outer join keeps unmatched rows from one side or both sides, depending on which kind of outer join you choose.
- '
LEFT OUTER JOINkeeps all rows from the left table' - '
RIGHT OUTER JOINkeeps all rows from the right table' - '
FULL OUTER JOINkeeps unmatched rows from both sides'
In this query, every customer appears. If a customer has no order, the order columns are NULL.
That makes outer joins the right tool for questions such as "which customers have not placed an order" or "show every customer, even those with no related records."
Filter Placement Can Change the Meaning
A very common mistake is to write a LEFT JOIN and then place a filter on the right-hand table in the WHERE clause.
That filter removes rows where o is NULL, which means the query now behaves more like an inner join for that condition.
If you want to preserve unmatched left-side rows while filtering the joined table, move the filter into the ON clause.
This is one of the most important practical join habits to learn.
Aggregates and Row Multiplication
Join choice also affects aggregates. In one-to-many relationships, joining can multiply rows. If you then count rows without thinking about the business entity you actually meant to count, the result can be wrong.
That is why questions like "how many customers do we have" often require COUNT(DISTINCT c.id) or pre-aggregation instead of a raw count after a join.
Join correctness is not only about row presence. It is also about which unit of data each result row represents.
Performance Starts with Correct Semantics
Performance matters, but the first question is always semantic correctness. Choose the join type that matches the business rule, then improve indexing and query shape afterward.
Indexes on join keys help both inner and outer joins. But no amount of indexing can fix a query that chose the wrong join semantics.
Common Pitfalls
Using INNER JOIN by habit can accidentally remove valid unmatched business records.
Applying WHERE filters on the nullable side of a LEFT JOIN can undo the outer-join behavior you intended.
Ignoring row multiplication in one-to-many joins can break counts and totals.
Summary
- '
INNER JOINreturns only matching rows.' - '
OUTER JOINpreserves unmatched rows from one side or both sides, depending on the join type.' - '
LEFT JOINplus aWHEREfilter on the right table can accidentally behave like an inner join.' - Always choose join type based on business meaning first, then optimize performance.
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.