An overview of types of Joins in MySQL

SQL JOIN

A JOIN a clause is used to associate rows from two or more tables, placed on a similar column between them.

With the help of joins, we can recover the data from two or more tables rooted in logical relationships between the tables. Joins signalizes how the  SQL Server is compelled with the use of data from one table to select the rows in another table. Based on the two circumstances such as by specifying the column from each table to be used for the join.

Basically, database tables are connected with each other with keys. We use this key relationship in SQL Joins.

Types of Joins in SQL 

SQL Server we have three types of joins. By using these joins we get the data from multiple tables based on their condition.

Inner Join
Inner join returns only those records that match the tables. In an inner join, we generally use FORM or the WHERE clause in which the data of the first table is merged/joined. By using another table using the terms 'inner join' followed by the second table to be joined with the first table.

INNER JOIN Syntax

SELECT column_name(s)
FROM table1
INNER JOIN table2
ON table1.column_name = table2.column_name;

 

Table name Order

Order Id Customer Id Employee Id Order date

82929

627 005 2000-7-23

245262

898 008 2000-7-24

262839

 

262

006 2000-7-25

245262

 

728

009 2000-7-26

Table name Customers

 

Customer Id Name Address Contact no Postal code

788

 Ravi Kishan Ck town near bus stand, Goa 7869690567 678768

898

Ram Singh H.no 456 near kv school, Pune 678904567 987543

726

Jk Maurya New street sector 8 H.N0 89, Goa 7694796831 876345

728

Aditya Nanda Near SPGI hospital house67, Lucknow 8989897650 765897

 

Example

SELECT Order.OrderId, Customers.Name
FROM Order
INNER JOIN Customers ON Order.CustomerId = Customers.CustomerId;

 

Example of Joining three table

SELECT Order.OrderId, Customers.Name, Employee.EmployeeName
FROM ((Order
INNER JOIN Customers ON Order.CustomerId = Customers.CustomerId)
INNER JOIN Employee ON Order.EmployeeId = Employee.EmployeeId);

 

Outer Join

Right outer join is also called an Outer Join. Right, join is used to join more than two tables from the database. In these use-cases, using a right join method is a better choice because it can avoid the relocation of a table through which our whole query joins one table. 
 The right join is exceptionally used due to their entanglement, so for the basic and uncomplicated join, it is better to use a left join than a right. It makes the query accessible and easier and readable by others.

There are three types of Outer Join 

Left Outer Join

Left outer join returns all records from the left table(table 1) and from the right table(table 2) returns only match or similar records. If there are no duplicate/identical columns in the right table, it returns NULL values.

LEFT OUTER JOIN Syntax

SELECT column_name(s)
FROM table1
LEFT JOIN table2
ON table1.column_name = table2.column_name;

 

Table name Order

Order Id Customer Id Employee Id Order date

69738390

627 005 2000-7-23

64849944

898 008 2000-7-24

83739945

262 006 2000-7-25

73883993

728 009 2000-7-26

Table name Customers

 

Customer Id Name Address Contact no Postal code

788

 Ravi Kishan Ck town near bus stand, Goa 7869690567 678768

898

Ram Singh H.no 456 near kv school, Pune 6789045670 987543

726

Jk Maurya New street sector 8 H.N0 89, Goa 7694796831 876345

728

Aditya Nanda Near SPGI hospital house67, Lucknow 8989897650 765897

Example 

SELECT Customers.Name, Order.OrderId
FROM Customers
LEFT JOIN Order
ON Customers.CustomerId=Order.CustomerId
ORDER BY Customers.Name;

 

Right Outer Join

A right outer join returns all records/rows from the right table( table2 )  and from the left table ( table1 )returns only match or similar records. If there are no duplicate/identical columns in the left table, it returns NULL values.

RIGHT OUTER JOIN Syntax

SELECT column_name(s)
FROM table1
RIGHT JOIN table2
ON table1.column_name = table2.column_name;

 

 

Table name Order

Order Id

Customer Id Employee Id OrderDate Shipper Id

62882

698 009 2000-04-20 700

82990

890 008 2000-05-21 800

27282

526 004 2000-06-22 300

712828

781 005 2000-07-23 100

Table name Employees

Employee Id LastName FirstName BirthDate Photo

004

Singh Raghav 12/8/1968 EmpId4.jpg

005

Maurya Manoj     2/19/1952 EmpId5.jpg

008

Gupta Shekhar 8/30/1963 EmpId8.jpg

009

Yadav Ravi 3/05/1963 EmpId1.jpg

Example

SELECT Order.OrderId, Employees.LastName, Employees.FirstName
FROM Order
RIGHT JOIN Employees
ON Orders.EmployeeId = Employees.EmployeeId
ORDER BY Order.OrderId;

 

Full Outer Join

Full outer join connects left outer join and right outer join. This join returns all records from both tables. that is (table 1) and ( table 2). If there are no columns duplicated/identical in both tables, it returns NULL values.

FULL OUTER JOIN Syntax

SELECT column_name
FROM table1
FULL OUTER JOIN table2
ON table1.column_name = table2.column_name
WHERE condition;

 

 

Table name Order

Order Id

Customer Id Employee Id OrderDate Shipper Id

62882

698 009 2000-04-20 700

82990

890 008 2000-05-21 800

27282

526 004 2000-06-22 300

712828

781 005 2000-07-23 100

Table name Customers

Customer Id Name Address Contact no Postal code

788

 Ravi Kishan Ck town near bus stand, Goa 7869690567 678768

898

Ram Singh H.no 456 near kv school, Pune 678904567 987543

726

Jk Maurya New street sector 8 H.N0 89, Goa 7694796831 876345

728

Aditya Nanda Near SPGI hospital house67, Lucknow 8989897650 765897

Example

SELECT Customers.Name, Orders.OrderId
FROM Customers
FULL OUTER JOIN Order ON Customers.CustomerId=Order.CustomerId
ORDER BY Customers.Name;

 

Self Join
Self-join is used to join a database table virtually or independently, specifically when the table has a Foreign key that indicates its own Primary Key.  We use only three JOINS to join a table to itself that is Inner join, Outer join, and Cross join. . Hence Self-join is not a type of SQL join.

SELF JOIN Syntax

SELECT column_name
FROM table1 T1, table1 T2
WHERE condition;

 

Table name Customers

Customer Id Name Address City Postal Code Country

47745

Karan C62/590 near Bandra  Mumbai 8589535 India

4545

Mannat Jk tower near Manthan school Noida 8494949 India

3456

Ravish L405 The hyde park sector12 Noida 156674 India

4522

Jaydeep T78 Bakshi ka Talab near ICIC Bank  Lucknow 4558789 India

2535

Abhay F34/1 Ranopali near hotel Clark Faizabad Lucknow 454789 India

Example

SELECT A.Name AS Name1, B.Name AS Name2, A.City
FROM Customers A, Customers B
WHERE A.CustomerId <> B.CustomerId
AND A.City = B.City
ORDER BY A.City;
 


Keywords: