MySQL入门练习(二)
本篇是上一篇的强化与扩展篇,同样材料来自于传智播客。
一、表格信息
1、部门表:由部门号、部门名称、部门地址组成:
CREATE TABLE dept(
deptno int PRIMARY KEY,
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');
2、员工表:由员工编号、姓名、工作、直属领导、入职日期、工资、奖金、部门号组成:
CREATE TABLE emp(
empno int PRIMARY KEY,
ename VARCHAR(10),
job VARCHAR(9),
mgr int,
hiredate DATE,
sal double,
comm double,
deptno int ,
foreign key emp(deptno) REFERENCES dept(deptno)
);
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-07-03',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-07-13',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,'1981-01-23',1300,NULL,10);
3、薪资等级表:由等级、最低工资、最高工资组成:
CREATE TABLE salgrade(
grade int,
losal double,
hisal double
);
INSERT INTO SALGRADE VALUES (1,700,1200);
INSERT INTO SALGRADE VALUES (2,1201,1400);
INSERT INTO SALGRADE VALUES (3,1401,2000);
INSERT INTO SALGRADE VALUES (4,2001,3000);
INSERT INTO SALGRADE VALUES (5,3001,9999);
二、表格内容输出
输出3个表格内容:
select * from emp;
select * from dept;
select * from salgrade;
1、员工表:
2、部门表:
3、薪资等级表:
三、基本练习
1、查找部门30中员工的详细信息。
2、找出从事clerk工作的员工的编号、姓名、部门号。
3、检索出奖金多于基本工资的员工信息。
4、检索出奖金多于基本工资60%的员工信息。
5、找出10部门的经理、20部门的职员 的员工信息。
6、找出10部门的经理、20部门的职员 或者既不是经理也不是职员但是工资高于2000元的员工信息。
7、找出获得奖金的员工的工作。
8、找出奖金少于100或者没有获得奖金的员工的信息。
9、找出姓名以A、B、S开始的员工信息。
10、找到名字长度为4个字符的员工信息。
11、名字中不包含R字符的员工信息。
12、返回员工的详细信息并按姓名排序。
13、返回员工的信息并按工作降序工资升序排列。
14、计算员工的日薪(按30天)。
15、找出姓名中包含A的员工信息。
四、基本练习答案
1、select * from emp where deptno=30;
2、select empno,ename,deptno from emp where job='clerk';
3、select * from emp where comm > sal;
4、select * from emp where comm > sal*0.6;
5、select * from emp where (job='manager' and deptno=10) or (job='clerk' and deptno=20);
6、select * from emp where (job='manager' and deptno=10) or (job='clerk' and deptno=20) or (job not in('manager','clerk') and sal>2000);
7、select job from emp where comm is not null;
8、select * from emp where comm is null or comm<100;
9、select * from emp where ename like 'A%' or ename like 'B%' or ename like 'S%';
10、select * from emp where length(ename)=4;
11、select * from emp where ename not like '%R%';
12、select * from emp order by ename;
13、select * from emp order by job desc,sal asc;
14、select ename,sal/30 from emp;
15、select * from emp where ename like '%A%';
四、扩展练习
1、返回拥有员工的部门名、部门号;
2、工资水平多于smith的员工信息;
3、返回员工和所属经理的姓名;
4、返回雇员的雇佣日期早于其经理雇佣日期的员工及其经理姓名;
5、返回员工姓名及其所在的部门名称;
6、返回从事clerk工作的员工姓名和所在部门名称;
7、返回部门号及其本部门的最低工资;
8、返回销售部(sales)所有员工的姓名;
9、返回工资水平多于平均工资的员工;
10、返回与SCOTT从事相同工作的员工;
11、返回与30部门员工工资水平相同的员工姓名与工资;
12、返回工资高于30部门所有员工工资水平的员工信息;
13、返回部门号、部门名、部门所在位置及其每个部门的员工总数;
14、返回员工的姓名、所在部门名及其工资;
15、返回员工的详细信息。(包括部门名)了;
16、返回员工工作及其从事此工作的最低工资;
17、返回不同部门经理的最低工资;
18、计算出员工的年薪,并且以年薪排序;
19、返回工资处于第四级别的员工的姓名;
20、返回工资为二等级的职员名字、部门所在地、和二等级的最低工资和最高工资;
五、扩展练习答案
1、select d.dname,d.deptno from dept d,emp e where d.deptno=e.deptno group by d.deptno;
2、select * from emp where sal > (select sal from emp where ename ='smith');
3、select e.ename,em.ename from emp e,emp em where e.mgr=em.empno and em.job='manager';
4、select e.ename,em.ename from emp e,emp em where e.mgr = em.empno and em.job = 'manager' and e.hiredate < em.hiredate;
5、select e.ename,d.dname from emp e,dept d where e.deptno=d.deptno;
6、select e.ename,d.dname from emp e,dept d where e.deptno=d.deptno and e.job='clerk';
7、select d.deptno,min(e.sal) from dept d,emp e where d.deptno=e.deptno group by e.deptno;
8、select e.ename from emp e,dept d where e.deptno=d.deptno and d.dname='sales';
9、select * from emp e where e.sal > (select avg(sal) from emp);
10、select * from emp e where e.job =(select job from emp where ename='scott');
11、select e.ename,e.sal from emp e where e.sal in(select sal from emp where deptno=30);
12、select * from emp where sal > (select max(sal) from emp where deptno=30);
13、select d.deptno,d.dname,d.loc,count(*) from dept d,emp e where e.deptno=d.deptno group by d.deptno;
14、select e.ename,d.dname,e.sal from dept d,emp e where e.deptno=d.deptno;
15、select e.*,d.dname from emp e,dept d where e.deptno=d.deptno;
16、select e.job,min(e.sal) from emp e,dept d where e.deptno=d.deptno group by e.job;
17、select min(e.sal) from dept d,emp e where d.deptno = e.deptno and e.job = 'MANAGER' group by d.deptno ;
18、select ename,(sal+ifnull(0,comm))*12 sum from emp order by sum;
19、select e.ename,e.sal from emp e,salgrade s where e.sal between s.losal and s.hisal and s.grade=4;
20、select e.ename,d.loc,s.losal,s.hisal from emp e,dept d,salgrade s where e.deptno=d.deptno and e.sal between s.losal and s.hisal and s.grade=2;