SQL Joins – INNER JOIN, LEFT JOIN, RIGHT JOIN & FULL OUTER JOIN with Examples (2026-27)
A SQL JOIN is used to combine rows from two or more tables based on a related column — usually a foreign key in one table matching a primary key in another. Real-world databases rarely store all data in one table — employee names are in one table, department details in another, salary in a third. JOINs let you pull all that data together in a single query. In this beginner-friendly guide you will learn all six SQL JOIN types — INNER JOIN, LEFT JOIN, RIGHT JOIN, FULL OUTER JOIN, CROSS JOIN, and SELF JOIN — with clear syntax, real examples, a comparison table, and the most common mistakes to avoid.
✅ What is a SQL JOIN?
✅ A SQL JOIN combines rows from two or more tables based on a related column shared between them.
✅ The related column is typically a foreign key in one table and a primary key in another.
✅ Without JOINs, you would need to run multiple separate queries and combine results manually — JOIN does it all in one step.
✅ All SQL JOIN Types at a Glance
✅ Sample Tables Used in All Examples
| EmpID | Name | DeptID |
|---|---|---|
| 1 | ANNI | 10 |
| 2 | POOJA | 20 |
| 3 | RAJ | 30 |
| 4 | SUJAN | 40 |
| 5 | MEENA | NULL |
| DeptID | DeptName |
|---|---|
| 10 | IT |
| 20 | HR |
| 30 | Sales |
| 50 | Finance |
✅ INNER JOIN – Only Matching Rows
INNER JOIN returns only rows where there is a matching value in both tables. Rows with no match are excluded.
✅ SELECT Query:-
SELECT E.EmpID, E.Name, D.DeptName
FROM Employees E
INNER JOIN Department D ON E.DeptID = D.DeptID;
✅ Result:-
| EmpID | Name | DeptName |
|---|---|---|
| 1 | ANNI | IT |
| 2 | POOJA | HR |
| 3 | RAJ | Sales |
✅ LEFT JOIN – All Left Table + Matching Right
LEFT JOIN returns ALL rows from the left table (Employees), plus matching rows from the right table (Department). Where no match exists, the right table columns show NULL.
✅ SELECT Query:-
SELECT E.EmpID, E.Name, D.DeptName
FROM Employees E
LEFT JOIN Department D ON E.DeptID = D.DeptID;
✅ Result:-
| EmpID | Name | DeptName |
|---|---|---|
| 1 | ANNI | IT |
| 2 | POOJA | HR |
| 3 | RAJ | Sales |
| 4 | SUJAN | NULL |
| 5 | MEENA | NULL |
✅ RIGHT JOIN – All Right Table + Matching Left
RIGHT JOIN returns ALL rows from the right table (Department), plus matching rows from the left table (Employees). Where no match exists, the left table columns show NULL.
✅ SELECT Query:-
SELECT E.EmpID, E.Name, D.DeptName
FROM Employees E
RIGHT JOIN Department D ON E.DeptID = D.DeptID;
✅ Result:-
| EmpID | Name | DeptName |
|---|---|---|
| 1 | ANNI | IT |
| 2 | POOJA | HR |
| 3 | RAJ | Sales |
| NULL | NULL | Finance |
✅ FULL OUTER JOIN – All Rows from Both Tables
FULL OUTER JOIN returns ALL rows from both tables. Matching rows are combined; non-matching rows from either side show NULL for the other table's columns.
✅ SELECT Query:-
SELECT E.EmpID, E.Name, D.DeptName
FROM Employees E
FULL OUTER JOIN Department D ON E.DeptID = D.DeptID;
✅ Result:-
| EmpID | Name | DeptName |
|---|---|---|
| 1 | ANNI | IT |
| 2 | POOJA | HR |
| 3 | RAJ | Sales |
| 4 | SUJAN | NULL |
| 5 | MEENA | NULL |
| NULL | NULL | Finance |
UNION of a LEFT JOIN and a RIGHT JOIN:SELECT ... FROM Employees LEFT JOIN Department ... UNION SELECT ... FROM Employees RIGHT JOIN Department ...
✅ CROSS JOIN – Every Possible Combination
CROSS JOIN returns the Cartesian product — every row from the left table paired with every row from the right table. No ON condition is used.
✅ SELECT Query:-
SELECT E.Name, D.DeptName
FROM Employees E
CROSS JOIN Department D;
✅ SELF JOIN – Table Joined with Itself
A SELF JOIN joins a table with itself using table aliases. Common use case: finding employee-manager relationships in the same table.
✅ Extended Employees Table with ManagerID:-
| EmpID | Name | ManagerID |
|---|---|---|
| 1 | ANNI | NULL |
| 2 | POOJA | 1 |
| 3 | RAJ | 1 |
| 4 | SUJAN | 2 |
| 5 | MEENA | 2 |
✅ SELECT Query — Show Each Employee and Their Manager Name:-
SELECT E.Name AS Employee, M.Name AS Manager
FROM Employees E
LEFT JOIN Employees M ON E.ManagerID = M.EmpID;
✅ Result:-
| Employee | Manager |
|---|---|
| ANNI | NULL |
| POOJA | ANNI |
| RAJ | ANNI |
| SUJAN | POOJA |
| MEENA | POOJA |
✅ JOIN Type Comparison Table
| JOIN Type | Returns Left Unmatched? | Returns Right Unmatched? | Only Matches? | Use Case |
|---|---|---|---|---|
| INNER JOIN | ❌ No | ❌ No | ✅ Yes | Common data in both tables |
| LEFT JOIN | ✅ Yes (NULL right) | ❌ No | ✅ Yes | All left + matches |
| RIGHT JOIN | ❌ No | ✅ Yes (NULL left) | ✅ Yes | All right + matches |
| FULL OUTER JOIN | ✅ Yes | ✅ Yes | ✅ Yes | All rows both tables |
| CROSS JOIN | N/A | N/A | N/A | Cartesian product |
| SELF JOIN | Depends on JOIN type used | Depends on JOIN type used | Depends | Hierarchical / self-referencing data |
✅ Key Points to Remember
✅ INNER JOIN — most used; returns only rows with a match in both tables.
✅ LEFT JOIN — use when you want all records from the left table regardless of matches.
✅ RIGHT JOIN — mirror of LEFT JOIN; all records from the right table.
✅ FULL OUTER JOIN — all rows from both; NULL fills unmatched sides.
✅ CROSS JOIN — every combination; result size = rows left × rows right.
✅ SELF JOIN — join a table with itself; always use aliases (E, M, etc.).
✅ Always use aliases — makes multi-table queries readable: FROM Employees E JOIN Department D ON E.DeptID = D.DeptID.
✅ MySQL workaround — MySQL does not support FULL OUTER JOIN; use UNION of LEFT and RIGHT JOIN instead.
✅ Common SQL JOIN Mistakes to Avoid
Wrong:
SELECT * FROM Employees JOIN Department; — this is a CROSS JOIN producing millions of rows.Correct:
SELECT * FROM Employees JOIN Department ON Employees.DeptID = Department.DeptID;
Always join on the column that logically connects the tables — usually the foreign key in one table and primary key in the other. Joining on unrelated columns produces meaningless results.
Wrong:
SELECT DeptID FROM Employees JOIN Department ON ... — SQL doesn't know which table's DeptID you want.Correct:
SELECT E.DeptID FROM Employees E JOIN Department D ON E.DeptID = D.DeptID;
MySQL does not support FULL OUTER JOIN. Attempting it gives a syntax error. Use
LEFT JOIN UNION RIGHT JOIN to simulate the same result in MySQL.
✅ Frequently Asked Questions (FAQ)
SELECT * FROM Employees E JOIN Department D ON E.DeptID = D.DeptID JOIN Location L ON D.LocID = L.LocID; Each JOIN adds another table to the result set.