内联接与外联接的区别通过实例来说明是最清楚的了,下面先准备好测试环境:
 
create database test
go

create table t1 (deptid int,uname char(10))
create table t2 (deptid int,memo char(50))

insert into t1 values(1,'John')
insert into t1 values(2,'Tom')
insert into t1 values(3,'Michal')

insert into t2 values(1,'Human Resources Department')
insert into t2 values(2,'General Accounting Department')
insert into t2 values(4,'Engineering Department')
insert into t2 values(5,'Sales Department')
 
下面依次运行下列语句:
 
select * from t1,t2 where t1.deptid=t2.deptid

select * from t1 inner join t2 on t1.deptid=t2.deptid

select * from t1 left join t2 on t1.deptid=t2.deptid

select * from t1 right join t2 on t1.deptid=t2.deptid

select * from t1 full join t2 on t1.deptid=t2.deptid

select * from t1 cross join t2
 
语句1和2结果均为:
 deptid   
 uname  
 deptid   
 memo
 1
 John 
 1
 Human Resources Department
 2
 Tom 
 2
 General Accounting Department  
 
可见内联接与where子句效果一样。
 
语句3(左联)运行结果为:
 deptid   
 uname 
  deptid  
 memo
  1
  John
  1
 Human Resources Department
  2
  Tom
  2
 General Accounting Department  
 3
Michael
 NULL 
 NULL 
 
语句4(右联)运行结果为:
 deptid  
  uname  
 deptid   
memo
 1
 John
  
 Human Resources Department
  2
Tom
 2
 General Accounting Department
 NULL
 NULL
4
 Engineering Department
 NULL
 NULL
5
 Sales Department
 
语句5(全联)运行结果为:
 deptid  
uname 
deptid
 memo  
 1
 John
  
 Human Resources Department
 2
 Tom
 2
 General Accounting Department
 NULL
 NULL
 4
 Engineering Department
 NULL
 NULL
 5
 Sales Department
 3
Michael
  NULL 
 NULL 
 
 
语句6(交叉联接,注意该语句是没有ON子句的!)运行结果为:
deptid     uname   deptid      memo
 1   John          1    Human Resources Department                       
 1   John          2    General Accounting Department                    
 1   John          4    Engineering Department                           
 1   John          5    Sales Department                                 
 2   Tom             1    Human Resources Department                       
 2   Tom             2    General Accounting Department                    
 2   Tom             4    Engineering Department                           
 2   Tom             5    Sales Department                                 
 3   Michael        1    Human Resources Department                       
 3   Michael        2    General Accounting Department                    
 3   Michael        4    Engineering Department                           
 3   Michael        5    Sales Department   
 

参考资料: