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 to 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
- create table emp(empId int, empName varchar(15))
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
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
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.
- 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.
- 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
- create table emp_mngr(empName varchar(15),mngrName varchar(15))
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
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.
- select e.empName,e.mngrName from emp_mngr e inner join emp_mngr e1 on e.empName=e1.mngrName
Output
Summary
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.