mysql练习:员工部门表(含答案)

文章目录

建表和添加数据

创建部门表

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

添加部门数据

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');

创建员工表

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
) ;

添加员工数据

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);

查询

基本查询问题

所有员工的信息

select * from emp;

薪资大于等于1000并且小于等于2000的员工信息

select * from emp where sal between 1000 and 2000;

从员工表中查询出所有的部门编号

select distinct deptno from emp;

查询出名字以A开头的员工的信息

select * from emp where ename like 'A%';

查询出名字第二个字母是L的员工信息

select * from emp where ename like '_L%';

查询出没有奖金的员工信息

select * from emp where comm is null;

所有员工的平均工资

select avg(sal) from emp;

所有员工的工资总和

select sum(sal) from emp;

所有员工的数量

select count(empno) from emp;

最高工资

select max(sal) from emp;

最少工资

select min(sal) from emp;

最高工资的员工信息

select * from emp where sal = (select max(sal) from emp);

最低工资的员工信息

select * from emp where sal = (select min(sal) from emp);

分组查询

每个部门的平均工资

select deptno,avg(sal) from emp group by deptno;

子查询

单行子查询

查询出高于10号部门的平均工资的员工信息
select * from emp where sal > (select avg(sal) from emp where deptno=10);

多行子查询(in not in any all) >any >all

查询出比10号部门任何员工薪资高的员工信息
select * from emp where sal > any(select sal from emp where deptno=10);

多列子查询(实际使用较少) in

和10号部门同名同工作的员工信息
select * from emp where (ename,job) in (select ename,job from emp where deptno=10);

Select接子查询

获取员工的名字和部门的名字
select e.ename,d.dname from emp e join dept d on e.deptno=d.deptno; 

from后面接子查询

查询emp表中经理信息
select * from emp where job='MANAGER';

where 接子查询

薪资高于10号部门平均工资的所有员工信息
select * from emp where sal > (select avg(sal) from emp where deptno=10);

having后面接子查询

有哪些部门的平均工资高于30号部门的平均工资
select deptno from emp group by deptno 
	having avg(sal) > (select avg(sal) from emp where deptno =30);
工资>JONES工资
select * from emp where sal > (select sal from emp where ename = 'JONES');
查询与SCOTT同一个部门的员工
select * from emp where deptno=(select deptno from emp where ename = 'SCOTT');
工资高于30号部门所有人的员工信息
select * from emp where sal > (select max(sal) from emp where deptno=30);
查询工作和工资与MARTIN完全相同的员工信息
select * from emp where (job,sal) 
	in (select job,sal from emp where ename = "MARTIN");
有两个以上直接下属的员工信息
select * from emp group by mgr having count(empno) > 2;
查询员工编号为7788的员工名称,员工工资,部门名称,部门地址
select e.ename,e.sal,d.dname,d.loc from emp e , dept d
	 where e.deptno=d.deptno and e.empno = 7788; 

SQL查询的综合案例

查询出高于本部门平均工资的员工信息
select * from emp e where sal > 
	any (select avg(sal) from emp ee group by deptno having e.deptno=ee.deptno);
列出达拉斯加工作的人中,比纽约平均工资高的人
SELECT * FROM emp WHERE deptno = ( SELECT deptno FROM dept WHERE loc ='DALLAS')
      AND 
      sal > (SELECT AVG(sal) FROM emp WHERE deptno = (SELECT deptno FROM dept WHERE loc ='NEW YORK'));
查询7369员工编号,姓名,经理编号和经理姓名
select e.empno,e.ename,d.empno,d.ename mgr from emp e,emp d 
	where e.mgr=d.empno and e.empno=7369;
查询出各个部门薪水最高的员工所有信息
- 方式一
select * from emp where (sal,deptno) 
	in (select max(sal),deptno from emp group by deptno);
- 方式二
select * from emp e,(select max(sal) max,deptno from emp group by deptno) s 
	where e.deptno=s.deptno and e.sal = s.max;

版权声明:本文为Student108原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
THE END
< <上一篇
下一篇>>