MySQL练习2

来源网络,整理后分享

下图是表结构:dept为部门表,grade为绩效表, emp为雇员表,其中deptno字段关联dept表的deptno字段

MySQL练习2_第1张图片

执行的sql语句如下:

DROP DATABASE IF EXISTS test;
CREATE DATABASE test;
use test;



create table dept (
deptno char(2) not null primary key,
dname varchar(15) not null,
loc varchar(15) not null
) default charset=utf8;

insert into dept (deptno, dname, loc) values('10', 'accounting' ,'new york');
insert into dept (deptno, dname, loc) values('20', 'research','dallas');
insert into dept (deptno, dname, loc) values('30', 'sales' ,'chicago');
insert into dept (deptno, dname, loc) values('40', 'operations' ,'boston');

create table grade (
grade int(1) not null primary key,
losal int(4) not null,
hisal int(4) not null
) default charset=utf8;

insert into grade (grade, losal, hisal) values (1, 700, 1200);
insert into grade (grade, losal, hisal) values (2, 1201, 1400);
insert into grade (grade, losal, hisal) values (3, 1401, 2000);
insert into grade (grade, losal, hisal) values (4, 2001, 3000);
insert into grade (grade, losal, hisal) values (5, 3001, 9900);

CREATE TABLE emp (
empno char(4) not null primary key,
ename varchar(10) not null,
job varchar(15) not null,
mgr char(4),
hiredate date not null,
sal int(4) not null,
comm int(4),
deptno char(2),
foreign key(deptno) references dept(deptno)
)default charset=utf8;

insert into emp(empno, ename, job, mgr, hiredate, sal, comm, deptno) values('7369', 'smith', 'clerk', '7902', '1980-12-17', 800, null, 

'20');
insert into emp(empno, ename, job, mgr, hiredate, sal, comm, deptno) values('7499', 'allen', 'salesman', '7698', '1981-02-20', 1600, 300, 

'30');
insert into emp(empno, ename, job, mgr, hiredate, sal, comm, deptno) values('7521', 'ward', 'salesman', '7902', '1981-02-22', 1250, 500, 

'30');
insert into emp(empno, ename, job, mgr, hiredate, sal, comm, deptno) values('7566', 'jones', 'manager', '7839', '1981-04-02', 2975, null, 

'20');
insert into emp(empno, ename, job, mgr, hiredate, sal, comm, deptno) values('7654', 'martin', 'salesman', '7698', '1981-09-28', 1250, 

1400, '30');
insert into emp(empno, ename, job, mgr, hiredate, sal, comm, deptno) values('7698', 'blake', 'manager', '7839', '1981-05-01', 2850,
null,'30');
insert into emp(empno, ename, job, mgr, hiredate, sal, comm, deptno) values('7782', 'clark', 'manager', '7839', '1981-06-09', 2450, 
null,'10');
insert into emp(empno, ename, job, mgr, hiredate, sal, comm, deptno) values('7788', 'scott', 'analyst', '7566', '1987-04-19', 3000, null, 

'20');
insert into emp(empno, ename, job, mgr, hiredate, sal, comm, deptno) values('7839', 'king','president', null, '1981-11-17', 5000, 

null,'10');
insert into emp(empno, ename, job, mgr, hiredate, sal, comm, deptno) values('7844', 'turner', 'salesman', '7698', '1981-09-08', 1500, 0, 

'30');
insert into emp(empno, ename, job, mgr, hiredate, sal, comm, deptno) values('7876', 'adams', 'clerk', '7788', '1981-05-23', 1100, 
null,'20');
insert into emp(empno, ename, job, mgr, hiredate, sal, comm, deptno) values('7900', 'james', 'clerk', '7698', '1981-12-03', 950, null, 

'30');
insert into emp(empno, ename, job, mgr, hiredate, sal, comm, deptno) values('7902', 'ford','analyst', '7566', '1981-12-03', 3000, 

null,'20');
insert into emp(empno, ename, job, mgr, hiredate, sal, comm, deptno) values('7934', 'miller', 'clerk', '7782', '1982-01-23', 1300, null, 

'10');

练习题目及参考答案:

1、取得每个部门中的最高薪水的人员名称。
   select b.ename, b.sal, b.deptno from (select deptno, max(sal) as sal from emp  group by deptno) a, emp b where a.deptno = b.deptno and a.sal = b.sal;
2、哪些人的薪水在部门的平均薪水之上, 列出姓名,薪金,部门号。
 select b.ename, b.sal, b.deptno from (select deptno, avg(sal)  as sal from emp  group by deptno) a, emp b where a.deptno = b.deptno and b.sal > a.sal;
3、取得部门平均薪水的等级, 列出部门,等级:
    select a.deptno, b.grade from  (select deptno, avg(sal) as sal from emp group by deptno) a, grade b where a.sal between b.losal and b.hisal;
4、不准用分组函数(Max),取得最高薪水。
    select * from emp order by sal desc limit 1;
5、取得平均薪水最高的部门的部门编号
   select deptno, avg(sal) as sal from emp group by deptno order by sal desc limit 1;
6、取得平均薪水最高的部门的部门名称。
   select a.deptno, avg(a.sal) as sal, b.dname from emp a, dept b where a.deptno = b.deptno group by a.deptno order by sal desc limit 1;
7、求平均薪水的等级最低的部门的部门名称。
    select a.deptno, c.dname, b.grade from (select deptno, avg(sal) as avg_sal from emp group by deptno) a, grade b, dept c where a.deptno = c.deptno and a.avg_sal between b.losal and b.hisal order by b.grade limit 1; 
