A Guide to MySQL JOINS
Tags: guide, mysql
Published: 2021-09-07 21:57:24

# Introduction
**In a single query, JOINS are utilized to get data from several tables.** In MySQL, JOINS aggregate data from several tables and get it as a single result. Each table in a relational database includes unique or common data, and each table is connected logically. JOINS are used to obtain data from tables that share a common field. 
​
## **What Are JOINS in MySQL?**
In MySQL, the `JOIN` statement is a technique of connecting data between several tables in a database based on the values of common fields in those tables. The same column name and data type are generally present in the tables being linked as common values. The join key or common key refers to the shared columns. `SELECT`, `UPDATE`, and `DELETE` commands can all employ JOINS.
​
### Getting Started with JOINs
The MySQL JOIN type specifies how two tables in a query are linked. INNER JOIN, OUTER JOIN, and CROSS JOIN are the three types of JOIN clauses supported by MySQL. LEFT JOINs and RIGHT JOINs are two different types of OUTER JOINS. To show how the JOINs operate more visually, we need to [create a new schema](https://docs.arctype.com/managing-tables/creating-a-table) and insert some sample data.
​
```
CREATE TABLE Users (
    UserID int,
    UserName varchar(255),
    Password varchar(255),
    isActive boolean
);

CREATE TABLE Userprofile (
    ProfileID int,
    LastName varchar(255),
    FirstName varchar(255),
    Email varchar(255),
    Phone varchar(255)
);
```

The next thing we will do is insert some data into it. You can insert as many users into the tables as you like.
​
```
INSERT INTO Users
	(UserID, UserName,Password, isActive)
VALUES
	(1,'krofax','krofax1234', true);

INSERT INTO userprofile
	(profileid, lastname, firstname, email, phone)
VALUES
	(1,'Ada', 'George', 'adageorge@gmail.com','1290003456');
```

## MySQL INNER JOIN Clause
Only common matched records are retrieved using INNER JOINS. The INNER JOIN clause restricts records retrieval from Table A and Table B to those that satisfy the join requirement. It is the most often used JOIN type. Examine the Venn diagram below to acquire a better understanding of INNER JOINS.

![](https://blog.devart.com/wp-content/uploads/2021/04/inner-join-2.png)

Here is the syntax for MySQL INNER JOIN:
​
```
SELECT 
	columns
FROM 
	tableA 
INNER JOIN tableB
	ON tableA.column = tableB.column;
```
​
![](https://paper-attachments.dropbox.com/s_7089066EA8FFE66D567C974E4D37410CFA274CB2DA52C7F834784CA7B0DC0B2D_1628959854410_Screen+Shot+2021-08-14+at+5.50.25+PM.png)


## MySQL OUTER JOINs
OUTER JOINS, in contrast to INNER JOINS, yield non-matching records as well as matching rows. If rows in a connected table don't match, the NULL values will be shown.MySQL has two different forms of OUTER JOINS: MySQL LEFT JOIN and MySQL RIGHT JOIN. Let's look at each of them in more detail.
​
### MySQL LEFT JOIN Clause
LEFT JOINS allow you to get all entries from Table A and those from Table B that meet the join criteria. NULL values are displayed for records from Table A that do not fit the criteria. Examine the Venn diagram below to have a better understanding of LEFT JOINS.
![](https://blog.devart.com/wp-content/uploads/2021/04/left-join.png)

Here is the syntax for MySQL LEFT JOIN:
​
```
SELECT 
	columns
FROM 
	tableA
LEFT JOIN tableB
	ON tableA.column = tableB.column;
```
​
![](https://paper-attachments.dropbox.com/s_7089066EA8FFE66D567C974E4D37410CFA274CB2DA52C7F834784CA7B0DC0B2D_1629241908030_Screen+Shot+2021-08-18+at+12.11.05+AM.png)

**Note:** The `LEFT JOIN` keyword returns all records from the left table (Customers), even if there are no matches in the right table (Orders).
​
### MySQL RIGHT JOIN Clause
As a result, RIGHT JOINS allow you to get all entries from Table B and those from Table A that meet the join criteria. The NULL values are presented for the records from Table B that do not fit the criteria. For a better understanding of RIGHT JOINS, see the Venn diagram below.

![](https://blog.devart.com/wp-content/uploads/2021/04/right-join.png)

Here is the syntax for MySQL RIGHT JOIN:
​
```
SELECT 
	columns
FROM 
	tableA
RIGHT JOIN tableB
	ON tableA.column = tableB.column;
```

![](https://paper-attachments.dropbox.com/s_7089066EA8FFE66D567C974E4D37410CFA274CB2DA52C7F834784CA7B0DC0B2D_1629242766639_Screen+Shot+2021-08-18+at+12.23.24+AM.png)

**Note:** The `RIGHT JOIN` keyword returns all records from the right table (Employees), even if there are no matches in the left table (Orders).
​
## MySQL CROSS JOIN Clause
The MySQL CROSS JOIN, commonly known as a cartesian join, returns all possible row combinations from each table. If no extra condition is provided, the result set is obtained by multiplying each row of table A with all rows in table B.Examine the Venn diagram below to learn more about CROSS JOINS.

![](https://blog.devart.com/wp-content/uploads/2021/04/left-joincross-join.png)

When do you think you'll need that kind of JOIN? Assume, for example, that you're tasked with finding all possible combinations of a product and a color. In that scenario, a CROSS JOIN would be quite useful.
​

**Note:** `CROSS JOIN` might result in quite huge result sets!
​
Here is the syntax for MySQL CROSS JOIN:
​
```
SELECT 
	columns
FROM 
	tableA
CROSS JOIN tableB;
```

![](https://paper-attachments.dropbox.com/s_7089066EA8FFE66D567C974E4D37410CFA274CB2DA52C7F834784CA7B0DC0B2D_1629270425407_Screen+Shot+2021-08-18+at+8.06.43+AM.png)

## Tips for JOINing
In MySQL, JOINs allow you to perform a single JOIN query rather than many simple queries. As a result, you'll get improved speed, lower server overhead, and fewer data transfers between MySQL and your application. Unlike SQL Server, MySQL does not have a distinct JOIN type for FULL OUTER JOIN. You may, however, combine LEFT OUTER JOIN and RIGHT OUTER JOIN to get the same effects as FULL OUTER JOIN.
​
```
SELECT 
	* 
FROM 
	tableA
LEFT JOIN tableB 
	ON tableA.id = tableB.id
UNION
SELECT 
	* 
FROM 
	tableA
RIGHT JOIN tableB 
	ON tableA.id = tableB.id
```
​
![](https://blog.devart.com/wp-content/uploads/2021/04/full-join-2.png)

Using MySQL JOINs, you can also join more than two tables.
​
```
SELECT 
	*
FROM 
	tableA
LEFT JOIN tableB
	ON tableA.id = tableB.id
LEFT JOIN tableC
	ON tableC.id = tableA.id;
```
![](https://paper-attachments.dropbox.com/s_7089066EA8FFE66D567C974E4D37410CFA274CB2DA52C7F834784CA7B0DC0B2D_1629270091364_Screen+Shot+2021-08-18+at+7.57.58+AM.png)

### Why Are JOINS Useful?
- **It's a lot quicker**. In a single query, JOINS allow you to get data from two or more linked database tables. JOINS are very valuable since they save time over running queries one by one to achieve the same results.
- **MySQL is more efficient**. Another advantage of utilizing JOINS is that MySQL performs better since [joins are performed via indexing](__GHOST_URL__/mysql-index-performance/).
- **Using JOINS lowers server load**. Because you execute one query, you get better and faster results.
​

## Conclusion
Any analyst or DBA must have a solid grasp of JOINs and utilize them freely in their daily job. This is when Arctype for MySQL comes in handy. Even for complicated JOIN clauses, its sophisticated code completion works flawlessly. Arctype for MySQL will present you with a full JOIN clause, so you won't have to memorize hundreds of column names or aliases. Its feature-rich capability makes creating complicated queries and managing JOIN conditions a breeze.