20。列出最低薪资大于1500的各种工作 以及从事此工作的全部雇员人数按照工作岗位求最小值
按照工作岗位找到工资大于1500的
select job from emp group by job having min(sal)>1500;
+-----------+
| job |
+-----------+
| ANALYST |
| MANAGER |
| PRESIDENT |
+-----------+
找总人数
select job from emp group by job having min(sal)>1500;
+-----------+----------+
| job | count(*) |
+-----------+----------+
| ANALYST | 2 |
| MANAGER | 3 |
| PRESIDENT | 1 |
+-----------+----------+
按照岗位找人
select
e.*
from
emp e
join
(select job from emp group by job having min(sal)>1500) t
on
t.job=e.job;
+-------+-------+-----------+------+------------+---------+------+--------+
| EMPNO | ENAME | JOB | MGR | HIREDATE | SAL | COMM | DEPTNO |
+-------+-------+-----------+------+------------+---------+------+--------+
| 7566 | JONES | MANAGER | 7839 | 1981-04-02 | 2975.00 | NULL | 20 |
| 7698 | BLAKE | MANAGER | 7839 | 1981-05-01 | 2850.00 | NULL | 30 |
| 7782 | CLARK | MANAGER | 7839 | 1981-06-09 | 2450.00 | NULL | 10 |
| 7788 | SCOTT | ANALYST | 7566 | 1987-04-19 | 3000.00 | NULL | 20 |
| 7839 | KING | PRESIDENT | NULL | 1981-11-17 | 5000.00 | NULL | 10 |
| 7902 | FORD | ANALYST | 7566 | 1981-12-03 | 3000.00 | NULL | 20 |
+-------+-------+-----------+------+------------+---------+------+--------+
21.找出部门sales的员工姓名 假设不知道部门编号
找部门编号
select deptno from dept where dname='sales';
+--------+
| deptno |
+--------+
| 30 |
+--------+
找员工:
select ename from emp where deptno=(select deptno from dept where dname='sales');
+--------+
| ename |
+--------+
| ALLEN |
| WARD |
| MARTIN |
| BLAKE |
| TURNER |
| JAMES |
+--------+
22.列出薪资高于公司平均薪资的所有员工 所在部门,上级领导,工资等级
找出工资高于平均值的
select
e.ename '员工', d.dname,l.ename '领导', s.grade
from 先是 员工和岗位匹配 找到对应的岗位
emp e
join
dept d
on
e.deptno=d.deptno
left join 表左自连接找到对应的领导
emp l
on
e.mgr=l.empno
join 找到每一个员工对应的工资等级
salgrade s
on
e.sal between s.losal and s.hisal
where 工资的等级大于平均工资
e.sal>(select avg(sal) from emp);
+-------+------------+-------+-------+
| 员工 | dname | 领导 | grade |
+-------+------------+-------+-------+
| JONES | RESEARCH | KING | 4 |
| BLAKE | SALES | KING | 4 |
| CLARK | ACCOUNTING | KING | 4 |
| SCOTT | RESEARCH | JONES | 4 |
| KING | ACCOUNTING | NULL | 5 |
| FORD | RESEARCH | JONES | 4 |
+-------+------------+-------+-------+
23.列出与scott从事相同工作的所有员工以及部门名称
找到scott是干嘛的
select job from emp where ename='scott';
找与scott相同岗位的名称 部门编号
select e.ename, e.job, d.dname from emp e
join
dept d
on
d.deptno=e.deptno
where job=(select job from emp where ename='scott');
+-------+---------+----------+
| ename | job | dname |
+-------+---------+----------+
| SCOTT | ANALYST | RESEARCH |
| FORD | ANALYST | RESEARCH |
+-------+---------+----------+
24.列出薪资等于部门30 中员工的薪金的其他员工的姓名和薪资
部门30的薪资:
select distinct sal from emp where deptno=30;
+---------+
| sal |
+---------+
| 1600.00 |
| 1250.00 |
| 2850.00 |
| 1500.00 |
| 950.00 |
+---------+
找其他部门与这个薪资相同的员工
select ename sal from emp where sal in (select distinct sal from emp where deptno=30) and deptno<>30;
Empty set (0.00 sec)
25.列出薪资高于在部门30中工作的所有员工的薪资的员工姓名和薪资部门名称
就是找出部门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) from emp where deptno=30);
+-------+---------+------------+
| ename | sal | dname |
+-------+---------+------------+
| KING | 5000.00 | ACCOUNTING |
| JONES | 2975.00 | RESEARCH |
| SCOTT | 3000.00 | RESEARCH |
| FORD | 3000.00 | RESEARCH |
+-------+---------+------------+
26.列出在每个部门工作的员工数量,平均工资和平均服务期限
mysql中 有专门计算日期差的函数 timestampdiff(间隔类型,起始日期,当前日期)
select
d.*, count(e.ename), ifnull(avg(e.sal),0), ifnull(avg(timestampdiff(year,hiredate,now())),0) as '平均年'
from
emp e
right join
dept d
on
e.deptno=d.deptno
group by
d.deptno, d.dname, d.loc;
+--------+------------+----------+----------------+----------------------+---------+
| DEPTNO | DNAME | LOC | count(e.ename) | ifnull(avg(e.sal),0) | 平均年 |
+--------+------------+----------+----------------+----------------------+---------+
| 10 | ACCOUNTING | NEW YORK | 3 | 2916.666667 | 38.3333 |
| 20 | RESEARCH | DALLAS | 5 | 2175.000000 | 36.4000 |
| 30 | SALES | CHICAGO | 6 | 1566.666667 | 38.5000 |
| 40 | OPERATIONS | BOSTON | 0 | 0.000000 | 0.0000 |
+--------+------------+----------+----------------+----------------------+---------+
27.列出所有员工的姓名 部门和工资
select
e.ename, e.sal, d.dname
from
emp e
join
dept d
on
e.deptno= d.deptno;
+--------+---------+------------+
| ename | sal | dname |
+--------+---------+------------+
| CLARK | 2450.00 | ACCOUNTING |
| KING | 5000.00 | ACCOUNTING |
| MILLER | 1300.00 | ACCOUNTING |
| SMITH | 800.00 | RESEARCH |
| JONES | 2975.00 | RESEARCH |
| SCOTT | 3000.00 | RESEARCH |
| ADAMS | 1100.00 | RESEARCH |
| FORD | 3000.00 | RESEARCH |
| ALLEN | 1600.00 | SALES |
| WARD | 1250.00 | SALES |
| MARTIN | 1250.00 | SALES |
| BLAKE | 2850.00 | SALES |
| TURNER | 1500.00 | SALES |
| JAMES | 950.00 | SALES |
+--------+---------+------------+
28.列出所有部门的详细信息和人数
select
d.dname, d.deptno, d.loc, count(e.ename)
from
emp e
left join
dept d
on
d.deptno=e.deptno
group by
d.dname;
+------------+--------+----------+----------------+
| dname | deptno | loc | count(e.ename) |
+------------+--------+----------+----------------+
| ACCOUNTING | 10 | NEW YORK | 3 |
| RESEARCH | 20 | DALLAS | 5 |
| SALES | 30 | CHICAGO | 6 |
+------------+--------+----------+----------------+
29.找出每个工作的最低工资 以及从事此工作的雇员姓名
select job, ename, min(sal) from emp group by job;
+-----------+-------+----------+
| job | ename | min(sal) |
+-----------+-------+----------+
| ANALYST | SCOTT | 3000.00 |
| CLERK | SMITH | 800.00 |
| MANAGER | JONES | 2450.00 |
| PRESIDENT | KING | 5000.00 |
| SALESMAN | ALLEN | 1250.00 |
+-----------+-------+----------+
30.列出各个部门的领导的最低薪资
select
deptno, min(sal)
from emp
where job='manager'
group by deptno;
+--------+----------+
| deptno | min(sal) |
+--------+----------+
| 10 | 2450.00 |
| 20 | 2975.00 |
| 30 | 2850.00 |
+--------+----------+
31.列出所有员工的年薪 按照从低到高排序
select ename, (sal*12)+ifnull(comm,0) a from emp order by a ;
+--------+----------+
| ename | a |
+--------+----------+
| SMITH | 9600.00 |
| JAMES | 11400.00 |
| ADAMS | 13200.00 |
| WARD | 15500.00 |
| MILLER | 15600.00 |
| MARTIN | 16400.00 |
| TURNER | 18000.00 |
| ALLEN | 19500.00 |
| CLARK | 29400.00 |
| BLAKE | 34200.00 |
| JONES | 35700.00 |
| FORD | 36000.00 |
| SCOTT | 36000.00 |
| KING | 60000.00 |
+--------+----------+
32 找出员工领导的薪水超过3000的员工与领导名称
自连接
select
a.ename, b.ename
from
emp a
join
emp b
on
a.mgr=b.empno
where
b.sal>3000;
+-------+-------+
| ename | ename |
+-------+-------+
| JONES | KING |
| BLAKE | KING |
| CLARK | KING |
+-------+-------+
33.求出部门名称中,带‘s’字符的部门员工的工资合计。部门人数
select
d.deptno, d.dname, d.loc, count(e.ename), sum(e.sal)
from
emp e
right join
dept d
on
e.deptno = d.deptno
where
d.dname like'%s%'
group by
d.deptno;
+--------+------------+---------+----------------+------------+
| deptno | dname | loc | count(e.ename) | sum(e.sal) |
+--------+------------+---------+----------------+------------+
| 20 | RESEARCH | DALLAS | 5 | 10875.00 |
| 30 | SALES | CHICAGO | 6 | 9400.00 |
| 40 | OPERATIONS | BOSTON | 0 | NULL |
+--------+------------+---------+----------------+------------+
34.给任职日期超过30年的员工加薪10%
update emp set sal=sal*1.1 where timestampdiff(year,hiredate,now())>30;