数据准备

  1. 建库建表 ```sql CREATE DATABASE subquery;

CREATE TABLE dept( deptno INT, dname VARCHAR(14), loc VARCHAR(13) );

CREATE TABLE emp( empno INT, ename VARCHAR(50), job VARCHAR(50), mgr INT, hiredate DATE, sal DECIMAL(7,2), comm DECIMAL(7,2), deptno INT );


2. 插入数据
```sql
INSERT INTO emp VALUES(7369,'SMITH','CLERK',7902,'1980-12-17',800,NULL,20);
INSERT INTO emp VALUES(7499,'ALLEN','SALESMAN',7698,'1981-02-20',1600,300,30);
INSERT INTO emp VALUES(7521,'WARD','SALESMAN',7698,'1981-02-22',1250,500,30);
INSERT INTO emp VALUES(7566,'JONES','MANAGER',7839,'1981-04-02',2975,NULL,20);
INSERT INTO emp VALUES(7654,'MARTIN','SALESMAN',7698,'1981-09-28',1250,1400,30);
INSERT INTO emp VALUES(7698,'BLAKE','MANAGER',7839,'1981-05-01',2850,NULL,30);
INSERT INTO emp VALUES(7782,'CLARK','MANAGER',7839,'1981-06-09',2450,NULL,10);
INSERT INTO emp VALUES(7788,'SCOTT','ANALYST',7566,'1987-04-19',3000,NULL,20);
INSERT INTO emp VALUES(7839,'KING','PRESIDENT',NULL,'1981-11-17',5000,NULL,10);
INSERT INTO emp VALUES(7844,'TURNER','SALESMAN',7698,'1981-09-08',1500,0,30);
INSERT INTO emp VALUES(7876,'ADAMS','CLERK',7788,'1987-05-23',1100,NULL,20);
INSERT INTO emp VALUES(7900,'JAMES','CLERK',7698,'1981-12-03',950,NULL,30);
INSERT INTO emp VALUES(7902,'FORD','ANALYST',7566,'1981-12-03',3000,NULL,20);
INSERT INTO emp VALUES(7934,'MILLER','CLERK',7782,'1982-01-23',1300,NULL,10);
INSERT INTO emp VALUES(7981,'MILLER','CLERK',7788,'1992-01-23',2600,500,20);

INSERT INTO dept VALUES(10, 'ACCOUNTING', 'NEW YORK');
INSERT INTO dept VALUES(20, 'RESEARCH', 'DALLAS');
INSERT INTO dept VALUES(30, 'SALES', 'CHICAGO');
INSERT INTO dept VALUES(40, 'OPERATIONS', 'BOSTON');

where型子查询

单值结果

  1. 查询出高于10号部门的平均工资的员工信息 ```sql

    先查询10号部门的平均工资是多少

    select avg(sal) from emp where deptno = 10;

在查询高于10号部门平均工资的员工信息

select * from emp where sal > (select avg(sal) from emp where deptno = 10);


2. 工资大于JONES工资的员工信息
```sql
# 先查询JONES工资为多少
select sal from emp where ename = 'JONES';

# 在查询工资大于Jones的员工信息
select * from emp where sal > (select sal from emp where ename = 'JONES');
  1. 查询与SCOTT同一个部门的员工 ```sql

    先查询SCOTT所在的部门

    select deptno from emp where ename = ‘SCOTT’;

在查询和SCOTT在同一个部门的员工信息

select * from emp where deptno = (select deptno from emp where ename = ‘SCOTT’);

<a name="KdYXw"></a>
### 多值结果

1. 查询出比10号部门任何员工薪资高的员工信息
```sql
# 先查询出10号部门的员工薪资
select sal from emp where deptno = 10;

# 查询出比10号部门任何员工薪资高的员工信息
select * from emp where sal > any(select sal from emp where deptno = 10);
  1. 查询工作和工资与MARTIN完全相同的员工信息 ```sql

    先查询MARTIN的工作和工资

    select job,sal from emp where ename=’MARTIN’;

在查询工作工资和MARTIN完全相同的员工信息

select * from emp where (job,sal) in (select job,sal from emp where ename=’MARTIN’);


3. 查询两个以上直接下属的员工信息
```sql
# 先分组统计mgr的信息
select mgr from emp group by mgr having count(*) > 2;

# 在查询员工信息
select * from emp where empno in (select mgr from emp group by mgr having count(*) > 2);

from型子查询

  1. 查询每个部门的编号,部门名称,部门的人数 ```sql

    每个部门的编号,部门名称 在部门表中

    每个部门的编号,部门的人数,在员工表中统计出来的

先将每个部门的编号,部门的人数查询出来

select deptno,count(*) from emp group by (deptno);

然后将结果数据的临时表关联emp表进行查询

select d.deptno,d.dname,tmp.dcount from dept as d inner join (select deptno,count(*) as dcount from emp group by (deptno)) as tmp on d.deptno = tmp.deptno;

<a name="z6k5a"></a>
## having后接子查询

1. 有哪些部门的平均工资高于30号部门的平均工资
```sql
# 先查询30号部门的平均工资
select avg(sal) from emp where deptno = 30;

# 查询哪些部门平均工资高于30号部门的平均工资
select deptno,avg(sal) as avg_sal from emp group by deptno having avg_sal > (select avg(sal) from emp where deptno = 30);