Introduction
In this article I describe joins, types of joins, Inner Joins, Left Outer Joins, Right Outer Joins, Full Outer Joins, Cross Joins and Self Joins with examples.
Joins:
Joins are used to relate one or more tables in SQL Server. Joins are a part of a SQL Statement that retrieves rows from a table or tables according specified conditions.
Types of Joins:
- Inner Join
- Outer Join
- Cross Join
- Self Join
First we create two tables on which we apply the joins.
Creation of the first table is as:
Creation of the first table is as:
create table emp(empId int, empName varchar(15))
Insertion of data into the table:
Insertion of data into the table:
insert into emp
select 1,'deepak'union all
select 2,'Arora'union all
Select 3,'raj'union all
select 4,'Mahi'union all
select 5,'daljeet'union all
select 6,'kiran'
Output
select * from emp
Creation of second table:
create table emp_add(empId int, empAdd varchar(25))
insertion of data:
select 1,'Lakser'union all
select 2,'haridwar'union all
select 3,'usa'union all
select 7,'canada'union all
select 8,'punjab'union all
select 9,'Chandigarh'
Output:
select * from emp_add
Inner Join:It return all the Rows that satisfy the join Condition. Inner join produces records that match in Tables.
Inner Join:It return all the Rows that satisfy the join Condition. Inner join produces records that match in Tables.
select e.empId,e.empName,e1.empAdd from emp e inner join emp_add e1 on e.empId=e1.empId
Output:
Outer Join:
There are three types of Outer Join:
- Left Outer Join
- Right Outer Join
- Full Outer Join
Left Outer Join:
The result of the Left Outer Join contains all the records of the left table and if any record of the left table does not match the right table than it returns Null for the right side table.
The result of the Left Outer Join contains all the records of the left table and if any record of the left table does not match the right table than it returns Null for the right side table.
select e.empId,e.empName,e1.empAdd from emp e left outer join emp_add e1 on e.empId=e1.empId
Output:
Right Outer Join:
The result of the Right Outer Join contains all the records of the right table and if any record of the right table does not match the Left table than it returns Null for the left side table.
Output:
Right Outer Join:
The result of the Right Outer Join contains all the records of the right table and if any record of the right table does not match the Left table than it returns Null for the left side table.
select e.empId,e.empName,e1.empAdd from emp e right outer join emp_add e1 on e.empId=e1.empId
Output:
Full Outer Join:
A Full Outer Join fetches all records of both tables; where the record does not match, it returns Null.
select e.empId,e.empName,e1.empAdd from emp e full outer join emp_add e1 on e.empId=e1.empId
Output:
Cross Join:
This join is a Cartesian join. The result of a Cross Join contains records that are the multiplication of the records from both tables.
select e.empId,e.empName,e1.empAdd from emp e cross join emp_add e1
Output:
Here I create a table to explain self join:
Output:
Here I create a table to explain self join:
create table emp_mngr(empName varchar(15),mngrName varchar(15))
Insertion of data:
Insertion of data:
insert into emp_mngr
select 'ravi','Gaurav'union all
select'Gaurav','tom'union all
select 'sem','singh'union all
select 'singh','arora
Output:
Output:
select * from emp_mngr
Self Join:
In the Self Join a table is joined to itself. A Self Join can be an Inner Join or Outer Join. In the given example we find the employees that are the manager of other employees.
In the Self Join a table is joined to itself. A Self Join can be an Inner Join or Outer Join. In the given example we find the employees that are the manager of other employees.
select e.empName,e.mngrName from emp_mngr e inner join emp_mngr e1 on e.empName=e1.mngrName
Output:
In this article I described joins in SQL Server. I hope this article has helped you in understanding this topic. Please share it. If you know more about this, your feedback and constructive contributions are welcome.
Output:
create table abc(emp_ID int, manager varchar(20) , manager_id int)
emp_ID manager manager_id
1 abc NULL
2 def 1
3 ghi 2
4 klm 3
5 def1 1
6 ghi1 2
7 klm1 3
select a.emp_ID , a.manager emp_name,b.manager manager_name
from abc a
left join abc b
on a.manager_id = b.emp_ID
Result:
emp_ID emp_name manager_name
1 abc NULL
2 def abc
3 ghi def
4 klm ghi
5 def1 abc
6 ghi1 def
7 klm1 ghi
SummaryIn this article I described joins in SQL Server. I hope this article has helped you in understanding this topic. Please share it. If you know more about this, your feedback and constructive contributions are welcome.
No comments:
Post a Comment