What's the difference between INNER JOIN, LEFT JOIN, RIGHT JOIN and FULL JOIN?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
The difference between SQL join types is really about which unmatched rows you want to keep. INNER JOIN keeps only matches, LEFT JOIN keeps all rows from the left table, RIGHT JOIN keeps all rows from the right table, and FULL JOIN keeps unmatched rows from both sides.
A Small Example Dataset
Assume these two tables:
employees
departments
Notice two important facts:
- employee
Adahas a matching department - employee
Linuspoints to a department that does not exist in the second table - department
Financehas no matching employee
Those missing matches are what make the join types different.
INNER JOIN: Keep Only Matches
Result idea:
- '
Adaappears' - '
Linusdoes not appear' - '
Samdoes not appear' - '
Financedoes not appear'
INNER JOIN answers the question, "show me rows where both sides matched the join condition."
LEFT JOIN: Keep All Rows from the Left Table
Now every employee row stays in the result:
- '
Adaappears withEngineering' - '
Linusappears withNULLdepartment data' - '
Samappears withNULLdepartment data'
This is the join to use when the left table is your primary subject and the right table is optional lookup data.
RIGHT JOIN: Keep All Rows from the Right Table
Now every department row stays in the result:
- '
Engineeringappears withAda' - '
Financeappears withNULLemployee data'
RIGHT JOIN is valid, but many teams prefer rewriting it as a LEFT JOIN with the table order swapped because it is easier to read consistently.
FULL JOIN: Keep Unmatched Rows from Both Sides
This keeps:
- matched rows such as
AdaandEngineering - unmatched employees such as
LinusandSam - unmatched departments such as
Finance
So you can think of FULL OUTER JOIN as the union of the unmatched-row behavior of left and right joins.
The Join Condition Still Matters
A common source of confusion is not the join type but where filtering happens. If you use a LEFT JOIN and then put a filter on the right table in the WHERE clause, you can accidentally turn it back into inner-join behavior.
Example:
That removes rows where d.department_name is NULL, which means unmatched left-side rows are gone. If you need to preserve left-side rows, push the condition into the join or handle NULL explicitly.
Common Pitfalls
- Memorizing join names without understanding which unmatched rows survive.
- Using
RIGHT JOINwhen a clearerLEFT JOINwith swapped tables would read better. - Filtering the right table in
WHEREafter aLEFT JOINand accidentally removing unmatched rows. - Assuming
FULL OUTER JOINexists in every database engine. - Forgetting that
NULLjoin keys do not match ordinary equality conditions.
Summary
- '
INNER JOINkeeps only rows that match on both sides.' - '
LEFT JOINkeeps all rows from the left table and fills unmatched right-side columns withNULL.' - '
RIGHT JOINdoes the same thing in the other direction.' - '
FULL OUTER JOINkeeps unmatched rows from both tables.' - The best join type depends on which table's unmatched rows you want to preserve.

