Mysql练习题二

SQL练习题二#openLAB123

1、列出至少有一个员工的所有部门编号、名称,并统计出这些部门的平均工资、最低工资、最高工资

SELECT e.`部门编号`,e.`部门名称`,e.`平均工资`,e.`最低工资`,e.`最高工资` FROM ( SELECT

count(a.empno) AS '部门人数', d.DEPTNO AS '部门编号', d.DNAME AS '部门名称', avg(sal) AS '平均工资', MAX(sal) AS '最高工资', min(sal) AS '最低工资' FROM emp a, (SELECT deptno, DNAME FROM dept) d WHERE a.DEPTNO = d.deptno GROUP BY d.deptno ) e WHERE e.`部门人数` > 0;

2、列出薪金比SMITH或者ALLEN多的所有员工的编号、姓名、部门名称、其领导姓名

select e.empno , e.ename, d.dname ,m.ename from emp e join dept d on e.deptno =

d.deptno join emp m on e.mgr = m.empno where e.sal > (select max(sal) from emp where ename in ('SMITH','ALLEN'));

3、列出所有员工的编号、姓名及其直接上级的编号、姓名,显示的结果按领导年工资的降序排列

select e.empno, e.ename , m.empno, m.ename,(m.sal + ifnull(m.comm,0))*12 年薪 from emp e left join emp m on e.mgr = m.empno order by (m.sal + ifnull(m.comm,0))*12 desc;

4、列出雇佣日期早于其直接上级的所有员工的编号、姓名、部门名称、部门位置、部门人数

select e.empno , e.ename , d.dname , d.loc , n.ctfrom emp e join dept d on e.deptno = d.deptno

left join emp m on e.mgr = m.empno join (select deptno , count(empno) ct from emp group by deptno) n on e.deptno = n.deptno where e.hiredate < m.hiredate;

5、列出部门名称和这些部门的员工信息(数量、平均工资),同时列出那些没有员工的部门

select d.deptno, d.dname , count(e.empno) , avg(e.sal) from emp e right join dept d on e.deptno = d.deptno group by d.deptno, d.dname;

6、列出所有CLERK的姓名及其部门名称,部门的人数,工资等级

select e.ename , d.dname , n.ct , s.grade from emp e join dept d on e.deptno = d.deptno join salgrade s on e.sal between s.losal and s.hisal join (select deptno , count(empno) ct from emp group by deptno) n on e.deptno = n.deptno where e.job = 'CLERK';

7、列出最低薪金大于1500的各种工作及从事此工作的全部雇员人数及所在部门名称、位置、平均工资

select n.job, n.ct, n.avg_sal , d.dname , d.loc from emp e join dept d on e.deptno = d.deptno join (select job, count(e.empno) ct , avg(sal) avg_sal from emp e group by e.job having min(e.sal) > 1500) n on e.job = n.job;

8、列出在部门SALES工作的员工的姓名、基本工资、雇佣日期、部门名称,假定不知道该部门的部门编号

select e.ename, e.sal , e.hiredate, d.dname from emp e join dept d on e.deptno = d.deptno where d.dname = 'SALES';

9、列出薪金高于公司平均薪金的所有员工,所在部门,上级领导,公司的工资等级

10、列出与SCOTT从事相同工作的所有员工及部门名称,部门人数

select e.*, d.dname, n.ct from emp e , dept d , ( select deptno , count(empno) ct from emp group by deptno) n where e.deptno = d.deptno and e.deptno = n.deptno and e.job = (select job from emp where ename = 'SCOTT');

11、列出公司各个工资等级雇员的数量、平均工资

select s.grade,count(empno), avg(sal) from emp e , salgrade s where e.sal between s.losal and s.hisal group by s.grade;

12、列出薪金高于在部门30工作的所有员工的薪金的员工姓名和薪金、部门名称

select e.ename,e.sal,d.dname from emp e join dept d on e.deptno=d.deptno where e.sal>( select max(sal) maxsal from emp where deptno=30) and e.deptno<>30;

13、列出在每个部门工作的员工数量、平均工资和平均服务期限

select deptno, count(empno),avg(sal) , avg(datediff(now(),hiredate)/365) from emp group by deptno;

14、列出所有员工的姓名、部门名称和工资

select ename, dname, sal from emp e join dept d on e.deptno = d.deptno;

15、列出所有部门的详细信息和部门人数

select d.*,count(empno) from emp e right outer join dept d on (e.deptno = d.deptno)

group by deptno;

16、列出各种工作的最低工资及从事此工作的雇员姓名

select ename from emp where sal in (select min(sal) from emp group by job);

17、列出各个部门的MANAGER的最低薪金、姓名、部门名称、部门人数

select nt_sal.min_sal, e.ename, d.dname,nt_ct.ct from emp e,dept d, (select deptno, min(sal) min_sal from emp

where job = 'MANAGER' group by deptno) nt_sal , (select deptno , count(1) ct from emp group by deptno) nt_ct where e.deptno = d.deptno and e.deptno = nt_sal.deptno and e.deptno = nt_ct.deptno and e.job = 'MANAGER';

18、列出所有员工的年工资,所在部门名称,按年薪从低到高排序

select e.sal + ifnull(comm,0) 年薪, d.dname

from emp e join dept d on e.deptno = d.deptno order by 年薪;

19、列出某个员工的上级主管及所在部门名称,并要求出这些主管中的薪水超过3000

select e.ename, m.ename, d.dname from emp e,emp m,dept d where e.mgr = m.empno and m.deptno = d.deptno and m.sal >= 3000;

20、求出部门名称中带‘S’字符的部门员工的工资合计、部门人数

select d.deptno , sum(e.sal) , count(1)

from emp e right join dept d on e.deptno = d.deptno

where d.dname like '%S%'

group by d.deptno;

21、给任职日期超过30年或者87年雇佣的雇员加薪,加薪原则:10部门增长10%,20部门增长20%,30部门增长30%,以此类推

select sal, sal*(1+ deptno/100)

from emp

where adddate(curdate(), interval -30 year) > hiredate or year(hiredate) = 1987;

你可能感兴趣的:(mysql,数据库,sql)