34道经典SQL试题(下)

18.列出薪水比“SMITH”多的所有员工信息

select * from emp where sal > (select sal from emp where ename = 'SIMITH');
+-------+--------+-----------+------+------------+---------+---------+--------+
| empno | ename  | job       | mgr  | hiredate   | sal     | comm    | deptno |
+-------+--------+-----------+------+------------+---------+---------+--------+
|  7499 | ALLEN  | SALESMAN  | 7698 | 1981-02-20 | 1600.00 |  300.00 |     30 |
|  7521 | WARD   | SALESMAN  | 7698 | 1981-02-22 | 1250.00 |  500.00 |     30 |
|  7566 | JONES  | MANAGER   | 7839 | 1981-04-02 | 2975.00 |    NULL |     20 |
|  7654 | MARTIN | SALESMAN  | 7698 | 1981-09-28 | 1250.00 | 1400.00 |     30 |
|  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 |
|  7844 | TURNER | SALESMAN  | 7698 | 1981-09-08 | 1500.00 |    NULL |     30 |
|  7876 | ADAMS  | CLERK     | 7788 | 1987-05-23 | 1100.00 |    NULL |     20 |
|  7900 | JAMES  | CLERK     | 7698 | 1981-12-03 |  950.00 |    NULL |     30 |
|  7902 | FORD   | ANALYST   | 7566 | 1981-12-03 | 3000.00 |    NULL |     20 |
|  7934 | MILLER | CLERK     | 7782 | 1982-01-23 | 1300.00 |    NULL |     10 |
+-------+--------+-----------+------+------------+---------+---------+--------+

19.列出所有“CLERK”(办事员)的姓名及其部门名称,部门人数

select
	t1.deptno,t1.dname,t1.ename,t2.totalEmp
from
	(select 
	d.deptno,d.dname,e.ename
from
	emp e
join
	dept d
on
	e.deptno = d.deptno
where
	e.job = 'CLERK')t1
join
	(select
	e.deptno,count(e.ename) as totalEmp
from
	emp e
group by
	e.deptno)t2
on
	t1.deptno = t2.deptno;
+--------+-------------+--------+----------+
| deptno | dname       | ename  | totalEmp |
+--------+-------------+--------+----------+
|     20 | RESEARCHING | SIMITH |        5 |
|     20 | RESEARCHING | ADAMS  |        5 |
|     30 | SALES       | JAMES  |        6 |
|     10 | ACCOUNTING  | MILLER |        3 |
+--------+-------------+--------+----------+
把两张表一张当成t1 一张当成t2然后连接

20.列出最低薪水大亍1500的各种工作及从事此工作的全部雇员人数
第一步:先求出每种工作岗位最低薪水

select
	e.job,min(e.sal) as minsal,count(e.ename) as totalEmp
from
	emp e
group by
	e.job
having
	minsal >1500;
+-----------+---------+----------+
| job       | minsal  | totalEmp |
+-----------+---------+----------+
| MANAGER   | 2450.00 |        3 |
| ANALYST   | 3000.00 |        2 |
| PRESIDENT | 5000.00 |        1 |
+-----------+---------+----------+

21.列出在部门“SALES”<销售部>工作的员工的姓名,假定不知道销售部门的部门编号

select ename from emp where deptno = (select deptno from dept where dname = 'SALES');
+--------+
| ename  |
+--------+
| ALLEN  |
| WARD   |
| MARTIN |
| BLAKE  |
| TURNER |
| JAMES  |
+--------+

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

select
	d.dname,e.ename,b.ename as leadername,s.grade
from
	emp e
join
	dept d
on
	e.deptno = d.deptno
left join
	emp b
on
	e.mgr = b.empno
join
	salgrade s
on
	e.sal between s.losal and s.hisal
where
	e.sal >(select avg(sal) as avgsal from emp);
+-------------+-------+------------+-------+
| dname       | ename | leadername | grade |
+-------------+-------+------------+-------+
| RESEARCHING | JONES | KING       |     4 |
| SALES       | BLAKE | KING       |     4 |
| ACCOUNTING  | CLARK | KING       |     4 |
| RESEARCHING | SCOTT | JONES      |     4 |
| RESEARCHING | FORD  | JONES      |     4 |
| ACCOUNTING  | KING  | NULL       |     5 |
+-------------+-------+------------+-------+

23.列出与“SCOTT”从事相同工作的所有员工及部门名称

select
	d.dname,
	e.*
from
	emp e
join
	dept d
on
	e.deptno = d.deptno
where
	e.job = (select job from emp where ename = 'SCOTT');
+-------------+-------+-------+---------+------+------------+---------+------+--------+
| dname       | empno | ename | job     | mgr  | hiredate   | sal     | comm | deptno |
+-------------+-------+-------+---------+------+------------+---------+------+--------+
| RESEARCHING |  7788 | SCOTT | ANALYST | 7566 | 1987-04-19 | 3000.00 | NULL |     20 |
| RESEARCHING |  7902 | FORD  | ANALYST | 7566 | 1981-12-03 | 3000.00 | NULL |     20 |
+-------------+-------+-------+---------+------+------------+---------+------+--------+

24.列出薪金等于部门30中员工的薪金的其它员工的姓名和薪金

select deptno,ename,sal from emp where sal in(select distinct sal from emp where deptno = 30) and deptno <> 30;
结果为空

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

select
	d.dname,e.ename,e.sal
from
	emp e
join
	dept d
on
	e.deptno = d.deptno
