Or copy link
Copy link
MySQL, one of the most popular relational database management systems, is essential for data manipulation and retrieval in various applications. In relational databases, data is typically spread across multiple tables, and JOINS are fundamental tools that allow users to retrieve related data from these tables in a single query.
MySQL JOINS is a powerful tool for combining rows from two or more tables based on a related column between them. In this article, we will explore the different types of MySQL JOINS and provide practical examples to illustrate their usage.
MySQL database supports several types of JOINS to facilitate different ways of combining data from multiple tables:
Build Blazing-fast Websites with MySQL Hosting!
Ultahost’s high-performance pre-configured servers ensure fast MySQL websites and apps with easy setup, scalability, and expert support!
Let’s create two sample tables, employees, and departments to demonstrate how JOINS work. To MySQL JOINS explained, we will use the MySQL Workbench interface.
CREATE TABLE employees ( employee_id INT PRIMARY KEY, first_name VARCHAR(50), last_name VARCHAR(50), department_id INT ); CREATE TABLE departments ( department_id INT PRIMARY KEY, department_name VARCHAR(50) ); INSERT INTO employees (employee_id, first_name, last_name, department_id) VALUES (1, 'John', 'Doe', 1), (2, 'Jane', 'Smith', 2), (3, 'Sam', 'Brown', 1), (4, 'Sara', 'Davis', 3); INSERT INTO departments (department_id, department_name) VALUES (1, 'HR'), (2, 'Finance'), (3, 'Engineering');
The INNER JOIN keyword selects records that have matching values in both tables. Here’s how to use an INNER JOIN to retrieve a list of employees along with their department names:
SELECT employees.first_name, employees.last_name, departments.department_name FROM employees INNER JOIN departments ON employees.department_id = departments.department_id;
The LEFT JOIN keyword returns all records from the left table (employees), and the matched records from the right table (departments). If there is no match, the result is NULL on the right side.
SELECT employees.first_name, employees.last_name, departments.department_name FROM employees LEFT JOIN departments ON employees.department_id = departments.department_id;
The RIGHT JOIN keyword returns all records from the right table (departments) and the matched records from the left table (employees). If there is no match, the result is NULL on the left side.
SELECT employees.first_name, employees.last_name, departments.department_name FROM employees RIGHT JOIN departments ON employees.department_id = departments.department_id;
Also, Read How To Use MySQL Triggers with Examples.
MySQL does not support the FULL JOIN keyword directly. However, you can achieve the same result using a combination of LEFT JOIN and RIGHT JOIN with UNION.
SELECT employees.first_name, employees.last_name, departments.department_name FROM employees LEFT JOIN departments ON employees.department_id = departments.department_id UNION SELECT employees.first_name, employees.last_name, departments.department_name FROM employees RIGHT JOIN departments ON employees.department_id = departments.department_id;
The CROSS JOIN keyword returns the Cartesian product of the two tables, i.e., it returns all possible combinations of rows from the two tables.
SELECT employees.first_name, employees.last_name, departments.department_name FROM employees CROSS JOIN departments;
A SELF JOIN is a regular join but the table is joined with itself. This can be useful for hierarchical data or comparing rows within the same table.
CREATE TABLE employee ( employee_id INT PRIMARY KEY, first_name VARCHAR(50), manager_id INT ); INSERT INTO employee (employee_id, first_name, manager_id) VALUES (1, 'John', NULL), (2, 'Jane', 1), (3, 'Sam', 1), (4, 'Sara', 2); SELECT e1.first_name AS Employee, e2.first_name AS Manager FROM employee e1 LEFT JOIN employee e2 ON e1.manager_id = e2.employee_id;
Above we created another example with an employee table that has a manager_id column to demonstrate a SELF JOIN.
manager_id
MySQL JOINS are powerful tools for retrieving related data from multiple tables. Whether you’re combining rows with INNER JOIN, including all rows from one table with LEFT JOIN or RIGHT JOIN, or performing more complex operations with FULL JOIN, CROSS JOIN, or SELF JOIN, mastering these operations is essential for efficient database management. Practice these examples to gain a deeper understanding and enhance your database querying skills.
We hope this guide has empowered you to use JOINS in MySQL effectively. Consider using Ultahost’s fully managed dedicated server which provides the control and security needed for a smooth database experience. With root access to your server, you can easily install and configure MySQL and set up JOINS to automate your database tasks efficiently.
MySQL JOINS combines data from two or more tables based on a related column.
JOINS helps retrieve related data from multiple tables in a single query.
INNER JOIN returns rows with matching values in both tables.
LEFT JOIN returns all rows from the left table and matching rows from the right table.
RIGHT JOIN returns all rows from the right table and matching rows from the left table.
FULL JOIN returns all rows when there is a match in either table.
Yes, you can combine multiple JOINS to retrieve data from several tables.
In the realm of modern technology, data plays a pivotal...
SQL Server Management Studio (SSMS) is a comprehensive,...
Network authentication is an essential aspect of mainta...
FTP (File Transfer Protocol) is a widely adopted standa...
SSH stands for Secure Shell is a powerful network proto...
Apache Cassandra is a highly scalable distributed NoSQL...
Save my name, email, and website in this browser for the next time I comment.
Δ