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 IdCustomer IdEmployee IdOrder date

82929

6270052000-7-23

245262

8980082000-7-24

262839

 

262

0062000-7-25

245262

 

728

0092000-7-26

Table name Customers

 

Customer IdNameAddressContact noPostal code

788

 Ravi KishanCk town near bus stand, Goa7869690567678768

898

Ram SinghH.no 456 near kv school, Pune678904567987543

726

Jk MauryaNew street sector 8 H.N0 89, Goa7694796831876345

728

Aditya NandaNear SPGI hospital house67, Lucknow8989897650765897

 

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 IdCustomer IdEmployee IdOrder date

69738390

6270052000-7-23

64849944

8980082000-7-24

83739945

2620062000-7-25

73883993

7280092000-7-26

Table name Customers

 

Customer IdNameAddressContact noPostal code

788

 Ravi KishanCk town near bus stand, Goa7869690567678768

898

Ram SinghH.no 456 near kv school, Pune6789045670987543

726

Jk MauryaNew street sector 8 H.N0 89, Goa7694796831876345

728

Aditya NandaNear SPGI hospital house67, Lucknow8989897650765897

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 IdEmployee IdOrderDateShipper Id

62882

6980092000-04-20700

82990

8900082000-05-21800

27282

5260042000-06-22300

712828

7810052000-07-23100

Table name Employees

Employee IdLastNameFirstNameBirthDatePhoto

004

SinghRaghav12/8/1968EmpId4.jpg

005

MauryaManoj    2/19/1952EmpId5.jpg

008

GuptaShekhar8/30/1963EmpId8.jpg

009

YadavRavi3/05/1963EmpId1.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 IdEmployee IdOrderDateShipper Id

62882

6980092000-04-20700

82990

8900082000-05-21800

27282

5260042000-06-22300

712828

7810052000-07-23100

Table name Customers

Customer IdNameAddressContact noPostal code

788

 Ravi KishanCk town near bus stand, Goa7869690567678768

898

Ram SinghH.no 456 near kv school, Pune678904567987543

726

Jk MauryaNew street sector 8 H.N0 89, Goa7694796831876345

728

Aditya NandaNear SPGI hospital house67, Lucknow8989897650765897

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 IdNameAddressCityPostal CodeCountry

47745

KaranC62/590 near Bandra Mumbai8589535India

4545

MannatJk tower near Manthan schoolNoida8494949India

3456

RavishL405 The hyde park sector12Noida156674India

4522

JaydeepT78 Bakshi ka Talab near ICIC Bank Lucknow4558789India

2535

AbhayF34/1 Ranopali near hotel Clark FaizabadLucknow454789India

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: