Mysql Join Made Easy For Beginners
Mysql Join Made Easy For Beginners
For example, in the sample database, we have the orders and orderdetails tables that are
linked using the orderNumber column.
To get complete orders’ data, you need to query data from both orders and
orderdetails table.
And that’s why MySQL JOIN comes into the play.
A MySQL join is a method of linking data between one (self-join) or more tables based on
values of the common column between tables.
1. Cross join
2. Inner join
3. Left join
4. Right join
To join tables, you use the CROSS JOIN, INNER JOIN, LEFT JOIN or RIGHT JOIN clause for the
corresponding type of join. The join clause is used in the SELECT statement appeared after
the FROM clause.
Notice that MySQL does not support full outer join.
To make easy for you to understand each type of join, we will use the t1 and t2 tables with the
following structures:
1 CREATE TABLE t1 (
2 id INT PRIMARY KEY,
3 pattern VARCHAR(50) NOT NULL
4 );
5
6 CREATE TABLE t2 (
7 id VARCHAR(50) PRIMARY KEY,
8 pattern VARCHAR(50) NOT NULL
9 );
Both t1 and t2 tables have the pattern column, which is also the common column between
tables.
The following statements insert data into both t1 and t2 tables:
1 INSERT INTO t1(id, pattern)
2 VALUES(1,'Divot'),
3 (2,'Brick'),
4 (3,'Grid');
5
6 INSERT INTO t2(id, pattern)
7 VALUES('A','Brick'),
8 ('B','Grid'),
9 ('C','Diamond');
And the pictures below illustrate data from both t1 and t2 tables:
1 t1.pattern = t2.pattern
It means that rows in t1 and t2 tables must have the same values in the pattern column to be
included in the result.
The following illustrates the result of the query:
The following picture illustrates the INNER JOIN between t1 and t2 tables:
In this illustration, the rows in both tables must have the same pattern to be included in the result
set.
As you can see, all rows in the t1 table are included in the result set. For the rows in the t1 table
(left table) that do not have any matching row in the t2 table (right table), NULLs are used for
columns in t2 table.
The following picture illustrates the LEFT JOIN between t1 and t2 tables:
In this illustration, the following rows share the same pattern: (2 and A), (3 and B). The row with
id 1 in the t1table has no matching row in the t2 table, therefore, NULL are used for columns of
the t2 table in the result set.
MySQL RIGHT JOIN
A RIGHT JOIN is similar to the LEFT JOIN except that the treatment of tables is reversed. With
a RIGHT JOIN, every row from the right table ( t2) will appear in the result set. For the rows in
the right table that do not have the matching rows in the left table ( t1), NULLs appear for
columns in the left table ( t1).
The following statement joins t1 and t2 tables using RIGHT JOIN:
1 SELECT
2 t1.id, t2.id
3 FROM
4 t1
5 RIGHT JOIN
6 t2 on t1.pattern = t2.pattern
7 ORDER BY t2.id;
In this result, all rows from the right table ( t2) appear in the result set. For the rows in the right
table ( t2) that have no matching rows in the left table ( t1), NULL appears for columns of the
left table ( t1).
The following picture illustrates the RIGHT JOIN between t1 and t2 tables:
In this tutorial, you have learned various MySQL join statements including cross join, inner join,
left join and right join to query data from two or more tables.
Related Tutorials