![]() |
VOOZH | about |
MySQL JOIN is a fundamental feature that combines rows from two or more tables based on a related column between them. It allows for efficient dataretrieval by enabling the extraction of related information from multiple tables in a single query.
In this article, We will learn about MySQL JOIN by understanding various types of JOINS with the help of examples and so on.
SELECT column_names
FROM table1
INNER JOIN table2
ON table1.common_column = table2.common_column;
where,
We will Create tables and insert data to demonstrate each type of MySQL JOIN. In this case, we shall consider two tables: employees and departments.
CREATE TABLE employees (
employee_id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(100),
department_id INT
);
CREATE TABLE departments (
department_id INT AUTO_INCREMENT PRIMARY KEY,
department_name VARCHAR(100)
);
INSERT INTO employees (name, department_id) VALUES
('Alice', 1),
('Bob', 2),
('Charlie', 1),
('David', 3),
('Eve', NULL);
INSERT INTO departments (department_id, department_name) VALUES
(1, 'HR'),
(2, 'Engineering'),
(3, 'Marketing'),
(4, 'Finance');
Tables:
employee_id | name | department_id |
|---|---|---|
1 | Alice | 1 |
2 | Bob | 2 |
3 | Charlie | 1 |
4 | David | 3 |
5 | Eve | NULL |
Departments Table:
department_id | department_name |
|---|---|
1 | HR |
2 | Engineering |
3 | Marketing |
4 | Finance |
Above is the table created for performing the the queries of the JOIN
It Returns records that have matching values in both tables.
SELECT employees.name, departments.department_name
FROM employees
INNER JOIN departments
ON employees.department_id = departments.department_id;
Output:
name | department_name |
|---|---|
Alice | HR |
Bob | Engineering |
Charlie | HR |
David | Marketing |
Explanation:
It returns all records from the Left table and matched records from the Right table. If there is no match, then NULL values are returned for Right table columns.
SELECT employees.name, departments.department_name
FROM employees
LEFT JOIN departments
ON employees.department_id = departments.department_id;
Output:
name | department_name |
|---|---|
Alice | HR |
Bob | Engineering |
Charlie | HR |
David | Marketing |
Eve | NULL |
It Returns all the rows from the right table and the matched rows from the left table. NULL values will be returned for columns from the left table when there are no matches.
SELECT employees.name, departments.department_name
FROM employees
RIGHT JOIN departments
ON employees.department_id = departments.department_id;
Output:
name | department_name |
|---|---|
Alice | HR |
Bob | Engineering |
Charlie | HR |
David | Marketing |
NULL | Finance |
Explanation:
It Returns all records when there is a match in either the left or the right table. In case of no match, NULL values are returned for columns that have no 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;
Output:
name | department_name |
|---|---|
Alice | HR |
Bob | Engineering |
Charlie | HR |
David | Marketing |
Eve | NULL |
NULL | Finance |
Explanation
It Returns the Cartesian product of two tables. Matches every row of one table with every row of another table.
SELECT employees.name, departments.department_name
FROM employees
CROSS JOIN departments;
Output:
name | department_name |
|---|---|
Alice | HR |
Alice | Engineering |
Alice | Marketing |
Alice | Finance |
Bob | HR |
Bob | Engineering |
Bob | Marketing |
Bob | Finance |
Charlie | HR |
Charlie | Engineering |
Charlie | Marketing |
Charlie | Finance |
David | HR |
David | Engineering |
David | Marketing |
David | Finance |
Eve | HR |
Eve | Engineering |
Eve | Marketing |
Eve | Finance |
Explanation:
A self join is a type of join in which a table is joined to itself. This is useful when you need to compare rows within the same table, such as relating employees to their managers in an organizational hierarchy.
Consider an employees table that needs to represent employees and their managers. To achieve this, we can introduce a new column called manager_id in the employees table to store the ID of each employee's manager. After updating the table, we'll perform a self join to associate each employee with their manager.
manager_id ColumnALTER TABLE employees ADD COLUMN manager_id INT;UPDATE employees SET manager_id = 3 WHERE employee_id = 1;
UPDATE employees SET manager_id = 3 WHERE employee_id = 2;
UPDATE employees SET manager_id = 4 WHERE employee_id = 3;
Output:
| employee_id | name |
|---|---|
| 1 | Alice |
| 2 | Bob |
| 3 | Charlie |
| 4 | David |
SELECT a.name AS employee, b.name AS manager
FROM employees a, employees b
WHERE a.manager_id = b.employee_id;
Output:
employee | manager |
|---|---|
Alice | Charlie |
Bob | Charlie |
Charlie | David |
Explanation:
In summary, MySQL JOINs provide a versatile and efficient way to retrieve and analyze data across multiple tables. By utilizing different types of JOINs, such as INNER JOIN, LEFT JOIN, RIGHT JOIN, FULL JOIN, CROSS JOIN, and SELF JOIN, you can achieve a wide range of data retrieval scenarios, from simple to complex.