MySQL joins
Tags: mysql, joins, programming, beginners
Published: 2023-03-27 10:18:20

MySQL joins are used to combine rows from two or more tables based on a related column between them. Joins are essential for retrieving data from multiple tables in a single query. In this article, we will discuss different types of joins in MySQL with examples.

Types of Joins in MySQL:

**1. INNER JOIN**: An inner join returns only the `matching rows` from both tables. It returns the rows that have matching values in both tables.

Syntax: 
```
SELECT table1.column1, table2.column2...
FROM table1
INNER JOIN table2
ON table1.column_name = table2.column_name;

```
Example:

Consider two tables `employees` and `departments`:

Table: `employees`

<table><thead><tr><th>emp_id</th><th>emp_name</th><th>emp_salary</th><th>dept_id</th></tr></thead><tbody><tr><td>101</td><td>John</td><td>50000</td><td>1</td></tr><tr><td>102</td><td>Smith</td><td>60000</td><td>2</td></tr><tr><td>103</td><td>Mary</td><td>55000</td><td>1</td></tr><tr><td>104</td><td>Joe</td><td>45000</td><td>2</td></tr></tbody></table>

Table: `departments`

<table><thead><tr><th>dept_id</th><th>dept_name</th></tr></thead><tbody><tr><td>1</td><td>HR</td></tr><tr><td>2</td><td>IT</td></tr></tbody></table>

To join these tables based on `dept_id` column:

```
SELECT employees.emp_name, departments.dept_name
FROM employees
INNER JOIN departments
ON employees.dept_id = departments.dept_id;
```

Output:

<table><thead><tr><th>emp_name</th><th>dept_name</th></tr></thead><tbody><tr><td>John</td><td>HR</td></tr><tr><td>Smith</td><td>IT</td></tr><tr><td>Mary</td><td>HR</td></tr><tr><td>Joe</td><td>IT</td></tr></tbody></table>


**2. LEFT JOIN**: A left join returns all the rows from the left table and matching rows from the right table. If there is no matching row in the right table, it returns null.

Syntax:
```
SELECT table1.column1, table2.column2...
FROM table1
LEFT JOIN table2
ON table1.column_name = table2.column_name;
```

Example:

Consider two tables `students` and `grades`:

Table: `students`

<table><thead><tr><th>student_id</th><th>student_name</th></tr></thead><tbody><tr><td>101</td><td>John</td></tr><tr><td>102</td><td>Smith</td></tr><tr><td>103</td><td>Mary</td></tr><tr><td>104</td><td>Joe</td></tr></tbody></table>

Table: `grades`

<table><thead><tr><th>student_id</th><th>grade</th></tr></thead><tbody><tr><td>101</td><td>A</td></tr><tr><td>102</td><td>B</td></tr><tr><td>104</td><td>A</td></tr></tbody></table>

To join these tables based on `student_id` column:

```
SELECT students.student_name, grades.grade
FROM students
LEFT JOIN grades
ON students.student_id = grades.student_id;
```

Output:

<table><thead><tr><th>student_name</th><th>grade</th></tr></thead><tbody><tr><td>John</td><td>A</td></tr><tr><td>Smith</td><td>B</td></tr><tr><td>Mary</td><td>NULL</td></tr><tr><td>Joe</td><td>A</td></tr></tbody></table>


**3. RIGHT JOIN**: A right join returns all the rows from the right table and matching rows from the left table. If there is no matching row in the left table, it returns null.

Syntax:
```
SELECT table1.column1, table2.column2...
FROM table1
RIGHT JOIN table2
ON table1.column_name = table2.column_name;
```


Example:

Consider two tables `employees` and `departments`:

Table: `employees`

<table><thead><tr><th>emp_id</th><th>emp_name</th><th>emp_salary</th><th>dept_id</th></tr></thead><tbody><tr><td>101</td><td>John</td><td>50000</td><td>1</td></tr><tr><td>102</td><td>Smith</td><td>60000</td><td>2</td></tr><tr><td>103</td><td>Mary</td><td>55000</td><td>1</td></tr><tr><td></td><td></td><td></td><td></td></tr></tbody></table>