/* 时间:2020/09/10 功能: 一 连接 二 内连接 三 SQL标准 四 练习 */
一 连接
/* 连接查询 1 定义: 将两个表或者两个以上的表以一定的连接条件连接起来,从中检索出满足的条件数据 */
/* 分类: 内连接、外连接、完全连接、交叉连接、自连接 */
二 内连接
-- 笛卡尔积 select * from emp, dept -- 70行11列 select * from emp, dept where emp.empno = 7369 -- 5行11列 -- 输出行数是dept表行数倍数 : 因为表结构是多对一,emp表是多的那方。 select * from emp, dept where emp.deptno = 10 -- 输出15行 -- 输出行数是emp表行数 : 因为表结构是多对一, dept表是一的那方。 select * from emp, dept where dept.deptno = 10 -- 输入14行
三 SQL标准
/* SQL92 select... from A, B SQL99 select... from A join B on */
-- SQL 92标准 select * from emp, dept where emp.deptno = dept.deptno -- 14行11列 -- SQL 99标准 select * from emp "E" join dept "D" on "E".deptno = "D".deptno -- 14行11列 -- 输出工资大于2000,员工姓名,部门名称,薪水等级 -- test sql 99 select "E".ename, "D".dname, "S".grade from emp "E" join dept "D" on "E".deptno = "D".deptno join salgrade "S" on "E".sal >= "S".losal and "E".sal <= "hisal" where "E".sal > 2000 -- test sql 92 select "E".ename, "D".dname, "S".grade from emp "E", dept "D", salgrade "S" where "E".deptno = "D".deptno and "E".sal >= "S".losal and "E".sal <= "S".hisal and "E".sal > 2000
四 练习
-- 每个员工姓名、部门编号、部门名称、薪水、薪水等级 select "E".ename, "E".deptno, "D".dname, "E".sal, "S".grade from emp "E" join dept "D" on "E".deptno = "D".deptno join salgrade "S" on "E".sal >= "S".losal and "E".sal <= "S".hisal-- 求emp表 所有领导的姓名 select distinct ename from emp where mgr is not null -- 工资最低三个员工: 姓名、薪水、部门编号、部门名称、薪水等级 select top 3 "E".ename, "E".sal, "E".deptno, "D".dname, "S".grade from emp "E" join dept "D" on "E".deptno = "D".deptno join salgrade "S" on "E".sal >= "S".losal and "E".sal <= "S".hisal order by "E".sal