Joins In SQL Server - Part 1

Introduction

Join means combine. The join clause is used to combine the data from one and more than one table. Suppose there is more than one table and the user would like to display the data from all tables. In this scenario we will join the table. Let us assume there are two tables: Employee and Branch. From these tables user would like to combine the column from both the tables, then user has to use join.

Types of Join

  1. Self join
  2. Left outer join
  3. Right outer join
  4. Inner join

Before discussing join we will see the steps to work with joins in SQL Server.

Step 1: Go to Start button and select SQL Server Management Studio as in the following figure:

SQL server management studio

Now click on SQL Server Management Studio and go to object explorer.

Step 2: Create a new database using the following:

Create database database_Name

In this discussion I have created a database with name DBView as in the following figure:

database

Now go to table and create two tables,
  1. Employee
  2. Branch

Create table employee

Create table employee with four columns as:

  1. create table Employee(eid int primary key,ename varchar(20),designation varchar(20),mgrid int)  
error

Now insert the values in table Employee:
  1. insert into Employee values(1,'Sandeep','DBA',2)  
  2. insert into Employee values(2,'Rakesh','.Net Developer',1)  
  3. insert into Employee values(3,'sohan','Java Developer',1)  
  4. insert into Employee values(4,'Suresh','Software Trainee',3)  
  5. insert into Employee values(5,'Mohan','Software Trainee',4)  
After inserting the data in table run the query.
  1. select * from Employee  
Now output will be as in the following figure:

table

Again create one more table with name Branch.

Create table Branch.

Output

Now insert the values in table branch:
  1. insert into branch values(1oo,'Hyd',1)  
  2. insert into branch values(1o1,'DEL',2)  
  3. insert into branch values(1o3,'Bag',3)  
  4. insert into branch values(1o4,'Chn',4)  
  5. insert into branch values(1o5,'Nod',5)  
Now run the query: 
  1. Select * from branch  
Output will be displayed as in the following figure:

Run query

After completing the database and table work we will start working with Joins, which will be in next article of the series.
Ebook Download
View all
Learn
View all