# 系列文章目录

### 文章目录

• 系列文章目录
• 前言
• 一、什么是连接查询？
• 二、连接查询的分类
• 三、笛卡尔积
• 四、如何避免笛卡尔积现象？
• 五、内连接
• 5.1 等值连接
• 5.2 内连接中的非等值连接
• 5.3 内连接中的自连接
• 六、外连接
• 6.1 外连接的左外连接
• 6.2 外连接的右外连接
• 6.3 练习题
• 总结

# 一、什么是连接查询？

``````select column1、column2、column3 from table1, table2 ,table3;
``````

# 三、笛卡尔积

``````select column1、column2、column3 from table1, table2 ,table3;
``````

``````select ename ,dname from emp,dept;
``````

# 四、如何避免笛卡尔积现象？

``````select ename , dname from emp,dept where emp.deptno=dept.deptno;
``````

``````select emp.ename , dept.dname from emp,dept where emp.deptno=dept.deptno;
``````

# 五、内连接

## 5.1 等值连接

`sql92`

``````select emp. ename ,dept.dname from emp,dept where emp.deptno=dept.deptno;
``````

`sql99`

``````select emp. ename ,dept.dname from emp join dept on emp.deptno=dept.deptno;
``````

## 5.2 内连接中的非等值连接

``````select e.ename ,e.sal, s.grade from emp e join salgrade s on e.sal between s.losal and s.hisal;
``````

## 5.3 内连接中的自连接

``````select e.ename , s.ename from emp e join emp s on e.mgr=s.empno;
``````

# 六、外连接

``````select e.ename , s.ename from emp e left join emp s on e.mgr=s.empno;
``````

## 6.3 练习题

``````select e.ename, d.dname, s.grade ,e.sal from emp e join dept d on d.deptno = e.deptno join salgrade s on e.sal between s.losal and s.hisal ;
``````

``````select e.ename, d.dname, s.grade ,e.sal,l.ename from emp e join dept d on d.deptno = e.deptno join salgrade s on e.sal between s.losal and s.hisal left join emp l on e.mgr=l.empno;
``````