2. MySQL Joins OverviewMySQL Joins Overview
MySQL Join is used to join the records from twoMySQL Join is used to join the records from two
tables using join clause.tables using join clause.
The Join Clause return you the set of records fromThe Join Clause return you the set of records from
both table on the basis of common column.both table on the basis of common column.
3. MySQL Join TypesMySQL Join Types
MySQLMySQL Inner JoinInner Join
MySQL Equi JoinMySQL Equi Join
MySQL Natural JoinMySQL Natural Join
MySQL Cross JoinMySQL Cross Join
MySQL Outer JoinMySQL Outer Join
Left Outer JoinLeft Outer Join
Right Outer JoinRight Outer Join
Self JoinSelf Join
4. Inner joinInner join produces only the set of records that produces only the set of records that
match in both Table A and Table B.match in both Table A and Table B.
MySQL Inner JoinMySQL Inner Join
5. The INNER JOIN keyword returns rows when thereThe INNER JOIN keyword returns rows when there
is at least one match in both tables.is at least one match in both tables.
If there are rows in "Persons" that do not haveIf there are rows in "Persons" that do not have
matches in "Orders", those rows will NOT be listed.matches in "Orders", those rows will NOT be listed.
Example:Example:
SELECT Persons.LastName,SELECT Persons.LastName,
Persons.FirstName, Orders.OrderNoPersons.FirstName, Orders.OrderNo
FROM Persons INNER JOIN OrdersFROM Persons INNER JOIN Orders
ON Persons.P_Id = Orders.P_IdON Persons.P_Id = Orders.P_Id
ORDER BY Persons.LastNameORDER BY Persons.LastName
(Contd:)MySQL Inner JoinMySQL Inner Join
6. MySQL Outer JoinMySQL Outer Join
MySQL Outer Join return you the set of all matchingMySQL Outer Join return you the set of all matching
records from both table.records from both table.
The Outer Join does not requires each records to beThe Outer Join does not requires each records to be
matched in both the tables.matched in both the tables.
MySQL Outer Join is categorized into two groups.MySQL Outer Join is categorized into two groups.
MySQL Left Outer JoinMySQL Left Outer Join
MySQL Right Outer JoinMySQL Right Outer Join
7. MySQL Left Outer JoinMySQL Left Outer Join
Left outer joinLeft outer join produces a complete set of records produces a complete set of records
from Table A, with the matching records (wherefrom Table A, with the matching records (where
available) in Table B. If there is no match, the right sideavailable) in Table B. If there is no match, the right side
will contain null.will contain null.
8. MySQL Left Outer JoinMySQL Left Outer Join
The left join is used in case of need to return all rowsThe left join is used in case of need to return all rows
from the left table, even if the right table doesn't havefrom the left table, even if the right table doesn't have
any match.any match.
Example:Example:
SELECT Persons.LastName,SELECT Persons.LastName,
Persons.FirstName, Orders.OrderNoPersons.FirstName, Orders.OrderNo
FROM Persons LEFT JOIN OrdersFROM Persons LEFT JOIN Orders
ON Persons.P_Id=Orders.P_IdON Persons.P_Id=Orders.P_Id
ORDER BY Persons.LastNameORDER BY Persons.LastName
(Contd:)
9. MySQL Right Outer JoinMySQL Right Outer Join
The right join is used in case of need to return allThe right join is used in case of need to return all
rows from the right table, even if the left tablerows from the right table, even if the left table
doesn't have any match.doesn't have any match.
Example:Example:
SELECT Persons.LastName,Persons.FirstName,SELECT Persons.LastName,Persons.FirstName,
Orders.OrderNoOrders.OrderNo
FROM Persons RIGHT JOIN OrdersFROM Persons RIGHT JOIN Orders
ON Persons.P_Id = Orders.P_IdON Persons.P_Id = Orders.P_Id
ORDER BY Persons.LastNameORDER BY Persons.LastName
10. MySQL Cross JoinMySQL Cross Join
Cross Join is also called Cartesian Product Join.Cross Join is also called Cartesian Product Join.
The Cross Join in SQL return you a result table inThe Cross Join in SQL return you a result table in
which each row from the first table is combined withwhich each row from the first table is combined with
each rows from the second table.each rows from the second table.
11. MySQL Cross JoinMySQL Cross Join
In other words, you can say it is the crossIn other words, you can say it is the cross
multiplication of number of rows in each table.multiplication of number of rows in each table.
Example:Example:
SELECT * FROM persons cross join orders;SELECT * FROM persons cross join orders;
(Contd:)
12. MySQL Equi JoinMySQL Equi Join
Equi Join is a classified type of Inner Join in Mysql.Equi Join is a classified type of Inner Join in Mysql.
Equi Join is used to combine records from two tableEqui Join is used to combine records from two table
based on the common column exists in both table.based on the common column exists in both table.
The Equi Join returns you only those records whichThe Equi Join returns you only those records which
are available in both table on the basis of commonare available in both table on the basis of common
primary field name.primary field name.
Example:Example:
SELECT persons.firstname,orders.orderNoSELECT persons.firstname,orders.orderNo
FROM persons, ordersFROM persons, orders
WHERE persons.p_id = orders.p_id;WHERE persons.p_id = orders.p_id;
13. MySQL Natural JoinMySQL Natural Join
MySQL Natural Join is a specialization of equi-joins.MySQL Natural Join is a specialization of equi-joins.
The join compares all columns in both tables thatThe join compares all columns in both tables that
have the same column-name in both tables that havehave the same column-name in both tables that have
column name in the joined table.column name in the joined table.
Example:Example:
SELECT persons.firstname, orders.orderNoSELECT persons.firstname, orders.orderNo
FROM persons NATURAL JOIN orders;FROM persons NATURAL JOIN orders;
14. MySQL Self JoinMySQL Self Join
These join allow you to retrieve related recordsThese join allow you to retrieve related records
from the same table. from the same table.
The most common case where you'd use a self-join isThe most common case where you'd use a self-join is
when you have a table that references itself.when you have a table that references itself.
Example:Example:
SELECT m.name as "Manager", p.name asSELECT m.name as "Manager", p.name as
"Employee“"Employee“
FROM employee m, employee pFROM employee m, employee p
WHERE m.emp_id = p.manager_id;WHERE m.emp_id = p.manager_id;