8、取得比普通员工(员工代码没有在mgr字段上出现的)的最高薪水还要高的领导人姓名。
   1.员工编号,薪水表
       // select empno from emp where empno not in (select distinct mgr from emp where mgr is not null);
       select distinct e1.empno, e1.sal from emp e1 left join emp e2 on e1.empno = e2.mgr where e2.empno is null;
   2.员工最高薪水值
       select max(e1.sal) from emp e1 left join emp e2 on e1.empno = e2.mgr where e2.empno is null;
   3.领导表
       select distinct mgr from emp where mgr is not null
   4.联结上述表
       select em.ename from (select distinct mgr from emp where mgr is not null) as ma, emp em where ma.mgr = em.empno and em.sal > 


(select max(e1.sal) from emp e1 left join emp e2 on e1.empno = e2.mgr where e2.empno is null);
9、取得薪水最高的前五名员工。
    select * from emp order by sal desc limit 5;
10、取得薪水最高的第六到第十名员工。
    select * from emp order by sal desc limit 5, 5;
11、取得最后入职的5名员工。
    select * from emp order by hiredate desc limit 5;
12、取得每个薪水等级有多少员工
    select gr.grade, count(em.empno) from emp em, grade gr where em.sal between gr.losal and gr.hisal group by gr.grade;
13、列出所有员工及领导的姓名
    select ename from emp;
14、列出受雇日期早于其直接上级的所有员工的编号,姓名,部门名称
    select em1.empno, em1.ename, em1.deptno from emp em1, emp em2 where em1.mgr = em2.empno and em1.hiredate < em2.hiredate;
15、列出部门名称和这些部门的员工信息(员工编号,员工姓名),同时列出那些没有员工的部门.
    select de.dname, em.empno, em.ename from dept de left join emp em on de.deptno = em.deptno;
16、列出至少有5个员工的所有部门编号
    select deptno from emp group by deptno having count(empno) >= 5;
17、列出薪金比"SMITH"多的所有员工信息.
    select * from emp where sal > (select sal from emp where ename ='smith');
18、列出所有"CLERK"(办事员)的姓名及其部门名称,部门的人数.
    1. 各部门的人数表
    select deptno, count(empno) from emp group by deptno
    2. 与办事员的姓名,部门名称
    select em.ename, de.dname from dept de, emp em where de.deptno = em.deptno and em.job = 'clerk';
    3. 联立上述两张表
    select em.ename, de.dname, t.c from dept de, emp em, (select deptno, count(empno) as c from emp group by deptno) t where de.deptno = em.deptno and t.deptno = de.deptno and em.job = 'clerk';
19、列出最低薪金大于1500的各种工作及从事此工作的全部雇员人数.
    select job, count(empno) from emp group by job having min(sal) > 1500;
20、列出在部门"SALES"<销售部>工作的员工的姓名,假定不知道销售部的部门编号.
    select em.ename from dept de, emp em where de.deptno = em.deptno and de.dname = 'sales';
21、列出薪金高于公司平均薪金的所有员工,所在部门,编号上级领导编号,雇员的工资等级.
    select em.empno, em.ename, em.deptno, em.mgr, gr.grade from emp em, grade gr where em.sal > (select avg(sal) from emp) and em.sal between gr.losal and gr.hisal;
22、列出与"SCOTT"从事相同工作的其他员工及部门名称.
    select em.empno, em.ename, de.dname from dept de, emp em where de.deptno = em.deptno and em.job = (select job from emp where ename='scott') and em.ename != 'scott';
23、列出薪金等于部门30中员工的薪金的其他员工的姓名和薪金.
    select ename, sal from emp where deptno != '30' and sal = any(select sal from emp where deptno='30');-- 可以用in
24、列出薪金高于在部门30工作的所有员工的薪金的员工姓名和薪金.部门名称.
    select em.ename, em.sal, de.dname from emp em, dept de where em.deptno = de.deptno and em.deptno != '30' and em.sal > ALL(select sal from emp where deptno='30');-- 可以用max
25、列出在每个部门工作的员工数量,平均工资和平均服务期限.
    select deptno, count(empno), avg(sal), avg(year(now()) - year(hiredate)) from emp group by deptno;
26、列出所有员工的姓名、部门名称和工资。
select em.ename, de.dname,  em.sal from emp em, dept de where em.deptno = de.deptno;
27、列出所有部门的详细信息和人数(有些部门没有员工)
    select d.deptno, d.dname, t.c from dept d left join (select de.deptno as no , count(em.empno) as c from emp em, dept de where em.deptno = de.deptno group by de.deptno) t on t.no = d.deptno;
28、列出各种工作的最低工资及从事此工作的雇员姓名
    select em1.ename, em1.sal from emp em1, (select job, min(sal) as min_sal from emp group by job) em2 where em1.job = em2.job and em1.sal = em2.min_sal;
29、列出各个部门的job为MANAGER(领导)的最低薪金
    select min(sal) from emp where job='manager' group by deptno;
30、列出所有员工的年工资,按年薪从低到高排序((sal + comm) * 12)
    select ename, (sal  + IFNULL(comm, 0)) * 12  as year_sal from emp order by year_sal;
31、求出领导的薪水超过3000的员工名称与领导名称
    select e1.ename, e1.sal, e2.ename, e2.sal from emp e1, emp e2 where e1.mgr = e2.empno and e2.sal > 3000;
32、求出部门名称中,带'S'字符的部门员工的工资合计、部门人数.
    select em.deptno, sum(em.sal), count(em.empno) from emp em, dept de where em.deptno = de.deptno and de.dname like '%s%' group by em.deptno; -- 或者使用 de.dname regexp 's' 
33、给任职日期超过30年的员工加薪10%.
    update emp set sal = sal * 1.1 where year(now()) - year(hiredate) > 30;

你可能感兴趣的:(MySQL)