DEV Community

Harsh Mishra
Harsh Mishra

Posted on

Complete Guide to MySQL Joins

The Complete Guide to MySQL Joins: A Comprehensive Explanation

MySQL joins are fundamental for working with relational databases where data is stored across multiple tables. Joins allow you to combine rows from two or more tables based on related columns. In this guide, we will cover all types of MySQL joins, with real-world examples, SQL queries, and sample output.


1. Understanding MySQL Joins

A join in MySQL is used to retrieve data from multiple tables based on a common column. Joins help eliminate redundancy and keep databases normalized.

Types of MySQL Joins

  1. INNER JOIN – Returns only matching records from both tables.
  2. LEFT JOIN (LEFT OUTER JOIN) – Returns all records from the left table and matching records from the right.
  3. RIGHT JOIN (RIGHT OUTER JOIN) – Returns all records from the right table and matching records from the left.
  4. FULL OUTER JOIN – Returns all records when there is a match in either table (not directly supported in MySQL, but can be achieved with UNION).
  5. CROSS JOIN – Returns the Cartesian product of both tables.
  6. SELF JOIN – Joins a table to itself.
  7. NATURAL JOIN – Automatically joins tables based on common column names.

2. Creating Sample Database & Tables

Before we perform joins, let's create a database and define sample tables.

Step 1: Create a Database

CREATE DATABASE CompanyDB;
USE CompanyDB;
Enter fullscreen mode Exit fullscreen mode

Step 2: Create Employees and Departments Tables

CREATE TABLE Departments (
    department_id INT PRIMARY KEY,
    department_name VARCHAR(50)
);

CREATE TABLE Employees (
    employee_id INT PRIMARY KEY,
    name VARCHAR(50),
    department_id INT,
    salary DECIMAL(10,2),
    FOREIGN KEY (department_id) REFERENCES Departments(department_id)
);
Enter fullscreen mode Exit fullscreen mode
  • Departments contains department details.
  • Employees stores employee information and references Departments.

3. INNER JOIN (Default Join)

Definition

INNER JOIN returns only the matching rows between both tables.

SQL Query

SELECT Employees.employee_id, Employees.name, Employees.salary, Departments.department_name
FROM Employees
INNER JOIN Departments ON Employees.department_id = Departments.department_id;
Enter fullscreen mode Exit fullscreen mode

Example Output

employee_id name salary department_name
101 Alice 5000.00 HR
102 Bob 6000.00 IT
103 Charlie 7000.00 Finance

🔹 Explanation: Only employees with a valid department_id in the Departments table are included.


4. LEFT JOIN (LEFT OUTER JOIN)

Definition

LEFT JOIN returns all rows from the left table (Employees) and matching rows from the right (Departments). If no match is found, NULL is returned.

SQL Query

SELECT Employees.employee_id, Employees.name, Employees.salary, Departments.department_name
FROM Employees
LEFT JOIN Departments ON Employees.department_id = Departments.department_id;
Enter fullscreen mode Exit fullscreen mode

Example Output

employee_id name salary department_name
101 Alice 5000.00 HR
102 Bob 6000.00 IT
103 Charlie 7000.00 Finance
104 David 4500.00 NULL

🔹 Explanation:

  • David does not have a department, so NULL appears for department_name.

5. RIGHT JOIN (RIGHT OUTER JOIN)

Definition

RIGHT JOIN returns all rows from the right table (Departments) and matching rows from the left (Employees). If no match is found, NULL is returned.

SQL Query

SELECT Employees.employee_id, Employees.name, Employees.salary, Departments.department_name
FROM Employees
RIGHT JOIN Departments ON Employees.department_id = Departments.department_id;
Enter fullscreen mode Exit fullscreen mode

Example Output

employee_id name salary department_name
101 Alice 5000.00 HR
102 Bob 6000.00 IT
103 Charlie 7000.00 Finance
NULL NULL NULL Marketing

🔹 Explanation:

  • The Marketing department has no employees, so NULL appears for employee_id, name, and salary.

6. FULL OUTER JOIN (Using UNION)

Definition

MySQL does not support FULL OUTER JOIN directly, but we can achieve it using UNION.

SQL Query

SELECT Employees.employee_id, Employees.name, Employees.salary, Departments.department_name
FROM Employees
LEFT JOIN Departments ON Employees.department_id = Departments.department_id
UNION
SELECT Employees.employee_id, Employees.name, Employees.salary, Departments.department_name
FROM Employees
RIGHT JOIN Departments ON Employees.department_id = Departments.department_id;
Enter fullscreen mode Exit fullscreen mode

Example Output

employee_id name salary department_name
101 Alice 5000.00 HR
102 Bob 6000.00 IT
103 Charlie 7000.00 Finance
104 David 4500.00 NULL
NULL NULL NULL Marketing

7. CROSS JOIN (Cartesian Product)

Definition

CROSS JOIN returns the Cartesian product of both tables, meaning every row in one table is matched with every row in the other.

SQL Query

