--职工表 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 ) ; --部门表 CREATE TABLE dept( deptno INT, dname VARCHAR(14), loc VARCHAR(13) ); --插入数据到职工表 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 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‘); --练习开始!!! -- Q1:查询出高于10号部门的平均工资的员工信息 select * from emp where sal>(select avg(sal) from emp where deptno=10); -- Q2:查询出比10号部门任何员工薪资高的员工信息 select * from emp where sal>(select max(sal) from emp where deptno=10); --低效率写法 select * from emp where sal>all(select sal from emp where deptno=10); -- Q3:和10号部门同名同工作的员工信息 select * from emp e1 where exists(select * from emp e2 where e2.ename=e1.ename and e2.job=e1.job and e2.deptno=10) and e1.deptno<>10; --另一种写法(多列子查询) select * from emp where (ename,job) in (select ename,job from emp where deptno=10) and deptno<>10; -- Q4:获取员工的名字和部门的名字 select ename,dname from emp e,dept d where e.deptno=d.deptno; -- Q5:查询emp表中经理信息 select * from emp where empno in(select distinct mgr from emp); -- Q6:薪资高于10号部门平均工资的所有员工信息 select * from emp where sal>(select avg(sal) from emp where deptno=10); -- Q7:有哪些部门的平均工资高于30号部门的平均工资 select deptno from emp group by deptno having avg(sal)>(select avg(sal) from emp where deptno=30); -- Q8:工资>JONES工资的员工信息 select * from emp where sal>(select sal from emp where ename=‘JONES‘); --Q9:查询与SCOTT同一个部门的员工 select * from emp where deptno=(select deptno from emp where ename=‘SCOTT‘); -- Q10:工资高于30号部门所有人的员工信息 select * from emp where sal>(select max(sal) from emp where deptno=30); -- Q11:查询工作和工资与MARTIN完全相同的员工信息 select * from emp e1 where exists(select * from emp e2 where e1.job=e2.job and e1.sal=e2.sal and e2.ename=‘MARTIN‘) and ename<>‘MARTIN‘; --另一种写法(多列子查询) SELECT * FROM emp WHERE (job,sal) IN (SELECT job,sal FROM emp WHERE ename=‘MARTIN‘) and ename<>‘MARTIN‘; -- Q12:有两个以上直接下属的员工信息 select * from emp e1 where (select count(*) from emp e2 where e2.mgr=e1.empno)>2; --另一种方法 SELECT * FROM emp e1 WHERE e1.empno IN (SELECT e2.mgr FROM emp e2 GROUP BY e2.mgr HAVING COUNT(*)>2); -- Q13:查询员工编号为7788的员工名称,员工工资,部门名称,部门地址 select ename,sal,dname,loc from emp inner join dept on empno=7788 and emp.deptno=dept.deptno; -- Q14:查询出高于本部门平均工资的员工信息 select * from emp e1 where sal>(select avg(sal) from emp e2 where e2.deptno=e1.deptno); -- Q15:列出达拉斯加工作的人中,比纽约平均工资高的人 select * from emp inner join dept on sal>(select avg(sal) from emp inner join dept on emp.deptno=dept.deptno and loc=‘NEW YORK‘) and emp.deptno=dept.deptno and loc=‘DALLAS‘; -- Q16:查询7369员工编号,姓名,经理编号和经理姓名 select e1.empno,e1.ename,e1.mgr 经理编号,e2.ename 经理姓名 from emp e1 inner join emp e2 on e1.mgr=e2.empno and e1.empno=7369; -- Q17:查询出各个部门薪水最高的员工所有信息 select * from emp e1 where sal=(select max(sal) from emp e2 where e2.deptno=e1.deptno);
善于利用相关子查询编写SQL查询语句,可以较为方便,虽然可能会损失查找效率