MySQL joins
In MySQL, joins are used to retrieve data from multiple tables based on a related column between them. Joins allow you to combine rows from two or more tables into a single result set. There are several types of joins in MySQL, each serving different purposes depending on the nature of the relationship between the tables.
Types of Joins
- INNER JOIN
- LEFT JOIN (or LEFT OUTER JOIN)
- RIGHT JOIN (or RIGHT OUTER JOIN)
- FULL JOIN (or FULL OUTER JOIN)
- CROSS JOIN
- SELF JOIN
1. INNER JOIN
The INNER JOIN
returns rows when there is a match in both joined tables. It is the most common type of join.
Syntax:
SELECT columns
FROM table1
INNER JOIN table2
ON table1.common_column = table2.common_column;
Example:
Retrieve all employees along with their department names:
SELECT employees.name, departments.department_name
FROM employees
INNER JOIN departments
ON employees.department_id = departments.department_id;
This query returns only the rows where there is a matching department_id
in both tables.
2. LEFT JOIN (or LEFT OUTER JOIN)
The LEFT JOIN
returns all rows from the left table and the matched rows from the right table. If there is no match, the result is NULL
on the right side.
Syntax:
SELECT columns
FROM table1
LEFT JOIN table2
ON table1.common_column = table2.common_column;
Example:
Retrieve all employees and their department names, including employees who do not belong to any department:
SELECT employees.name, departments.department_name
FROM employees
LEFT JOIN departments
ON employees.department_id = departments.department_id;
This query includes all employees, even those without a matching department, showing NULL
for departments where there is no match.
3. RIGHT JOIN (or RIGHT OUTER JOIN)
The RIGHT JOIN
returns all rows from the right table and the matched rows from the left table. If there is no match, the result is NULL
on the left side.
Syntax:
SELECT columns
FROM table1
RIGHT JOIN table2
ON table1.common_column = table2.common_column;
Example:
Retrieve all departments and the employees in each department, including departments with no employees:
SELECT employees.name, departments.department_name
FROM employees
RIGHT JOIN departments
ON employees.department_id = departments.department_id;
This query includes all departments, even those without employees, showing NULL
for employees where there is no match.
4. FULL JOIN (or FULL OUTER JOIN)
The FULL JOIN
returns all rows when there is a match in either left or right table. If there is no match, the result is NULL
in the columns of the table where there is no match.
Syntax:
SELECT columns
FROM table1
FULL JOIN table2
ON table1.common_column = table2.common_column;
Note: MySQL does not directly support FULL JOIN
. To achieve a full outer join, you can use a combination of LEFT JOIN
and RIGHT JOIN
with UNION
.
Example:
Retrieve all employees and departments, including those without a match in either table:
SELECT employees.name, departments.department_name
FROM employees
LEFT JOIN departments
ON employees.department_id = departments.department_id
UNION
SELECT employees.name, departments.department_name
FROM employees
RIGHT JOIN departments
ON employees.department_id = departments.department_id;
5. CROSS JOIN
The CROSS JOIN
returns the Cartesian product of both tables, meaning it joins every row from the first table with every row from the second table.
Syntax:
SELECT columns
FROM table1
CROSS JOIN table2;
Example:
Retrieve all possible combinations of products and suppliers:
SELECT products.product_name, suppliers.supplier_name
FROM products
CROSS JOIN suppliers;
This query returns every combination of product_name
and supplier_name
.
6. SELF JOIN
A SELF JOIN
is a join where a table is joined with itself. This can be useful for querying hierarchical data or comparing rows within the same table.
Syntax:
SELECT a.columns, b.columns
FROM table a
INNER JOIN table b
ON a.common_column = b.common_column;
Example:
Find pairs of employees who have the same manager:
SELECT e1.name AS employee1, e2.name AS employee2
FROM employees e1
INNER JOIN employees e2
ON e1.manager_id = e2.manager_id
WHERE e1.employee_id <> e2.employee_id;
Key Points
- Column Matching: Ensure that the columns you join on have compatible data types.
- Performance: Joins can affect performance, especially with large tables. Indexing the columns used in the join can help.
- Alias Usage: Use table aliases to simplify query writing and improve readability, especially with complex joins.