In relational databases, data is usually stored across multiple tables to avoid duplication and improve organization. Joins are used to combine rows from two or more tables based on a related column between them. Without joins, it would be impossible to generate meaningful reports that require data from different tables, such as employee details along with department locations or project assignments.
Before understanding joins, it is important to understand the structure of the tables involved.
CREATE TABLE departments (
department_id INT PRIMARY KEY,
department_name VARCHAR(50),
location VARCHAR(50)
);
This table stores department-related information such as department name and location.
CREATE TABLE projects (
project_id INT PRIMARY KEY,
project_name VARCHAR(100),
employee_id INT,
budget DECIMAL(10,2)
);
This table stores project details and links projects to employees using employee_id.
The INNER JOIN returns only those rows where a matching value exists in both tables.
SELECT
e.first_name,
e.last_name,
e.department,
d.location
FROM employees e
INNER JOIN departments d
ON e.department = d.department_name;
department column in employees with department_name in departments.The LEFT JOIN returns all rows from the left table and only matching rows from the right table.
SELECT
e.first_name,
e.last_name,
e.department,
d.location
FROM employees e
LEFT JOIN departments d
ON e.department = d.department_name;
The RIGHT JOIN returns all rows from the right table and matching rows from the left table.
SELECT
d.department_name,
d.location,
e.first_name,
e.last_name
FROM employees e
RIGHT JOIN departments d
ON e.department = d.department_name;
The FULL OUTER JOIN returns all rows from both tables, with NULL values where no match exists.
SELECT
e.first_name,
e.last_name,
d.department_name,
d.location
FROM employees e
FULL OUTER JOIN departments d
ON e.department = d.department_name;
SELECT ...
FROM employees e
LEFT JOIN departments d ON ...
UNION
SELECT ...
FROM employees e
RIGHT JOIN departments d ON ...
The CROSS JOIN creates a Cartesian product, meaning every row from the first table is combined with every row from the second table.
SELECT
e.first_name,
e.last_name,
p.project_name
FROM employees e
CROSS JOIN projects p;
A SELF JOIN is a join where a table is joined with itself.
SELECT
e1.first_name AS employee1,
e2.first_name AS employee2,
e1.department
FROM employees e1
INNER JOIN employees e2
ON e1.department = e2.department
AND e1.employee_id != e2.employee_id;
SQL allows chaining multiple joins in a single query.
SELECT
e.first_name,
e.last_name,
d.department_name,
d.location,
p.project_name,
p.budget
FROM employees e
INNER JOIN departments d
ON e.department = d.department_name
LEFT JOIN projects p
ON e.employee_id = p.employee_id;
| Join Type | What It Returns |
|---|---|
| INNER JOIN | Matching rows in both tables |
| LEFT JOIN | All left + matching right |
| RIGHT JOIN | All right + matching left |
| FULL JOIN | All rows from both tables |
| CROSS JOIN | Every possible combination |
| SELF JOIN | Table joined with itself |