SELECT Employees.name, Departments.department_name
FROM Employees
CROSS JOIN Departments;
Enter fullscreen mode Exit fullscreen mode

Example Output

name department_name
Alice HR
Alice IT
Alice Finance
Alice Marketing
Bob HR
Bob IT
Bob Finance
Bob Marketing

🔹 Explanation: If Employees has 4 rows and Departments has 4 rows, the result will have 4 × 4 = 16 rows.


8. SELF JOIN

Definition

A SELF JOIN is a join where a table is joined with itself.

Example Use Case: Employee Manager Relationship

SELECT A.employee_id AS Employee_ID, A.name AS Employee_Name, B.name AS Manager_Name
FROM Employees A
LEFT JOIN Employees B ON A.manager_id = B.employee_id;
Enter fullscreen mode Exit fullscreen mode

🔹 Explanation: We join Employees to itself to find each employee’s manager.


9. NATURAL JOIN

Definition

NATURAL JOIN automatically joins tables based on common column names.

SQL Query

SELECT * FROM Employees NATURAL JOIN Departments;
Enter fullscreen mode Exit fullscreen mode

🔹 Explanation: If both tables have a column with the same name, they are joined automatically.


Summary of MySQL Join Syntax

Joins in MySQL allow you to retrieve data from multiple tables based on related columns. Below is a summary of different types of joins along with their syntax and important considerations.


1. INNER JOIN (Default Join)

  • Retrieves only matching rows from both tables based on the specified condition.
  • ON is required.
  • Syntax:
  SELECT column_names
  FROM table1
  INNER JOIN table2 ON table1.common_column = table2.common_column;
Enter fullscreen mode Exit fullscreen mode

2. LEFT JOIN (LEFT OUTER JOIN)

  • Retrieves all records from the left table and matching records from the right table. If there is no match, NULL values are returned for the right table’s columns.
  • ON is required.
  • Syntax:
  SELECT column_names
  FROM table1
  LEFT JOIN table2 ON table1.common_column = table2.common_column;
Enter fullscreen mode Exit fullscreen mode

3. RIGHT JOIN (RIGHT OUTER JOIN)

  • Retrieves all records from the right table and matching records from the left table. If there is no match, NULL values are returned for the left table’s columns.
  • ON is required.
  • Syntax:
  SELECT column_names
  FROM table1
  RIGHT JOIN table2 ON table1.common_column = table2.common_column;
Enter fullscreen mode Exit fullscreen mode

4. FULL OUTER JOIN (Simulated Using UNION in MySQL)

  • Retrieves all records from both tables. If a record does not have a match in the other table, NULL values are returned for the missing columns.
  • MySQL does not support FULL OUTER JOIN directly, so it is simulated using UNION.
  • ON is required.
  • Syntax:
  SELECT column_names
  FROM table1
  LEFT JOIN table2 ON table1.common_column = table2.common_column
  UNION
  SELECT column_names
  FROM table1
  RIGHT JOIN table2 ON table1.common_column = table2.common_column;
Enter fullscreen mode Exit fullscreen mode

5. CROSS JOIN (Cartesian Product)

  • Returns all possible combinations of rows from both tables (Cartesian product).
  • ON is not used and cannot be used.
  • Syntax:
  SELECT column_names
  FROM table1
  CROSS JOIN table2;
Enter fullscreen mode Exit fullscreen mode
  • Alternative Using JOIN Without ON:
  SELECT column_names
  FROM table1, table2;
Enter fullscreen mode Exit fullscreen mode

(Equivalent to CROSS JOIN)


6. SELF JOIN (Joining a Table with Itself)

  • Joins a table to itself, treating it as if it were two different tables.
  • ON is required.
  • Syntax:
  SELECT A.column_name, B.column_name
  FROM table_name A
  JOIN table_name B ON A.common_column = B.common_column;
Enter fullscreen mode Exit fullscreen mode

7. NATURAL JOIN (Automatic Join Based on Common Column Names)

  • Automatically joins tables based on columns with the same name.
  • ON is not used and cannot be used.
  • Syntax:
  SELECT column_names
  FROM table1
  NATURAL JOIN table2;
Enter fullscreen mode Exit fullscreen mode

Key Considerations

  1. ON is required for INNER JOIN, LEFT JOIN, RIGHT JOIN, FULL OUTER JOIN, and SELF JOIN.
  2. ON is not used and cannot be used with NATURAL JOIN and CROSS JOIN.
  3. FULL OUTER JOIN is not directly supported in MySQL and must be simulated using UNION.
  4. CROSS JOIN does not require an ON condition because it produces a Cartesian product.

Conclusion

  • INNER JOIN → Returns only matching rows.
  • LEFT JOIN → Returns all left table rows + matched right table rows.
  • RIGHT JOIN → Returns all right table rows + matched left table rows.
  • FULL JOIN → Returns all records from both tables (with UNION).
  • CROSS JOIN → Returns Cartesian product (all possible combinations).
  • SELF JOIN → Joins a table with itself.
  • NATURAL JOIN → Auto-matches based on common column names.

Top comments (0)