What is the difference between INNER JOIN, LEFT JOIN and FULL JOIN?

INNER, LEFT, RIGHT and FULL OUTER JOIN explained with two small tables and their exact output, plus how to find rows with no match.

0 0
Read Time:1 Minute, 48 Second

The clearest answer is two tiny tables and the rows each join returns. Draw these on the whiteboard.

Employee                    Department
id  name    dept_id         id  name
1   Asha    10              10  Sales
2   Ravi    20              20  IT
3   Meera   NULL            30  HR

INNER JOIN: only matching rows

SELECT e.name, d.name AS dept
FROM Employee e
INNER JOIN Department d ON e.dept_id = d.id;

Asha   Sales
Ravi   IT

Meera has no department and HR has no employee, so both are left out.

LEFT JOIN: every row from the left table

SELECT e.name, d.name AS dept
FROM Employee e
LEFT JOIN Department d ON e.dept_id = d.id;

Asha   Sales
Ravi   IT
Meera  NULL

All employees appear; the department columns are NULL where there is no match. RIGHT JOIN is the mirror image (every department, so HR appears with a NULL employee). Most teams just swap the table order and use LEFT JOIN.

FULL OUTER JOIN: every row from both

SELECT e.name, d.name AS dept
FROM Employee e
FULL OUTER JOIN Department d ON e.dept_id = d.id;

Asha   Sales
Ravi   IT
Meera  NULL
NULL   HR

MySQL has no FULL JOIN. Combine a LEFT JOIN and a RIGHT JOIN with UNION.

Finding rows with no match

-- Departments with no employees
SELECT d.name
FROM Department d
LEFT JOIN Employee e ON e.dept_id = d.id
WHERE e.id IS NULL;

A common trap

A condition on the right table in WHERE turns a LEFT JOIN back into an INNER JOIN, because NULL rows fail the condition. Put it in the ON clause if you want to keep unmatched rows: LEFT JOIN Orders o ON o.cust_id = c.id AND o.year = 2026.

Follow-ups

  • What is a CROSS JOIN and a SELF JOIN?
  • Write a query for employees who earn more than their manager.
  • JOIN versus a subquery: which is faster?

This answer was written by Debugger’s Space as practice material. It is asked in most Indian IT services interviews (TCS, Infosys, Wipro, HCLTech, Tech Mahindra, LTIMindtree, Accenture, Cognizant, Capgemini, IBM). See each company’s hiring steps and practice questions on Debugger’s Space.

Previous post What is the difference between checked and unchecked exceptions?
Next post What is normalisation? Explain 1NF, 2NF and 3NF.
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Close
0
Would love your thoughts, please comment.x
()
x