Skip to content
TechCareer Lab

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:

idnamedepartment_id
1Asha10
2Ravi20
3MeeraNULL
4Karan10

A departments table:

idname
10Engineering
20Finance
30Marketing

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;
employeedepartment
AshaEngineering
RaviFinance
KaranEngineering

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;
employeedepartment
AshaEngineering
RaviFinance
MeeraNULL
KaranEngineering

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;
employeedepartment
AshaEngineering
KaranEngineering
RaviFinance
NULLMarketing

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;
employeedepartment
AshaEngineering
RaviFinance
MeeraNULL
KaranEngineering
NULLMarketing

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 typeReturns
INNER JOINOnly rows that match in both tables
LEFT JOINAll rows from the left table, matches or NULL
RIGHT JOINAll rows from the right table, matches or NULL
FULL OUTER JOINAll 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.