where
	e.sal>(select max(sal) as maxsal from emp where deptno = 30);
+-------------+-------+---------+
| dname       | ename | sal     |
+-------------+-------+---------+
| RESEARCHING | JONES | 2975.00 |
| RESEARCHING | SCOTT | 3000.00 |
| ACCOUNTING  | KING  | 5000.00 |
| RESEARCHING | FORD  | 3000.00 |
+-------------+-------+---------+

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

select
	e.deptno,
	count(e.ename) as totalEmp,
	avg(e.sal) as avgsal,
	avg(to_days(now()) - to_days(hiredate))/365 as avgserveryear
from
	emp e
group by
	e.deptno;
+--------+----------+-------------+---------------+
| deptno | totalEmp | avgsal      | avgserveryear |
+--------+----------+-------------+---------------+
|     20 |        5 | 2175.000000 |   35.93479452 |
|     30 |        6 | 1566.666667 |   38.20639269 |
|     10 |        3 | 2916.666667 |   37.90776256 |
+--------+----------+-------------+---------------+

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

select
	d.dname,e.ename,e.sal
from
	emp e
right join
	dept d
on
	e.deptno = d.deptno;
+-------------+--------+---------+
| dname       | ename  | sal     |
+-------------+--------+---------+
| RESEARCHING | SIMITH |  800.00 |
| SALES       | ALLEN  | 1600.00 |
| SALES       | WARD   | 1250.00 |
| RESEARCHING | JONES  | 2975.00 |
| SALES       | MARTIN | 1250.00 |
| SALES       | BLAKE  | 2850.00 |
| ACCOUNTING  | CLARK  | 2450.00 |
| RESEARCHING | SCOTT  | 3000.00 |
| ACCOUNTING  | KING   | 5000.00 |
| SALES       | TURNER | 1500.00 |
| RESEARCHING | ADAMS  | 1100.00 |
| SALES       | JAMES  |  950.00 |
| RESEARCHING | FORD   | 3000.00 |
| ACCOUNTING  | MILLER | 1300.00 |
| OPERATIONS  | NULL   |    NULL |
+-------------+--------+---------+

28.列出所有部门的详细信息和人数

select
	e.deptno,d.dname,d.loc,count(e.ename) as totalEmp
from
	emp e
right join
	dept d
on
	e.deptno = d.deptno
group by
	d.deptno,d.dname,d.loc;
+--------+-------------+----------+----------+
| deptno | dname       | loc      | totalEmp |
+--------+-------------+----------+----------+
|     20 | RESEARCHING | DALLAS   |        5 |
|     30 | SALES       | CHICAGO  |        6 |
|     10 | ACCOUNTING  | NEW YORK |        3 |
|   NULL | OPERATIONS  | BOSTON   |        0 |
+--------+-------------+----------+----------+

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

select
	e.ename
from
	emp e
join
	(select
	e.job,min(e.sal) as minsal 
from
	emp e
group by
	e.job)t
on
	e.job = t.job
where
	e.sal = t.minsal;
+--------+
| ename  |
+--------+
| SIMITH |
| WARD   |
| MARTIN |
| CLARK  |
| SCOTT  |
| KING   |
| FORD   |
+--------+

30.列出各个部门MANAGER的最低薪金

select
	e.deptno ,min(e.sal) as minsal
from
	emp e
where 
	e.job = 'MANAGER'
group by
	e.deptno;
+--------+---------+
| deptno | minsal  |
+--------+---------+
|     20 | 2975.00 |
|     30 | 2850.00 |
|     10 | 2450.00 |
+--------+---------+

31.列出所有员工的年工资,按年薪从低到高排序

select ename,(sal+ifnull(comm,0))*12 as yearsal from emp order by yearsal asc;
+--------+----------+
| ename  | yearsal  |
+--------+----------+
| SIMITH |  9600.00 |
| JAMES  | 11400.00 |
| ADAMS  | 13200.00 |
| MILLER | 15600.00 |
| TURNER | 18000.00 |
| WARD   | 21000.00 |
| ALLEN  | 22800.00 |
| CLARK  | 29400.00 |
| MARTIN | 31800.00 |
| BLAKE  | 34200.00 |
| JONES  | 35700.00 |
| SCOTT  | 36000.00 |
| FORD   | 36000.00 |
| KING   | 60000.00 |
+--------+----------+

32.求出员工领导的薪水超过3000的员工名称和领导名称

select
	e.ename,b.ename as leadername
from
	emp e
join
	emp b
on
	e.mgr = b.empno
where
	b.sal >3000;
+-------+------------+
| ename | leadername |
+-------+------------+
| JONES | KING       |
| BLAKE | KING       |
| CLARK | KING       |
+-------+------------+

33.求部门名称中带“S”字符的部门员工的工资合计、部门人数

select
	d.dname ,sum(e.sal) as sumsal, count(e.ename) as totalEmp
from
	emp e
join
	dept d
on
	e.deptno = d.`deptno`
where
	d.dname like '%s%'
group by
	d.dname;
+-------------+----------+----------+
| dname       | sumsal   | totalEmp |
+-------------+----------+----------+
| RESEARCHING | 10875.00 |        5 |
| SALES       |  9400.00 |        6 |
+-------------+----------+----------+

34.给任职日期超过30年的员工加薪10%

update emp_bak1 set sal = sal * 1.1 where (to_days(now()) - to_days(hiredate))/365 >30;

你可能感兴趣的:(34道经典SQL试题(下))