SQL JOINs Explained with Practical Examples
Understand INNER, LEFT, RIGHT and FULL OUTER JOINs using two small tables, see exactly which rows each one returns, and avoid the most common JOIN mistake.
- Author
- TechCareer Lab Team
- Published
- Reading time
- 3 min read
On this page
JOINs combine rows from two or more tables based on a related column. They are one of the most used, and most misunderstood, parts of SQL. This tutorial uses two small tables so you can see exactly which rows each type of JOIN returns.
The example tables
An employees table, where each employee may belong to a department:
| id | name | department_id |
|---|---|---|
| 1 | Asha | 10 |
| 2 | Ravi | 20 |
| 3 | Meera | NULL |
| 4 | Karan | 10 |
A departments table:
| id | name |
|---|---|
| 10 | Engineering |
| 20 | Finance |
| 30 | Marketing |
Notice two things: Meera has no department, and nobody works in Marketing. These "unmatched" rows are what make the JOIN types behave differently.
INNER JOIN
An INNER JOIN returns only rows that have a match in both tables.
SELECT e.name AS employee, d.name AS department
FROM employees e
INNER JOIN departments d ON e.department_id = d.id;| employee | department |
|---|---|
| Asha | Engineering |
| Ravi | Finance |
| Karan | Engineering |
Meera (no department) and Marketing (no employees) are both excluded.
LEFT JOIN
A LEFT JOIN (also written LEFT OUTER JOIN) returns every row from the left table, plus matching rows from the right table. Where there is no match, the right-hand columns are NULL.
SELECT e.name AS employee, d.name AS department
FROM employees e
LEFT JOIN departments d ON e.department_id = d.id;| employee | department |
|---|---|
| Asha | Engineering |
| Ravi | Finance |
| Meera | NULL |
| Karan | Engineering |
Use a LEFT JOIN when you need all records from one table even if related data is missing, for example "all employees, with their department if they have one".
RIGHT JOIN
A RIGHT JOIN is the mirror image: every row from the right table, plus matches from the left.
SELECT e.name AS employee, d.name AS department
FROM employees e
RIGHT JOIN departments d ON e.department_id = d.id;| employee | department |
|---|---|
| Asha | Engineering |
| Karan | Engineering |
| Ravi | Finance |
| NULL | Marketing |
Any RIGHT JOIN can be rewritten as a LEFT JOIN by swapping the table order. Many teams prefer to always use LEFT JOIN for consistency.
FULL OUTER JOIN
A FULL OUTER JOIN returns all rows from both tables, matching them where possible.
SELECT e.name AS employee, d.name AS department
FROM employees e
FULL OUTER JOIN departments d ON e.department_id = d.id;| employee | department |
|---|---|
| Asha | Engineering |
| Ravi | Finance |
| Meera | NULL |
| Karan | Engineering |
| NULL | Marketing |
The most common JOIN mistake
Suppose you want all employees, showing the department name only for Engineering. A first attempt often looks like this:
SELECT e.name, d.name AS department
FROM employees e
LEFT JOIN departments d ON e.department_id = d.id
WHERE d.name = 'Engineering';This returns only Asha and Karan. The WHERE clause runs after the join and removes every row where d.name is not 'Engineering', including the NULL rows the LEFT JOIN kept. The query now behaves like an INNER JOIN.
To keep all employees, move the condition into the ON clause:
SELECT e.name, d.name AS department
FROM employees e
LEFT JOIN departments d
ON e.department_id = d.id
AND d.name = 'Engineering';Now every employee appears, and department is filled in only for Engineering staff.
Summary
| JOIN type | Returns |
|---|---|
| INNER JOIN | Only rows that match in both tables |
| LEFT JOIN | All rows from the left table, matches or NULL |
| RIGHT JOIN | All rows from the right table, matches or NULL |
| FULL OUTER JOIN | All rows from both tables, matched where possible |
The best way to remember these is to practise. Recreate the two tables above in a free database such as PostgreSQL or SQLite and run each query yourself.