1. Why we use joins???
Types of joins:
Inner join
Outer Join (left join,right join,full outer join)
Self join
The below image gives the summary of all joins
2. Inner join:
We use inner joins to get all the information from two tables which satisfies the condition.
Inner joins give only matching records from the tables.
The problem with inner join is that it will display only matching records.
We can even join more than two tables.
Outer Join:
Outer join displays all the rows of left table(table1) and all the rows of right
3. table(table 2)
It is the combination of left and right join.
Left Join:
But if we want to get all the records from table 1 and the matching records of
table1 and table2,we use left join in that case.
Left join displays all the records of table 1 and matching records of table 1 and
table 2.In case, if there are no values for the particular column,they will be
replaced by NULL
So, we use a left join which keeps all the data on the "left" (table 1) and pulls in
any matching data on the "right" (table 2)
4. We can even join more than two tables.
Right Join:
But if we want to get all the records from table 2 and the matching records of
table1 and table2,we use right join in that case.
Right join displays all the records of table 2 and matching records of table 1
and table 2.In case, if there are no values for the particular column,they will be
replaced by NULL
So, we use a right join which keeps all the data on the "right" (table 2) and pulls
in any matching data on the "left" (table 1)
We can even join more than two tables.
5. There is also another kind of join called as self join.
You use a self join when a table references data in itself.
E.g., an Employee table may have a SupervisorID column that points to the employee that is the boss
of the current employee.
To query the data and get information for both people in one row, you could self join like this:
select e1.EmployeeID,
e1.FirstName,
e1.LastName,
e1.SupervisorID,
e2.FirstName as SupervisorFirstName,
e2.LastName as SupervisorLastName
from Employee e1
left outer join Employee e2 on e1.SupervisorID = e2.EmployeeID
6. There is also another kind of join called as self join.
You use a self join when a table references data in itself.
E.g., an Employee table may have a SupervisorID column that points to the employee that is the boss
of the current employee.
To query the data and get information for both people in one row, you could self join like this:
select e1.EmployeeID,
e1.FirstName,
e1.LastName,
e1.SupervisorID,
e2.FirstName as SupervisorFirstName,
e2.LastName as SupervisorLastName
from Employee e1
left outer join Employee e2 on e1.SupervisorID = e2.EmployeeID