Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
Delinda
Constraint Violating Yak Guru
315 Posts |
Posted - 2010-04-07 : 05:12:07
|
I've as follow,declare @Departments table(DepartmentID int identity(1,1),Name varchar(30));insert into @Departments values('D1');insert into @Departments values('D2');insert into @Departments values('D3');insert into @Departments values('D4');declare @Employees table(EmployeeID int identity(1,1),[First Name] varchar(20),[Last Name] varchar(20),DepartmentID int);insert into @Employees values('John','Smith',1);insert into @Employees values('Dave','Johnson',2);insert into @Employees values('Mark','Andrew',2);insert into @Employees values('George','Michael',4);insert into @Employees values('Michael','Peterson',3);select EmployeeID,t2.DepartmentID,[First Name] + ' ' + [Last Name] as 'EmployeeFullName',t2.Name as DepartmentNamefrom @Employees t1 inner join @Departments t2on t1.DepartmentID=t2.DepartmentIDOutputEmployeeID | DepartmentID | EmployeeFullName | DepartmentName--------------------------------------------------------------------1 1 John Smith D12 2 Dave Johnson D23 2 Mark Andrew D24 4 George Michael D45 3 Michael Peterson D3select EmployeeID,t2.DepartmentID,[First Name] + ' ' + [Last Name] as 'EmployeeFullName',t2.Name as DepartmentNamefrom @Employees t1 right join @Departments t2on t1.DepartmentID=t2.DepartmentIDOutputEmployeeID | DepartmentID | EmployeeFullName | DepartmentName--------------------------------------------------------------------1 1 John Smith D12 2 Dave Johnson D23 2 Mark Andrew D25 3 Michael Peterson D34 4 George Michael D4What's the easy explanation between inner join and right join? |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-04-07 : 05:21:46
|
Now see the difference:-declare @Departments table(DepartmentID int identity(1,1),Name varchar(30));insert into @Departments values('D1');insert into @Departments values('D2');insert into @Departments values('D3');insert into @Departments values('D4');insert into @Departments values('D5');declare @Employees table(EmployeeID int identity(1,1),[First Name] varchar(20),[Last Name] varchar(20),DepartmentID int);insert into @Employees values('John','Smith',1);insert into @Employees values('Dave','Johnson',2);insert into @Employees values('Mark','Andrew',2);insert into @Employees values('George','Michael',4);insert into @Employees values('Michael','Peterson',3);select EmployeeID,t2.DepartmentID,[First Name] + ' ' + [Last Name] as 'EmployeeFullName',t2.Name as DepartmentNamefrom @Employees t1 inner join @Departments t2on t1.DepartmentID=t2.DepartmentIDoutput------------------------------------EmployeeID DepartmentID EmployeeFullName DepartmentName1 1 John Smith D12 2 Dave Johnson D23 2 Mark Andrew D24 4 George Michael D45 3 Michael Peterson D3select EmployeeID,t2.DepartmentID,[First Name] + ' ' + [Last Name] as 'EmployeeFullName',t2.Name as DepartmentNamefrom @Employees t1 right join @Departments t2on t1.DepartmentID=t2.DepartmentIDoutput-------------------EmployeeID DepartmentID EmployeeFullName DepartmentName1 1 John Smith D12 2 Dave Johnson D23 2 Mark Andrew D25 3 Michael Peterson D34 4 George Michael D4NULL 5 NULL D5the difference is right join brings all data from right table regardless of a match in left side (D5 in example) while inner join bring only matching values existing in both tables------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
Transact Charlie
Master Smack Fu Yak Hacker
3451 Posts |
Posted - 2010-04-07 : 05:24:44
|
| This is a pretty basic question so it's quite possible I've misunderstood what you were wanting.It sounds like you were just wanting the definition of INNER JOIN compared to RIGHT OUTER JOIN?Inner join only returns matching rows in both tables (so if a corresponding row doesn't exist in the left or right table then it won't be returned at all)Left join returns all rows in the left table (that aren't eliminated by the where clause) even if there is no matching row in the right tableRight join is exactly the same but returns all rows in the right table (that aren't eliminated by where clause) even if there are no matching rows in left tableCharlie===============================================================Msg 3903, Level 16, State 1, Line 1736The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION |
 |
|
|
vaibhavktiwari83
Aged Yak Warrior
843 Posts |
Posted - 2010-04-07 : 05:25:21
|
See the differencedeclare @Departments table(DepartmentID int identity(1,1),Name varchar(30));insert into @Departments values('D1');insert into @Departments values('D2');insert into @Departments values('D3');insert into @Departments values('D4');declare @Employees table(EmployeeID int identity(1,1),[First Name] varchar(20),[Last Name] varchar(20),DepartmentID int);insert into @Employees values('John','Smith',1);insert into @Employees values('Dave','Johnson',2);insert into @Employees values('Mark','Andrew',2);insert into @Employees values('George','Michael',4);--insert into @Employees values('Michael','Peterson',3);select EmployeeID,t2.DepartmentID,[First Name] + ' ' + [Last Name] as 'EmployeeFullName',t2.Name as DepartmentNamefrom @Employees t1 inner join @Departments t2on t1.DepartmentID=t2.DepartmentIDselect EmployeeID,t2.DepartmentID,[First Name] + ' ' + [Last Name] as 'EmployeeFullName',t2.Name as DepartmentNamefrom @Employees t1 right join @Departments t2on t1.DepartmentID=t2.DepartmentIDRight Join takes all the records from right hand side table and join with left hand side table show the matching data and null for non matching data on join condition unlike the inner join takes only records which are common to both the table on join condition for more details see Books online...Vaibhav TTo walk FAST walk ALONE To walk FAR walk TOGETHER |
 |
|
|
vaibhavktiwari83
Aged Yak Warrior
843 Posts |
Posted - 2010-04-07 : 05:26:37
|
Hey visakh identical answers Vaibhav TTo walk FAST walk ALONE To walk FAR walk TOGETHER |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-04-07 : 05:35:55
|
quote: Originally posted by vaibhavktiwari83 Hey visakh identical answers Vaibhav TTo walk FAST walk ALONE To walk FAR walk TOGETHER
great minds think alike ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
Delinda
Constraint Violating Yak Guru
315 Posts |
Posted - 2010-04-07 : 05:42:34
|
| tq very much |
 |
|
|
|
|
|
|
|