Joins in SQL Server 2012

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:

  1. Inner Join
  2. Outer Join
  3. Cross Join
  4. 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
 

select * from emp

join-in-sql-emp.jpg

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

join-in-sql-emp-add.jpg

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:

inner-join-in-sql.jpg

Outer Join:

There are three types of Outer Join:

  1. Left Outer Join
  2. Right Outer Join
  3. 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:

left-outer-join-in-sql.jpg

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:

right-outer-join-in-sql.jpg

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:

full-outer-join-in-sql.jpg

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:

cross-join-in-sql.jpg

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:
 

select * from emp_mngr

join-in-sql-emp_mngr.jpg

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:

self-join-in-sql.jpg

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.
 

Up Next
    Ebook Download
    View all
    Learn
    View all