C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Resources » Articles » Databases »

Joins in Sql Server


Posted Date: 25 Jun 2008    Resource Type: Articles    Category: Databases
Author: SriramMember Level: Gold    
Rating: 1 out of 5Points: 8



Joins in Sql Server:

Joins actualy Performs Two or more Tables combined into a single result set.
Joins can also fetch with different dataBase,different Server also.

There are 5 types of joins available in sql server

1) inner join
2) outer join( left outer join,Right outer join)
3) Full Join
4) Cross Join
5) Self Join

1) inner Join : Two tables having the common column values go with inner join.

Example:
select * from emp inner join empsalary
on
emp.empid=empsalary.empid

2) outer join:
Outer join has two sub types left outer join,Right outer join
Left outer join: Taking Left side values of the table checking into right side
select * from emp left outer join empsalary
on emp.empid=empsalary.empid
Right Outer join:Taking Right side values of the table checking left side
select * from emp right outer join empsalary
on emp.empid=empsalary.empid

3) Full join :
Full join is combined with left outer join ,Right outer join.
select * from emp full join employee
on emp.empid=employee.empid

4) cross Join: cross join is an carteasion product ,its like matrix format here it wont come on condidtion

select * from emp cross join employee

Self join :

Self join can check within the table called self join.








Responses

Author: Bunty    27 Jun 2008Member Level: Diamond   Points : 2
Hi,

Nice attempy to explain Join in SQL Server.

If possible explain outer join in brief,you didn't mention anything on outer join.
And also if possible give eample for self join.

Keep posting.

Thanks for sharing your knowledge.

Thanks and Regards
S.S.Bajoria


Author: Sriram    30 Jun 2008Member Level: Gold   Points : 2
joins in sql server
Inner join
Outer join(left outer join,Right outer join)
Full outer Join
Cross join

create table emps(empid int,empname varchar(34))
insert into emps values(103,'ramesh')

create table employee(empid int,salary money)
insert into employee values(104,6000)
select * from emps
select * from employee

inner join:
select * from emps inner join employee on
emps.empid=employee.empid

empid empname empid salary
----------- ---------------------------------- ----------- ---------------------
101 sriram 101 3000.0000
102 ganesh 102 4000.0000

(2 row(s) affected)

left outer join :
select * from emps left outer join employee on
emps.empid=employee.empid
empid empname empid salary
----------- ---------------------------------- ----------- ---------------------
101 sriram 101 3000.0000
102 ganesh 102 4000.0000
103 ramesh NULL NULL

(3 row(s) affected)


right outer join
select * from emps right outer join employee on
emps.empid=employee.empid

empid empname empid salary
----------- ---------------------------------- ----------- ---------------------
101 sriram 101 3000.0000
102 ganesh 102 4000.0000
NULL NULL 104 6000.0000
full join :
select * from emps full outer join employee on
emps.empid=employee.empid

empid empname empid salary
----------- ---------------------------------- ----------- ---------------------
101 sriram 101 3000.0000
102 ganesh 102 4000.0000
103 ramesh NULL NULL
NULL NULL 104 6000.0000

cross join:
select * from emps cross join employee
empid empname empid salary
----------- ---------------------------------- ----------- ---------------------
101 sriram 101 3000.0000
102 ganesh 101 3000.0000
103 ramesh 101 3000.0000
101 sriram 102 4000.0000
102 ganesh 102 4000.0000
103 ramesh 102 4000.0000
101 sriram 104 6000.0000
102 ganesh 104 6000.0000
103 ramesh 104 6000.0000


To Join different DataBase :

select * from master.dbo.emps inner join msdb.dbo.employee on
master.dbo.emps.empid=msdb.dbo.employee.empid




Author: UltimateRengan    30 Jun 2008Member Level: Diamond   Points : 1
This is very good information,Continue posting such useful articles.



Author: Anne Grace    23 Jul 2008Member Level: Gold   Points : 0
good one.keep going


Author: deepakyadav    23 Jul 2008Member Level: Bronze   Points : 0
thank u this really helps me pal


Author: deepakyadav    23 Jul 2008Member Level: Bronze   Points : 0
if possible can u please give me some more details on stored procedures


Author: krishna    18 Sep 2008Member Level: Bronze   Points : 0
If possible can u please give me some more details on Inner joins.


Author: Ratnasri    19 Sep 2008Member Level: Bronze   Points : 1
Good Information
If U dont mind Can U give me some information on TRIGGERS and EXCEPTIONS in SQL


Author: rajeshwar    31 Jan 2009Member Level: Gold   Points : 1
This article will be very useful for me. It 's a good explanation for sql. thank u


Author: sangeetha    08 May 2009Member Level: Gold   Points : 2
Tables in a database can be related to each other with keys. A primary key is a column with a unique value for each row. Each primary key value must be unique within the table. The purpose is to bind data together, across tables, without repeating all of the data in every table.
In the "Employees" table below, the "Employee_ID" column is the primary key, meaning that no two rows can have the same Employee_ID. The Employee_ID distinguishes two persons even if they have the same name.
When you look at the example tables below, notice that:
• The "Employee_ID" column is the primary key of the "Employees" table
• The "Prod_ID" column is the primary key of the "Orders" table
• The "Employee_ID" column in the "Orders" table is used to refer to the persons in the "Employees" table without using their names



Employees Table:

Employee_ID Name
01 Hansen, Ola
02 Svendson, Tove
03 Svendson, Stephen
04 Pettersen, Kari

Orders Table:

Prod_ID Product Employee_ID
234 Printer 01
657 Table 03
865 Chair 03


INNER JOIN:

The INNER JOIN returns all rows from both tables where there is a match. If there are rows in Employees that do not have matches in Orders, those rows will not be listed.

Example INNER JOIN

Who has ordered a product, and what did they order?

SELECT Employees.Name, Orders.Product FROM Employees INNER JOIN Orders
ON Employees.Employee_ID=Orders.Employee_ID


Result

Name Product
Hansen, Ola Printer
Svendson, Stephen Table
Svendson, Stephen Chair

LEFT JOIN:

The LEFT JOIN returns all the rows from the first table (Employees), even if there are no matches in the second table (Orders). If there are rows in Employees that do not have matches in Orders, those rows also will be listed.

Example LEFT JOIN

List all employees, and their orders - if any.

SELECT Employees.Name, Orders.Product FROM Employees LEFT JOIN Orders ON Employees.Employee_ID=Orders.Employee_ID


Result

Name Product
Hansen, Ola Printer
Svendson, Tove
Svendson, Stephen Table
Svendson, Stephen Chair
Pettersen, Kari

RIGHT JOIN:

The RIGHT JOIN returns all the rows from the second table (Orders), even if there are no matches in the first table (Employees). If there had been any rows in Orders that did not have matches in Employees, those rows also would have been listed.

Example RIGHT JOIN

List all orders, and who has ordered - if any.

SELECT Employees.Name, Orders.Product FROM Employees RIGHT JOIN Orders ON Employees.Employee_ID=Orders.Employee_ID


Result

Name Product
Hansen, Ola Printer
Svendson, Stephen Table
Svendson, Stephen Chair





Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Sign In to add tags.
(No tags found.)

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: Nested Trigger or Recursing Trigger in SQL Server
Previous Resource: creating a database
Return to Discussion Resource Index
Post New Resource
Category: Databases


Post resources and earn money!
 
Related Resources



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use