Your browser does not support JavaScript! Please enable script of your browser.
D
O
tNet-Tricks
Handy Dot Net Tricks Implementation !!
" Coding , A Rhythmic Literary Job "
SQL Server - Tutorial, Article, Archive, Handy Tricks, Training, Code Snippets, Reference Manual, T-SQL, PL/SQL, Development, Administration, Performance Tuning, Backup, Recovery, Reporting, Analysis, Security, XML, CLR Objects

SQL Server Joins / Basic of SQL Joins

Posted By : Shailendra Chauhan, 02 Mar 2011

Sql joins are used to fetch/retrieve data from two or more data tables, based on a join condition. A join condition is a relationship among some columns in the data tables that take part in Sql join. Basically data tables are related to each other with keys. We use these keys relationship in sql joins.

Types of Joins

In Sql Server we have only three types of joins. Using these joins we fetch the data from multiple tables based on condition.

  1. Inner Join

    Inner join returns only those records/rows that match/exists in both the tables. Syntax for Inner Join is as

     Select * from table_1 as t1
    inner join table_2 as t2
    on t1.IDcol=t2.IDcol 

  2. Outer Join

    We have three types of Outer Join.

    1. Left Outer Join

      Left outer join returns all records/rows from left table and from right table returns only matched records. If there are no columns matching in the right table, it returns NULL values. Syntax for Left outer Join is as :

       Select * from table_1 as t1
      left outer join table_2 as t2
      on t1.IDcol=t2.IDcol 

    2. Right Outer Join

      Right outer join returns all records/rows from right table and from left table returns only matched records. If there are no columns matching in the left table, it returns NULL values. Syntax for right outer Join is as :

       Select * from table_1 as t1
      right outer join table_2 as t2
      on t1.IDcol=t2.IDcol 

    3. Full Outer Join

      Full outer join combines left outer join and right outer join. This join returns all records/rows from both the tables.If there are no columns matching in the both tables, it returns NULL values. Syntax for full outer Join is as :

       Select * from table_1 as t1
      full outer join table_2 as t2
      on t1.IDcol=t2.IDcol 

  3. Cross Join

    Cross join is a cartesian join means cartesian product of both the tables. This join does not need any condition to join two tables. This join returns records/rows that are multiplication of record number from both the tables means each row on left table will related to each row of right table. Syntax for right outer Join is as :

     Select * from table_1
    cross join table_2 

  4. Self Join

    Self join is used to join a database table to itself, particularly when the table has a Foreign key that references its own Primary Key. Basically we have only three types of joins : Inner join, Outer join and Cross join. We use any of these three JOINS to join a table to itself. Hence Self join is not a type of Sql join.

Join Examples

Suppose we following two tables and data in these two tables is shown in figure.

 CREATE TABLE emp
(
 id int NOT NULL primary key,
 name varchar(100) NULL,
 designation varchar(50) NULL,
 supid int foreign key references emp(id)
 )
-- In this table we have a Foreign key supid that references its own Primary Key id. We use it for Self Join
INSERT INTO emp(id,name,designation) VALUES(1,'mohan','Manger')
INSERT INTO emp(id,name,designation,supid) VALUES(2,'raj kumar','SE',1)
INSERT INTO emp(id,name,designation) VALUES(3,'bipul kumar','Manager')
INSERT INTO emp(id,name,designation,supid) VALUES(4,'mrinal kumar','SE',2)
INSERT INTO emp(id,name,designation,supid) VALUES(5,'jitendra kumar','SE',2) 
 CREATE TABLE empinfo
(
 id int primary key,
 address varchar(50) NULL
)
INSERT INTO empinfo(id,address) VALUES(1,'Delhi')
INSERT INTO empinfo(id,address) VALUES(2,'Noida')
INSERT INTO empinfo(id,address) VALUES(4,'Gurgaon')
INSERT INTO empinfo(id,address) VALUES(6,'Delhi')
INSERT INTO empinfo(id,address) VALUES(7,'Noida') 

Inner Join

 select e.id,e.name,ei.id as eid, ei.address from emp e 
inner join empinfo ei on e.id=ei.id 

Left Outer Join

 select e.id,e.name,ei.id as eid, ei.address from emp e 
left outer join empinfo ei on e.id=ei.id 

Right Outer Join

 select e.id,e.name,ei.id as eid, ei.address from emp e 
right outer join empinfo ei on e.id=ei.id 

Full Outer Join

 select e.id,e.name,ei.id as eid, ei.address from emp e 
full outer join empinfo ei on e.id=ei.id 

Cross Join

 select e.id,e.name,ei.id as eid, ei.address from emp e 
cross join empinfo ei 

Self Join

 select e.id,e.name,e.supid as managerid, ei.name as managername from emp e 
left join emp ei on e.supid=ei.id; -- outer keyword is optional 
Summary

In this article I try to explain Sql Joins. I hope after reading this article your Sql Join concepts will be strong. I would like to have feedback from my blog readers. Please post your feedback, question, or comments about this article.

 

 
Tips & Tricks Subscription :