How to Join Tables in SQL
Learn the different types of SQL joins: INNER, LEFT, RIGHT, and FULL joins with examples.
Joins combine rows from two or more tables based on related columns. Here are the main types.
INNER JOIN
Returns only matching rows from both tables:
SELECT employees.name, departments.department_name
FROM employees
INNER JOIN departments ON employees.department_id = departments.id;
Only employees with a matching department will appear in results.
LEFT JOIN (LEFT OUTER JOIN)
Returns all rows from the left table, with matching rows from the right:
SELECT employees.name, departments.department_name
FROM employees
LEFT JOIN departments ON employees.department_id = departments.id;
All employees appear, even those without a department (NULL for department_name).
RIGHT JOIN (RIGHT OUTER JOIN)
Returns all rows from the right table, with matching rows from the left:
SELECT employees.name, departments.department_name
FROM employees
RIGHT JOIN departments ON employees.department_id = departments.id;
All departments appear, even those without employees.
FULL JOIN (FULL OUTER JOIN)
Returns all rows when there’s a match in either table:
SELECT employees.name, departments.department_name
FROM employees
FULL JOIN departments ON employees.department_id = departments.id;
All employees and all departments appear, with NULLs where no match exists.
Multiple Joins
Join more than two tables:
SELECT
employees.name,
departments.department_name,
locations.city
FROM employees
INNER JOIN departments ON employees.department_id = departments.id
INNER JOIN locations ON departments.location_id = locations.id;
Using Table Aliases
Make queries more readable:
SELECT e.name, d.department_name
FROM employees e
INNER JOIN departments d ON e.department_id = d.id;
Self Join
Join a table to itself:
SELECT
e.name AS employee,
m.name AS manager
FROM employees e
LEFT JOIN employees m ON e.manager_id = m.id;
Summary
| Join Type | Returns |
|---|---|
| INNER | Only matching rows |
| LEFT | All left + matching right |
| RIGHT | All right + matching left |
| FULL | All rows from both tables |
Choose the join type based on whether you need all rows from one or both tables.