✏️ Explanatory Question

What is the difference between INNER JOIN and OUTER JOIN?

👁 9 Views
📘 Detailed Answer
🟢 Easy
💡

Answer with Explanation

36

What is the difference between INNER JOIN and OUTER JOIN?

Level: Intermediate — Tests whether you truly understand how matched vs unmatched rows are handled.

The core difference lies in how they treat rows that have no match in the other table.

  • INNER JOIN: Returns only the rows that match in both tables. Unmatched rows from either side are completely excluded.
  • OUTER JOIN: Returns matched rows plus unmatched rows from one (or both) tables, filling missing values with NULL.
Types of OUTER JOIN: LEFT OUTER (all left rows), RIGHT OUTER (all right rows), and FULL OUTER (all rows from both — simulated in MySQL). The word "OUTER" is optional; LEFT JOIN = LEFT OUTER JOIN.

Side-by-Side Comparison

Feature INNER JOIN OUTER JOIN
Returns Only matching rows Matching + unmatched rows
Unmatched rows Excluded Included with NULLs
NULLs in result No (from the join) Yes (for missing matches)
Row count Smaller / equal Larger / equal
Sub-types Just one LEFT, RIGHT, FULL

Visual Scenario

Imagine employees where one employee has no department, and departments where one department has no employees:

INNER JOIN

  • Employee with no dept → excluded
  • Dept with no employee → excluded
  • Only fully matched pairs shown

LEFT OUTER JOIN

  • Employee with no dept → included (dept = NULL)
  • All employees appear
  • Great for finding "orphan" records

Quick Example

-- INNER JOIN: only employees that have a matching department
SELECT e.name, d.dept_name
FROM employees e
INNER JOIN departments d ON e.dept_id = d.dept_id;

-- LEFT OUTER JOIN: all employees, dept_name is NULL if unmatched
SELECT e.name, d.dept_name
FROM employees e
LEFT JOIN departments d ON e.dept_id = d.dept_id;

-- Practical use: find employees WITHOUT a department
SELECT e.name
FROM employees e
LEFT JOIN departments d ON e.dept_id = d.dept_id
WHERE d.dept_id IS NULL;
Interviewer tip: The one-liner they want — "INNER JOIN returns only rows matching in both tables, while OUTER JOIN also returns unmatched rows from one or both tables, filling gaps with NULLs."