* 本博客涉及到的数据为bjpowernode.sql,数据见文末。
* 该数据有三张表:部门表dept(deptno部门编号、dname部门名称、loc位置)、
员工表emp(empno工号、ename员工姓名、job职位、mgr直属领导工号、
hiredate入职日期、sal月薪、comm补贴、deptno部门编号)、
薪水等级表salgrade(grade等级、losal区间下限、hisal区间上限)
1、 取得每个部门最高薪水人员的名称
mysql> select t.deptno,e.ename,t.msal from emp e
-> join (selectdeptno,max(sal) msal from emp group by deptno) t
-> on e.sal =t.msal;
+--------+-------+---------+
| deptno | ename | msal |
+--------+-------+---------+
| 30 | BLAKE |2850.00 |
| 20 | SCOTT |3000.00 |
| 10 | KING | 5000.00 |
| 20 | FORD | 3000.00 |
+--------+-------+---------+
2、 哪些人的薪水在部门平均薪水之上?
思路:先求平均薪水构成表a2;联立emp e表,和a2表
mysql>select a.ename,a.sal,a.deptno
-> from emp a
-> join (select deptno,avg(sal) asalfrom emp group by deptno) b
-> on a.sal > b.asal and a.deptno =b.deptno;
mysql>create table a2 as select deptno,avg(sal) asal from emp group by deptno;
mysql>select e.ename,e.sal,e.deptno,a2.asal from emp e,a2 where e.sal > a2.asaland e.deptno = a2.deptno;
+-------+---------+--------+-------------+
| ename | sal |deptno | asal |
+-------+---------+--------+-------------+
| ALLEN | 1600.00 | 30 | 1566.666667 |
| JONES | 2975.00 | 20 | 2175.000000 |
| BLAKE | 2850.00 | 30 | 1566.666667 |
| SCOTT | 3000.00 | 20 | 2175.000000 |
| KING | 5000.00 | 10 | 2916.666667 |
| FORD | 3000.00 | 20 | 2175.000000 |
+-------+---------+--------+-------------+
3、 取得部门中(所有人的)平均的薪水等级
思路:计算每个人的薪水等级;然后根据部门编号分组
select e.ename,e.deptno,s.gradefrom emp e
join salgrades on e.sal between s.losal and s.hisal
group by e.deptno;
mysql>select e.deptno,avg(s.grade) agrade from emp e
-> join salgrade s on e.sal betweens.losal and s.hisal
-> group by e.deptno;
+--------+--------+
| deptno |agrade |
+--------+--------+
| 10 | 3.6667 |
| 20 | 2.8000 |
| 30 | 2.5000 |
+--------+--------+
4、 求最高薪水(给出两种解决方案)
法一:mysql>select ename, max(sal) from emp;
+--------+----------+
| ename | max(sal) |
+--------+----------+
| SIMITH | 5000.00 |
+--------+----------+
法二:mysql>select ename,sal from emp order by sal desc limit 1;
+-------+---------+
| ename | sal |
+-------+---------+
| KING | 5000.00 |
+-------+---------+
法三:mysql>select ename,sal from emp where sal not in(select distinct a.sal from emp ajoin emp b on a.sal < b.sal);
+-------+---------+
| ename | sal |
+-------+---------+
| KING | 5000.00 |
+-------+---------+
5、 取得平均薪水最高的部门和部门编号(至少给出两种解决方案)
mysql>create table a5 as select deptno,avg(sal) asal from emp group by deptno;
法一:mysql> select deptno,max(asal) masal from a5;
+--------+-------------+
| deptno | masal |
+--------+-------------+
| 10| 2916.666667 |
+--------+-------------+
select deptno,avg(sal) asal from emp group by deptno havingavg(sal) = (select max(t.asal) from (select deptno,avg(sal) asal from emp groupby deptno having avg(sal)) t);
法二:mysql>select deptno, asal from a5 order by asal desc limit 1;
+--------+-------------+
| deptno | asal |
+--------+-------------+
| 10| 2916.666667 |
+--------+-------------+
select deptno,avg(sal) asal from emp group by deptno having avg(sal) =(select avg(sal) from emp group by deptno order by avg(sal) desc limit 1);
6、 取得平均薪水最高的部门和部门名称
mysql>create table a6 as select deptno,avg(sal) asal from emp group by deptno;
mysql>select d.dname, max(a6.asal) from dept d,a6 where d.deptno = a6.deptno;
+------------+--------------+
|dname | max(a6.asal) |
+------------+--------------+
|ACCOUNTING | 2916.666667 |
+------------+--------------+
select d.dname,avg(e.sal)asal from emp e join dept d on e.deptno = d.deptno group by d.dname havingavg(e.sal) = (select avg(sal) from emp group by deptno order by avg(sal) desclimit 1);
7、 求平均薪水等级最低的部门的部门名称
mysql>select d.dname, avg(s.grade) agrade from emp e join dept d on e.deptno =d.deptno join salgrade s on e.sal between s.losal and s.hisal group by d.dnameorder by agrade asc limit 1;
+-------+--------+
| dname |agrade |
+-------+--------+
| SALES |2.5000 |
+-------+--------+
8、 取得比普通员工(院代码没有在mgr字段出现的)的最高薪水还要高的领导人姓名
mysql>select * from emp where empno not in(select distinct mgr from emp);
Empty set(0.00 sec)
无法查询到结果,原因是not in语句不会自动忽略空值,需要程序员手动排除NULL,即在语句后添加‘where mgr is not null’(这里KING没有mgr),但是in可以自动忽略空值
mysql>select * from emp where empno not in(select distinct mgr from emp where mgr isnot null);
+-------+--------+----------+------+------------+---------+---------+--------+
| empno |ename | job | mgr | hiredate | sal | comm | deptno |
+-------+--------+----------+------+------------+---------+---------+--------+
| 7369 | SIMITH | CLERK | 7902 | 1980-12-17 | 800.00 | NULL | 20 |
| 7499 | ALLEN | SALESMAN | 7698 | 1981-02-20 | 1600.00 | 300.00 | 30 |
| 7521 | WARD | SALESMAN | 7698 | 1981-02-22 | 1250.00 | 500.00 | 30 |
| 7654 | MARTIN | SALEDMAN | 7698 | 1981-09-28| 1250.00 | 1400.00 | 30 |
| 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 |
| 7934 | MILLER | CLERK |7782 | 1982-01-23 | 1300.00 | NULL| 10 |
+-------+--------+----------+------+------------+---------+---------+--------+
mysql>select * from emp where empno in(select distinct mgr from emp);
+-------+-------+-----------+------+------------+---------+------+--------+
| empno |ename | job | mgr | hiredate | sal | comm | deptno |
+-------+-------+-----------+------+------------+---------+------+--------+
| 7902 | FORD | ANALYST | 7566 | 1981-12-03 |3000.00 | NULL | 20 |
| 7698 | BLAKE | MANAGER | 7839 | 1981-05-01 | 2850.00 | NULL | 30 |
| 7839 | KING | PRESIDENT | NULL | 1981-11-17 | 5000.00 | NULL | 10 |
| 7566 | JONES | MANAGER | 7839 | 1981-04-02 | 2975.00 | NULL | 20 |
| 7788 | SCOTT | ANALYST | 7566 | 1987-04-19 | 3000.00 | NULL | 20 |
| 7782 | CLARK | MANAGER | 7839 | 1981-06-09 | 2450.00 | NULL | 10 |
+-------+-------+-----------+------+------------+---------+------+--------+
selectb.ename,b.sal
from (selectmax(sal) msal from emp where empno not in(select distinct mgr from emp wheremgr is not null)) a
join (select* from emp where empno in(select distinct mgr from emp) b
on a.msal< b.sal;
+-------+---------+
| ename |sal |
+-------+---------+
| JONES |2975.00 |
| BLAKE | 2850.00|
| CLARK |2450.00 |
| SCOTT |3000.00 |
| KING | 5000.00 |
| FORD | 3000.00 |
+-------+---------+
9、 取得薪水最高的前五名员工
mysql> select * from emp order by sal desc limit 5;
+-------+-------+-----------+------+------------+---------+------+--------+
| empno | ename | job | mgr | hiredate | sal | comm | deptno |
+-------+-------+-----------+------+------------+---------+------+--------+
| 7839 | KING | PRESIDENT | NULL | 1981-11-17 | 5000.00 |NULL | 10 |
| 7902 | FORD | ANALYST | 3000 | 1981-12-03 | 3000.00 | NULL | 20 |
| 7788 | SCOTT |ANALYST | 7566 | 1987-04-19 | 3000.00 |NULL | 20 |
| 7566 | JONES |MANAGER | 7839 | 1981-04-02 | 2975.00 |NULL | 20 |
| 7698 | BLAKE |MANAGER | 7839 | 1981-05-01 | 2850.00 |NULL | 30 |
+-------+-------+-----------+------+------------+---------+------+--------+
10、取得薪水最高的第六到第十名员工
mysql> select * from emp order by sal desc limit 5,5;
+-------+--------+----------+------+------------+---------+--------+--------+
| empno | ename |job | mgr | hiredate | sal | comm | deptno |
+-------+--------+----------+------+------------+---------+--------+--------+
| 7782 | CLARK | MANAGER | 7839 | 1981-06-09 | 2450.00 | NULL | 10 |
| 7499 | ALLEN | SALESMAN | 7698 | 1981-02-20 | 1600.00 |300.00 | 30 |
| 7844 | TURNER |SALESMAN | 7698 | 1981-09-08 | 1500.00 | NULL | 30 |
| 7934 | MILLER |CLERK | 7782 | 1982-01-23 | 1300.00| NULL | 10 |
| 7521 | WARD | SALESMAN | 7698 | 1921-02-22 | 1250.00 |500.00 | 30 |
+-------+--------+----------+------+------------+---------+--------+--------+
11、取得最后入职的5名员工
mysql> select * from emp order by hiredate desc limit 5;
+-------+--------+---------+------+------------+---------+------+--------+
| empno | ename |job | mgr | hiredate | sal | comm | deptno |
+-------+--------+---------+------+------------+---------+------+--------+
| 7876 | ADAMS | CLERK | 7788 | 1987-05-23 | 1100.00 | NULL | 20 |
| 7788 | SCOTT | ANALYST | 7566 | 1987-04-19 | 3000.00 |NULL | 20 |
| 7934 | MILLER |CLERK | 7782 | 1982-01-23 | 1300.00 |NULL | 10 |
| 7900 | JAMES | CLERK | 7698 | 1981-12-03 | 950.00 |NULL | 30 |
| 7902 | FORD | ANALYST | 3000 | 1981-12-03 | 3000.00 |NULL | 20 |
+-------+--------+---------+------+------------+---------+------+--------+
12、取得每个薪水等级有多少员工
mysql> select s.grade,count(e.empno) from emp e joinsalgrade s on e.sal between s.losal and s.hisal group by s.grade;
+-------+----------------+
| grade | count(e.empno) |
+-------+----------------+
| 1 | 3 |
| 2 | 3 |
| 3 | 2 |
| 4 | 5 |
| 5 | 1 |
+-------+----------------+
13、面试题(58-59)
有三个表S(学生表),C(课程表),SC(学生选课表)
S(SNO,SNAME) 代表(学号,姓名)
C(CNO,CNAME,CTEACHER)代表(课号,课名,教师)
SC(SNO,CNO,SCGRADE)代表(学号、课号、成绩)
问题:
1、找出没选过‘黎明’老师的所有学生姓名;
2、列出2门以上(含2门)不及格学生姓名及平均成绩;
3、既学过1号课程又学过2号课所有学生的姓名。
思路:先在草稿纸模拟数据
s 学生表
sno(pk) sname
-------------------------------------
1 a
2 b
3 c
4 d
c 学生表
cno(pk) cname cteacher
----------------------------------------
1 java 王老师
2 C++ 张老师
3 C# 李老师
4 mysql 周老师
5 oracle 黎明
sc 学生选课表【一个主键,两个外键,sno+cno是复合主键,同时sno是外键,con也是外键】
sno cno scgrade
----------------------------------------
1 1 50
1 2 50
1 3 50
2 2 80
2 3 70
2 4 59
3 1 60
3 2 61
3 3 99
3 4 100
3 5 52
4 3 82
4 4 99
4 5 40
1、找出没选过‘黎明’老师的所有学生姓名;
第一步:找出黎明老师所授课课程编号
select cno fromc where cteacher = ‘黎明’
第二步:通过学生选课表查询cno=上面结果的sno,这些sno都是选过课的学号
select sno fromsc where cno = select cno from c where cteacher = ‘黎明’
第三步:在学生表中查询sno not in上面结果的数据
select snamefrom s where sno not in select sno from sc where cno = select cno from c wherecteacher = ‘黎明’();
【我的方法】select sname from s where sno not in(select distinctsno from sc where cno in(select cno from c where cteacher = '黎明'));
+-------+
| sname |
+-------+
| a |
| b |
+-------+
2、列出2门以上(含2门)不及格学生姓名及平均成绩;
第一步:列出2门以上(含2门)不及格学生姓名
select s.sname from sc join s on sc.sno = s.sno wheresc.scgrade < 60 group by s.sname having count(*) >= 2;
第二步:找出每一个学生的平均成绩
select sno,avg(scgrade) avggrade from sc group by sno;
第三步: 第一步和第二步联合
select t1.sname,t2.avggrade
from (select s.sno,s.sname from sc join s on sc.sno = s.sno
where sc.scgrade < 60 group by s.sno,s.sname havingcount(*) >= 2) t1
join (select sc.sno,avg(scgrade) avggrade from sc group bysc.sno) t2
on t1.sno = t2.sno;
+-------+----------+
| sname | avggrade |
+-------+----------+
| a | 50.0000 |
+-------+----------+
【我的方法】mysql> select s.sno,s.sname,avg(sc.scgrade)avggrade from s join sc on s.sno = sc.sno where s.sno = (select sno from scwhere scgrade < 60 group by sno having count(sno) >= 2) group by s.sno;
+-----+-------+----------+
| sno | sname | avggrade |
+-----+-------+----------+
| 1 | a | 50.0000 |
+-----+-------+----------+
3、既学过1号课程又学过2号课所有学生的姓名。
第一步:找出学过1号课程的学生
select sno from sc where cno = 1
第一步:找出学过2号课程的学生
select sno from sc where cno = 2
第三步:第一步和第二步联合
select s.sname from sc join s on s.sno = sc.sno where sc.cno =1 and sc.cno in(select sno from sc where cno = 2)
第四步
【我的方法】select sname from s where sno = (select sno from scwhere cno = 1 and cno = 2);//失败
mysql> select sname from s where sno in (select sno from scwhere cno = 1 and cno in (select sno from sc where cno = 2));
+-------+
| sname |
+-------+
| a |
| c |
+-------+
14、列出所有员工及领导的姓名
mysql> select e.ename as empname,l.ename as leadername fromemp e left join emp l on e.mgr = l.empno;
+---------+------------+
| empname | leadername |
+---------+------------+
| SIMITH | FORD |
| ALLEN | BLAKE |
| WARD | BLAKE |
| JONES | KING |
| MARTIN | BLAKE |
| BLAKE | KING |
| CLARK | KING |
| SCOTT | JONES |
| KING | NULL |
| TURNER | BLAKE |
| ADAMS | SCOTT |
| JAMES | BLAKE |
| FORD | JONES |
| MILLER | CLARK |
+---------+------------+
15、列出受雇日期早于其直接上级的所有员工的编号、姓名、部门名称
mysql> select e.empno eno,e.ename ename,e.hiredateedate,l.empno lno,l.ename lname,l.hiredate ldate,d.dname dname from emp e joinemp l on e.mgr = l.empno and e.hiredate < l.hiredate join dept d on e.deptno= d.deptno;
+------+--------+------------+------+-------+------------+-------------+
| eno | ename | edate | lno | lname | ldate | dname |
+------+--------+------------+------+-------+------------+-------------+
| 7369 | SIMITH | 1980-12-17 | 7902 | FORD | 1981-12-03 | RESEARCHING |
| 7499 | ALLEN |1981-02-20 | 7698 | BLAKE | 1981-05-01 | SALES |
| 7521 | WARD | 1981-02-22| 7698 | BLAKE | 1981-05-01 | SALES |
| 7566 | JONES |1981-04-02 | 7839 | KING | 1981-11-17 |RESEARCHING |
| 7698 | BLAKE |1981-05-01 | 7839 | KING | 1981-11-17 |SALES |
| 7782 | CLARK |1981-06-09 | 7839 | KING | 1981-11-17 | ACCOUNTING |
+------+--------+------------+------+-------+------------+-------------+
16、列出部门名称和这些部门的员工信息,同时列出那些没有员工的部门
mysql> select e.*,d.dname from emp e right join dept d one.deptno = d.deptno;
+------+--------+-----------+------+----------+---------+---------+-------+-----------+
|empno | ename | job | mgr |hiredate | sal | comm | deptno | dname |
+------+--------+-----------+------+-----------+---------+---------+------+-----------+
| 7369 | SIMITH | CLERK | 7902 | 1980-12-17 | 800.00 | NULL | 20 | RESEARCHIN|
| 7499 | ALLEN | SALESMAN | 7698 | 1981-02-20 | 1600.00 | 300.00 | 30 | SALES |
| 7521 | WARD | SALESMAN | 7698 | 1981-02-22 | 1250.00 | 500.00 | 30 | SALES |
| 7566 | JONES | MANAGER | 7839 | 1981-04-02 | 2975.00 | NULL | 20 | RESEARCHING|
| 7654 | MARTIN | SALEDMAN | 7698 | 1981-09-28 | 1250.00 | 1400.00| 30 | SALES |
| 7698 | BLAKE | MANAGER | 7839 | 1981-05-01 | 2850.00 | NULL | 30 | SALES |
| 7782 | CLARK | MANAGER | 7839 | 1981-06-09 | 2450.00 | NULL | 10 | ACCOUNTING|
| 7788 | SCOTT | ANALYST | 7566 | 1987-04-19 | 3000.00 | NULL | 20 | RESEARCHING|
| 7839 | KING | PRESIDENT | NULL | 1981-11-17 | 5000.00| NULL | 10 | ACCOUNTING|
| 7844 | TURNER | SALESMAN | 7698 | 1981-09-08 | 1500.00 | NULL | 30 | SALES |
| 7876 | ADAMS | CLERK | 7788 | 1987-05-23 | 1100.00 | NULL | 20 | RESEARCHING|
| 7900 | JAMES | CLERK | 7698 | 1981-12-03 | 950.00| NULL | 30 | SALES |
| 7902 | FORD | ANALYST | 7566 | 1981-12-03 | 3000.00 | NULL | 20 | RESEARCHING|
| 7934 | MILLER | CLERK | 7782 | 1982-01-23 | 1300.00 | NULL | 10 | ACCOUNTING|
| NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | OPERATIONS|
+------+--------+-----------+------+-----------+---------+---------+------+-----------+
17、列出至少有5个员工的所有部门
mysql> select d.deptno,d.dname,e.ename,count(e.deptno) fromemp e join dept d on e.deptno = d.deptno group by e.deptno havingcount(e.deptno) >= 5;
+--------+-------------+--------+-----------------+
| deptno | dname |ename | count(e.deptno) |
+--------+-------------+--------+-----------------+
| 20 | RESEARCHING |SIMITH | 5 |
| 30 | SALES | ALLEN | 6 |
+--------+-------------+--------+-----------------+
18、列出薪金比‘simith’多的所有员工信息
mysql> select * from emp where sal > (select sal from empwhere 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 |SALEDMAN | 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’(办事员)的姓名及部门名称,部门人数
mysql> select e.ename,d.dname,c.deptcount
-> from (select *from emp where job = 'clerk') e
-> join dept d one.deptno = d.deptno
-> join (selectdeptno,count(deptno) deptcount from emp group by deptno) c
-> on e.deptno =c.deptno;
+--------+-------------+-----------+
| ename | dname | deptcount |
+--------+-------------+-----------+
| SIMITH | RESEARCHING | 5 |
| ADAMS | RESEARCHING| 5 |
| JAMES | SALES | 6 |
| MILLER | ACCOUNTING | 3 |
+--------+-------------+-----------+
20、列出最低薪金大于1500的各种工作及从事此工作的全部雇员人数
mysql> select job,min(sal) minsal,count(job) jobcount fromemp group by job having min(sal) >1500;
+-----------+---------+----------+
| job | minsal | jobcount |
+-----------+---------+----------+
| ANALYST | 3000.00| 2 |
| MANAGER | 2450.00| 3 |
| PRESIDENT | 5000.00 | 1 |
+-----------+---------+----------+
21、列出在部门‘sales’(销售部)工作的员工的姓名,假定不知道销售部的部门编号
mysql> select d.deptno,d.dname,e.ename from emp e join deptd on e.deptno = d.deptno having d.deptno = (select deptno from dept where dname= 'sales');
+--------+-------+--------+
| deptno | dname | ename |
+--------+-------+--------+
| 30 | SALES |ALLEN |
| 30 | SALES |WARD |
| 30 | SALES | MARTIN|
| 30 | SALES |BLAKE |
| 30 | SALES | TURNER|
| 30 | SALES |JAMES |
+--------+-------+--------+
22、列出薪金高于公司平均薪金的所有员工,所在部门,上级领导,雇员的工资等级
mysql> select a.empno,a.ename,a.mgr,a.deptno,s.grade fromemp a join (select avg(sal) avgsal from emp) b on a.sal > b.avgsal joinsalgrade s on a.sal between s.losal and s.hisal;
+-------+-------+------+--------+-------+
| empno | ename | mgr |deptno | grade |
+-------+-------+------+--------+-------+
| 7566 | JONES | 7839| 20 | 4 |
| 7698 | BLAKE | 7839| 30 | 4 |
| 7782 | CLARK | 7839| 10 | 4 |
| 7788 | SCOTT | 7566| 20 | 4 |
| 7902 | FORD | 7566 | 20 | 4 |
| 7839 | KING | NULL | 10 | 5 |
+-------+-------+------+--------+-------+
23、列出与‘scott’从事相同工作的所有员工及部门名称
mysql> select e.ename,d.dname from emp e join dept d one.deptno = d.deptno where e.job = (select job from emp where ename = 'scott');
+-------+-------------+
| ename | dname |
+-------+-------------+
| SCOTT | RESEARCHING |
| FORD | RESEARCHING |
+-------+-------------+
mysql> select e.ename,d.dname from emp e join dept d one.deptno = d.deptno where e.job = (select job from emp where ename = 'scott')and ename != 'scott';
+-------+-------------+
| ename | dname |
+-------+-------------+
| FORD | RESEARCHING |
+-------+-------------+
24、列出薪金等于部门30中员工的薪金的其他员工的姓名和薪金
mysql> select deptno,ename,sal from emp where sal in(selectdistinct sal from emp where deptno = 30) and deptno != 30;
25、列出薪金高于在部门30工作的所有员工的薪金的员工姓名和薪金,部门名称
mysql> select e.ename,e.sal,d.dname from emp e
-> join dept d one.deptno = d.deptno
-> where e.sal> (select max(sal) from emp where deptno = 30);
+-------+---------+-------------+
| ename | sal |dname |
+-------+---------+-------------+
| JONES | 2975.00 | RESEARCHING |
| SCOTT | 3000.00 | RESEARCHING |
| KING | 5000.00 |ACCOUNTING |
| FORD | 3000.00 |RESEARCHING |
+-------+---------+-------------+
26、列出在每个部门工作的员工数量,平均工资和平均服务期限
mysql> select d.deptno,count(e.deptno)deptcount,ifnull(avg(e.sal),0) avgsal,
->ifnull(avg((to_days(now()) - to_days(hiredate))/365),0) days
-> from emp e
-> right join deptd on e.deptno = d.deptno
-> group byd.deptno;
+--------+-----------+-------------+-------------+
| deptno | deptcount | avgsal | days |
+--------+-----------+-------------+-------------+
| 10 | 3 | 2916.666667 | 36.37900000 |
| 20 | 5 | 2175.000000 | 34.40602000 |
| 30 | 6 | 1566.666667 | 36.67761667 |
| 40 | 0 | 0.000000 | 0.00000000 |
+--------+-----------+-------------+-------------+
27、列出所有员工的姓名、部门名称、工资
mysql> select e.ename,d.dname,e.sal from emp e join dept don e.deptno = d.deptno;
+--------+-------------+---------+
| ename | dname | sal |
+--------+-------------+---------+
| SIMITH | RESEARCHING | 800.00 |
| ALLEN | SALES | 1600.00 |
| WARD | SALES | 1250.00 |
| JONES | RESEARCHING |2975.00 |
| MARTIN | SALES |1250.00 |
| BLAKE | SALES | 2850.00 |
| CLARK | ACCOUNTING | 2450.00 |
| SCOTT | RESEARCHING |3000.00 |
| KING |ACCOUNTING | 5000.00 |
| TURNER | SALES |1500.00 |
| ADAMS | RESEARCHING |1100.00 |
| JAMES | SALES | 950.00 |
| FORD | RESEARCHING |3000.00 |
| MILLER | ACCOUNTING |1300.00 |
+--------+-------------+---------+
28、列出所有部门的详细信息和人数
mysql> select d.*,count(e.deptno) deptcount from emp e rightjoin dept d on e.deptno = d.deptno group by e.deptno;
+--------+-------------+----------+-----------+
| deptno | dname |loc | deptcount |
+--------+-------------+----------+-----------+
| 40 |OPERATIONS | BOSTON | 0 |
| 10 |ACCOUNTING | NEW YORK | 3 |
| 20 | RESEARCHING |DALLAS | 5 |
| 30 | SALES | CHICAGO | 6 |
+--------+-------------+----------+-----------+
29、列出各种工作的最低工资以及从事此工作的雇员姓名
mysql> select e.ename,e.sal,a.minsal from emp e
-> join (selectjob,min(sal) minsal from emp group by job) a
-> on e.sal =a.minsal;
+--------+---------+---------+
| ename | sal | minsal |
+--------+---------+---------+
| SIMITH | 800.00 | 800.00 |
| WARD | 1250.00 |1250.00 |
| MARTIN | 1250.00 | 1250.00 |
| CLARK | 2450.00 |2450.00 |
| SCOTT | 3000.00 |3000.00 |
| KING | 5000.00 |5000.00 |
| FORD | 3000.00 |3000.00 |
+--------+---------+---------+
30、列出各个部门的manager的最低薪金
mysql> select e.ename,e.job,min(e.sal),e.deptno minsal fromemp e where e.job = 'manager' group by e.deptno;
+-------+---------+------------+--------+
| ename | job |min(e.sal) | minsal |
+-------+---------+------------+--------+
| CLARK | MANAGER | 2450.00 | 10 |
| JONES | MANAGER | 2975.00 | 20 |
| BLAKE | MANAGER | 2850.00 | 30 |
+-------+---------+------------+--------+
31、列出员工的年工资,按年薪从低到高排序
mysql> select *,(sal + ifnull(comm,0)) * 12 ysal from emporder by ysal asc;
+-------+--------+-----------+------+------------+---------+---------+--------+----------+
| empno | ename | job | mgr | hiredate | sal | comm | deptno | ysal |
+-------+--------+-----------+------+------------+---------+---------+-----+----------+
| 7369 | SIMITH | CLERK | 7902 |1980-12-17 | 800.00 | NULL | 20 | 9600.00 |
| 7900 | JAMES | CLERK | 7698 | 1981-12-03 | 950.00 | NULL | 30 | 11400.00 |
| 7876 | ADAMS | CLERK | 7788 | 1987-05-23 |1100.00 | NULL | 20 | 13200.00 |
| 7934 | MILLER | CLERK | 7782 |1982-01-23 | 1300.00 | NULL | 10 | 15600.00 |
| 7844 | TURNER | SALESMAN | 7698 |1981-09-08 | 1500.00 | NULL | 30 | 18000.00 |
| 7521 | WARD | SALESMAN | 7698 | 1981-02-22 | 1250.00 | 500.00 | 30 | 21000.00 |
| 7499 | ALLEN | SALESMAN | 7698 | 1981-02-20 | 1600.00 | 300.00 | 30 | 22800.00 |
| 7782 | CLARK | MANAGER | 7839 | 1981-06-09 | 2450.00 | NULL | 10 | 29400.00 |
| 7654 | MARTIN | SALESMAN | 7698 |1981-09-28 | 1250.00 | 1400.00 | 30 |31800.00 |
| 7698 | BLAKE | MANAGER | 7839 | 1981-05-01 | 2850.00 | NULL | 30 | 34200.00 |
| 7566 | JONES | MANAGER | 7839 | 1981-04-02 | 2975.00 | NULL | 20 | 35700.00 |
| 7788 | SCOTT | ANALYST | 7566 | 1987-04-19 | 3000.00 | NULL | 20 | 36000.00 |
| 7902 | FORD | ANALYST | 7566 | 1981-12-03 | 3000.00 | NULL | 20 | 36000.00 |
| 7839 | KING | PRESIDENT | NULL |1981-11-17 | 5000.00 | NULL | 10 | 60000.00 |
+-------+--------+-----------+------+------------+---------+---------+-----+----------+
32、求出员工领导的薪水超过3000的员工名称与领导姓名
mysql> select e.ename empname,e.job empjob,e.salempsal,l.ename leadername,l.sal leadersal
-> from emp e joinemp l on e.mgr = l.empno
-> where l.sal> 3000;
+---------+---------+---------+------------+-----------+
| empname | empjob |empsal | leadername | leadersal |
+---------+---------+---------+------------+-----------+
| JONES | MANAGER | 2975.00| KING | 5000.00 |
| BLAKE | MANAGER |2850.00 | KING | 5000.00 |
| CLARK | MANAGER |2450.00 | KING | 5000.00 |
+---------+---------+---------+------------+-----------+
33、求出部门名称中,带有‘s’字符的部门员工的工资合计、部门人数
mysql> select d.dname,sum(e.sal) cumsal,count(e.deptno)deptcount
-> from emp eright join dept d on e.deptno = d.deptno
-> group bye.deptno
-> having d.dnamelike '%s%';
+-------------+----------+-----------+
| dname |cumsal | deptcount |
+-------------+----------+-----------+
| OPERATIONS | NULL | 0 |
| RESEARCHING | 10875.00 | 5 |
| SALES | 9400.00 | 6 |
+-------------+----------+-----------+
34、给任职日期超过35年的员工加薪10%
mysql> create table emp_bak as select * from emp;
mysql> update emp_bak set sal = sal * 1.1 where((to_days(now()) - to_days(hiredate))/365) > 35;
mysql> select * from emp_bak;
+-------+--------+-----------+------+------------+---------+---------+--------+
| empno | ename |job | mgr | hiredate | sal | comm | deptno |
+-------+--------+-----------+------+------------+---------+---------+--------+
| 7369 | SIMITH |CLERK | 7902 | 1980-12-17 | 880.00 | NULL | 20 |
| 7499 | ALLEN | SALESMAN | 7698 | 1981-02-20 | 1760.00 | 300.00 | 30 |
| 7521 | WARD | SALESMAN | 7698 | 1981-02-22 | 1375.00 | 500.00 | 30 |
| 7566 | JONES | MANAGER | 7839 | 1981-04-02 | 3272.50 | NULL | 20 |
| 7654 | MARTIN | SALESMAN | 7698 | 1981-09-28 | 1375.00 | 1400.00| 30 |
| 7698 | BLAKE | MANAGER | 7839 | 1981-05-01 | 3135.00 | NULL | 30 |
| 7782 | CLARK | MANAGER | 7839 | 1981-06-09 | 2695.00 | NULL | 10 |
| 7788 | SCOTT | ANALYST | 7566 | 1987-04-19 | 3000.00 | NULL | 20 |
| 7839 | KING | PRESIDENT | NULL | 1981-11-17 | 5500.00| NULL | 10 |
| 7844 | TURNER |SALESMAN | 7698 | 1981-09-08 | 1650.00| NULL | 30 |
| 7876 | ADAMS | CLERK | 7788 | 1987-05-23 | 1100.00 | NULL | 20 |
| 7900 | JAMES | CLERK | 7698 | 1981-12-03 | 1045.00 | NULL | 30 |
| 7902 | FORD | ANALYST | 7566 | 1981-12-03 | 3300.00 | NULL | 20 |
| 7934 | MILLER |CLERK | 7782 | 1982-01-23 | 1430.00| NULL | 10 |
+-------+--------+-----------+------+------------+---------+---------+--------+
mysql> select * from emp;
+-------+--------+-----------+------+------------+---------+---------+--------+
| empno | ename |job | mgr | hiredate | sal | comm | deptno |
+-------+--------+-----------+------+------------+---------+---------+--------+
| 7369 | SIMITH |CLERK | 7902 | 1980-12-17 | 800.00 | NULL | 20 |
| 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 |
+-------+--------+-----------+------+------------+---------+---------+--------+
附录
bjpowernodeSQL文
drop table if exists dept;
drop table if exists salgrade;
drop table if exists emp;
create table dept(
deptno int(10) primary key,
dname varchar(14),
loc varchar(13)
);
create table salgrade(
grade int(11),
losal int(11),
hisal int(11)
);
create table emp(
empno int(4) primary key,
ename varchar(10),
job varchar(9),
mgr int(4),
hiredate date,
sal double(7,2),
comm double(7,2),
deptno int(2)
);
insert into dept(deptno,dname,loc) values(10,'ACCOUNTING','NEW YORK');
insert into dept(deptno,dname,loc) values(20,'RESEARCHING','DALLAS');
insert into dept(deptno,dname,loc) values(30,'SALES','CHICAGO');
insert into dept(deptno,dname,loc) values(40,'OPERATIONS','BOSTON');
insert into salgrade(grade,losal,hisal) values(1,700,1200);
insert into salgrade(grade,losal,hisal) values(2,1201,1400);
insert into salgrade(grade,losal,hisal) values(3,1401,2000);
insert into salgrade(grade,losal,hisal) values(4,2001,3000);
insert into salgrade(grade,losal,hisal) values(5,3001,5000);
insert into emp(empno,ename,job,mgr,hiredate,sal,comm,deptno)
values(7369,'SIMITH','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',7698,'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,null,30);
insert into emp(empno,ename,job,mgr,hiredate,sal,comm,deptno)
values(7876,'ADAMS','CLERK',7788,'1987-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);
select * from dept;
select * from salgrade;
select * from emp;
13题SQL文
drop table if exists s;
drop table if exists c;
drop table if exists sc;
create table s(
sno int(10) primary key,
sname varchar(14)
);
create table c(
cno int(10) primary key,
cname varchar(14),
cteacher varchar(14)
);
create table sc(
sno int(10),
cno int(10),
scgrade int(10),
primary key(sno,cno)
);
insert into s(sno,sname) values(1,'a');
insert into s(sno,sname) values(2,'b');
insert into s(sno,sname) values(3,'c');
insert into s(sno,sname) values(4,'d');
insert into c(cno,cname,cteacher) values(1,'java','王老师');
insert into c(cno,cname,cteacher) values(2,'C++','张老师');
insert into c(cno,cname,cteacher) values(3,'C#','李老师');
insert into c(cno,cname,cteacher) values(4,'mysql','周老师');
insert into c(cno,cname,cteacher) values(5,'oracle','黎明');
insert into sc(sno,cno,scgrade) values(1,1,50);
insert into sc(sno,cno,scgrade) values(1,2,50);
insert into sc(sno,cno,scgrade) values(1,3,50);
insert into sc(sno,cno,scgrade) values(2,2,80);
insert into sc(sno,cno,scgrade) values(2,3,70);
insert into sc(sno,cno,scgrade) values(2,4,59);
insert into sc(sno,cno,scgrade) values(3,1,60);
insert into sc(sno,cno,scgrade) values(3,2,61);
insert into sc(sno,cno,scgrade) values(3,3,99);
insert into sc(sno,cno,scgrade) values(3,4,100);
insert into sc(sno,cno,scgrade) values(3,5,52);
insert into sc(sno,cno,scgrade) values(4,3,82);
insert into sc(sno,cno,scgrade) values(4,4,99);
insert into sc(sno,cno,scgrade) values(4,5,40);
select * from s;
select * from c;
select * from sc;