关于oracle样例数据库emp、dept、salgrade的mysql脚本复杂查询分析
大家可以自行网上找资源(网上资源比较多,不建议下载我的),也可以在我这里下载:
1.取得每个部门最高薪水的人员名称:正确 一共有4个单位,要进行左外连接 其中一个单位没得员工
SELECT dept.dname, emp.ename, emp.sal
FROM emp
RIGHT JOIN dept ON emp.deptno = dept.deptno
RIGHT JOIN (
SELECT dept.dname, MAX(emp.sal) AS 'max_sal'
FROM emp
RIGHT JOIN dept ON emp.deptno = dept.deptno
GROUP BY dept.dname
)AS tt
ON tt.dname = dept.dname AND tt.max_sal = emp.sal
GROUP BY dname;
2.哪些人的薪水在部门平均薪水之上:不确定
SELECT dept.dname, emp.ename, emp.sal
FROM emp
INNER JOIN dept ON emp.deptno = dept.deptno
INNER JOIN (
SELECT dept.dname, AVG(emp.sal) AS 'avg_sal'
FROM emp
INNER JOIN dept ON emp.deptno = dept.deptno
GROUP BY dname
)AS tt
ON tt.dname = dept.dname AND tt.avg_sal <= emp.sal;
3.取得部门中所有人的平均薪水等级
SELECT tt.dname, salgrade.grade
FROM
(
SELECT dept.dname, AVG(emp.sal) AS 'avg_sal'
FROM emp
RIGHT JOIN dept ON emp.deptno = dept.deptno
GROUP BY dname
) AS tt,
salgrade
WHERE tt.avg_sal >= salgrade.losal AND tt.avg_sal <= hisal;
4.不准使用组函数(MAX),取得最高薪水
SELECT emp.ename, emp.sal
FROM emp
ORDER BY sal DESC -- 由高到底, asc 由低到高
LIMIT 0, 1;
5.取得平均薪水最高的部门的部门编号
SELECT deptno
FROM
(
SELECT dept.deptno, AVG(emp.sal) AS 'avg_sal'
FROM emp
INNER JOIN dept ON emp.deptno = dept.deptno
GROUP BY dname
ORDER BY avg_sal DESC
LIMIT 0, 1
)AS tt;
6.取得平均薪水最高的部门的部门名称
SELECT dname
FROM
(
SELECT dept.dname, AVG(emp.sal) AS 'avg_sal'
FROM emp
INNER JOIN dept ON emp.deptno = dept.deptno
GROUP BY dname
ORDER BY avg_sal DESC
LIMIT 0, 1
)AS tt;
7.求平均薪水的等级最低的部门的部门名称
SELECT dname
FROM
(
SELECT t1.dname , SALGRADE.`GRADE`
FROM
(
SELECT dept.dname, AVG(emp.sal) AS 'avg_sal'
FROM emp
INNER JOIN dept ON emp.deptno = dept.deptno
GROUP BY dname
) AS t1 , SALGRADE
WHERE t1.avg_sal >= SALGRADE.`LOSAL` AND t1.avg_sal <= SALGRADE.`HISAL`
)AS tt
WHERE tt.grade =
(
SELECT MIN(SALGRADE.`GRADE`)
FROM
(
SELECT dept.dname, AVG(emp.sal) AS 'avg_sal'
FROM emp
INNER JOIN dept ON emp.deptno = dept.deptno
GROUP BY dname
) AS t1 , SALGRADE
WHERE t1.avg_sal >= SALGRADE.`LOSAL` AND t1.avg_sal <= SALGRADE.`HISAL`
);
8.取得比普通员工(员工代码没有在mgr上出现的)的最高薪水
SELECT emp.ename
FROM emp
WHERE emp.mgr IS NULL
AND sal >
(
SELECT MAX(sal) FROM emp WHERE mgr IS NOT NULL
);
9.取得薪水最高的5名员工
SELECT emp.`EMPNO` AS '编号', emp.`ENAME` AS '姓名'-- , emp.`SAL` as '薪资'
FROM emp
ORDER BY emp.`SAL` DESC
LIMIT 0, 5;
10.取得薪水最高的第六到第十名员工
SELECT emp.`EMPNO` AS '编号', emp.`ENAME` AS '姓名'-- , emp.`SAL`
FROM emp
ORDER BY emp.`SAL` DESC
LIMIT 5, 5;
11.取得最后入职的5名员工
SELECT tt.no AS '编号', tt.name AS '姓名'-- , tt.days
FROM
(
SELECT emp.`EMPNO` AS 'no', emp.`ENAME` AS 'name', TO_DAYS(HIREDATE) AS 'days'
FROM emp
) AS tt
ORDER BY days DESC
LIMIT 0, 5;
12.取得每个薪水等级有多少员工
SELECT salgrade.`GRADE` AS '薪资等级', COUNT(tt.`EMPNO`) AS '员工数'
FROM salgrade,
(
SELECT emp.`EMPNO`, emp.`SAL`
FROM emp
) AS tt
WHERE tt.`SAL` >= salgrade.`LOSAL` AND tt.`SAL` <= salgrade.`HISAL`
GROUP BY salgrade.`GRADE`;
13.列出所有员工及领导的名字
SELECT emp.`ENAME` AS '员工名称', tt.lname AS '领导名称'
FROM
emp LEFT JOIN
(
SELECT emp.`EMPNO`, emp.`ENAME` AS 'lname'
FROM emp
WHERE empno IN
(
SELECT mgr FROM emp
) -- 找出所有是领导的员工no
) AS tt
ON emp.`MGR` = tt.empno;
14.列出受雇日期早于直接上级的所有员工编号、姓名、部门
SELECT t2.eno AS '编号', t2.ename AS '姓名', dept.`DNAME` AS '部门名称', t2.ehiredate AS '员工受雇日期', t2.lhiredate AS '领导受雇日期'
FROM dept
INNER JOIN
(
SELECT emp.`EMPNO` AS 'eno', emp.`ENAME`, emp.`HIREDATE` AS 'ehiredate', t1.lhiredate, emp.`DEPTNO`
FROM
emp LEFT JOIN
(
SELECT emp.`EMPNO` AS 'lno', emp.`HIREDATE` AS 'lhiredate'
FROM emp
WHERE empno IN
(
SELECT mgr FROM emp
) -- 找出所有是领导的员工no
) AS t1
ON emp.`MGR` = t1.lno
) AS t2
ON t2.deptno = dept.`DEPTNO`
WHERE t2.ehiredate < t2.lhiredate OR t2.lhiredate IS NULL;
15.列出部门名称和这些部门的员工信息,同时列出那些没有员工的部门
SELECT dept.`DNAME` AS '部门名称', emp.`ENAME` AS '员工名称', emp.`JOB` AS '职位', emp.`HIREDATE` AS '入职时间', emp.`SAL` AS '薪水'
FROM dept LEFT JOIN emp ON dept.`DEPTNO` = emp.`DEPTNO`
16.列出至少有5个员工的所有部门
SELECT dept.`DNAME` AS '部门名', COUNT(emp.`EMPNO`) AS '员工个数'
FROM dept LEFT JOIN emp ON dept.`DEPTNO` = emp.`DEPTNO`
GROUP BY dept.`DNAME` HAVING COUNT(emp.`EMPNO`) >= 5;
17.列出薪水比“SMITH”多的所有员工信息
SELECT *
FROM emp
WHERE emp.`SAL` >
(
SELECT emp.`SAL`
FROM emp
WHERE emp.`ENAME` = 'SMITH'
);
18.列出所有“CLERK”办事员的姓名及其部门名称,部门人数
SELECT emp.`ENAME` AS '姓名', dept.`DNAME` AS '部门名称', tt.emp_count AS '部门人数'
FROM emp LEFT JOIN dept ON emp.`DEPTNO` = dept.`DEPTNO`
INNER JOIN
(
SELECT dept.`DEPTNO`, COUNT(emp.`EMPNO`) AS 'emp_count'
FROM dept LEFT JOIN emp ON dept.`DEPTNO` = emp.`DEPTNO`
GROUP BY dept.`DEPTNO`
) AS tt
ON emp.`DEPTNO` = tt.deptno
WHERE emp.`JOB` = 'CLERK';
19.列出最低薪水大于1500的各种工作及从事此工作的全部雇员人数
SELECT emp.`JOB` AS '工作名称', COUNT(emp.`EMPNO`) AS '雇员人数'
FROM emp
WHERE emp.`JOB` IN (
SELECT emp.`JOB`
FROM emp
GROUP BY emp.`JOB` HAVING MIN(emp.`SAL`) >1500
)
GROUP BY emp.`JOB`;
-- 20.列出在部门“SALES”<销售部>工作的员工姓名(假定不知道销售部门的部门编号)
SELECT emp.`ENAME` AS '销售部人员'
FROM emp
WHERE emp.`DEPTNO` = (
SELECT dept.`DEPTNO`
FROM dept
WHERE dept.`DNAME` = 'sales'
);
21.列出薪水高于公司平均薪水的所有员工,所在部门、上级领导、雇员的工资等级
SELECT t1.ename AS '员工名称', dept.`DNAME` AS '部门名称', t2.ename AS '上级名称', t1.grade AS '工资等级', t1.sal AS '工资'
FROM
(
SELECT emp.`EMPNO`, emp.`DEPTNO`, emp.`ENAME`, emp.`SAL`, emp.`MGR`,salgrade.`GRADE`
FROM emp, salgrade
WHERE emp.`SAL` > (
SELECT AVG(sal) AS 'avg_sal'
FROM emp
) AND emp.`SAL` >= salgrade.`LOSAL` AND emp.`SAL` <= salgrade.`HISAL`
) AS t1
LEFT JOIN
(
SELECT emp.`EMPNO` AS 'mgr', emp.`ENAME` -- 注意这里是查出所有领导的empno,作为mgr编号
FROM emp
WHERE emp.`EMPNO` IN
(
SELECT DISTINCT(emp.`MGR`) -- 必须要distinct 不然查出来的mgr有重复,后面上层再查重复值接近笛卡尔积
FROM emp
WHERE mgr IS NOT NULL
)
) AS t2
ON t1.mgr = t2.mgr
LEFT JOIN dept ON t1.deptno = dept.`DEPTNO`;
22.列出“SCOTT”从事相同工作的所有员工及部门名称
SELECT emp.`EMPNO` AS '员工编号', emp.`ENAME` AS '员工名称', dept.`DNAME` AS '部门名称'-- , emp.`DEPTNO`
FROM emp, dept
WHERE emp.`JOB` = (
SELECT emp.`JOB` -- 查询出SCOTT从事的职业
FROM emp
WHERE emp.`ENAME` = 'SCOTT'
) AND emp.`ENAME` <> 'SCOTT'
AND emp.`DEPTNO` = dept.`DEPTNO`;
23.列出薪水等于部门30中员工的薪水的其它员工的姓名和薪水
SELECT emp.`EMPNO` AS '员工编号', emp.`ENAME` AS '员工名称', emp.`SAL` AS '员工薪水'
FROM emp
WHERE emp.`DEPTNO` <> 30
AND emp.`SAL` IN(
SELECT DISTINCT emp.`SAL` -- 查询出部门为30的所有的员工的sal 不重复
FROM emp
WHERE emp.`DEPTNO` = 30
);
24.列出薪金高于在部门 30 工作的所有员工的薪金的员工姓名和薪金、部门名称
SELECT emp.`EMPNO` AS '员工编号', emp.`ENAME` AS '员工名称', emp.`SAL` AS '员工薪水', dept.`DNAME` AS '部门名称'
FROM emp, dept
WHERE emp.`DEPTNO` <> 30
AND emp.`SAL` >(
SELECT MAX(emp.`SAL`) -- 查询出部门为30的所有的员工的sal 不重复
FROM emp
WHERE emp.`DEPTNO` = 30
)
AND dept.`DEPTNO` = emp.`DEPTNO`;
25. 列出在每个部门工作的员工数量、平均工资和平均服务期限
SELECT dept.`DNAME` AS '部门名称', COUNT(emp.`EMPNO`) AS '员工数量', ROUND(AVG(emp.`SAL`), 2) AS '平均工资', t2.avg_year AS '平均服务年限'
FROM dept LEFT JOIN emp ON dept.`DEPTNO` = emp.`DEPTNO`
LEFT JOIN
(
SELECT t1.deptno, ROUND(AVG(t1.year), 0) AS 'avg_year' -- 四舍五入,保留0位小数
FROM
(
SELECT emp.`DEPTNO`, ROUND((TO_DAYS(NOW())-TO_DAYS(emp.`HIREDATE`))/366, 0) AS 'year'
FROM emp
) AS t1
GROUP BY t1.deptno
) AS t2
ON dept.`DEPTNO` = t2.deptno
GROUP BY dept.`DEPTNO`;
26. 列出所有员工的姓名、部门名称和工资
SELECT emp.`ENAME` AS '员工名称', dept.`DNAME` AS '部门名称', emp.`SAL` AS '工资'
FROM emp, dept
WHERE dept.`DEPTNO` = emp.`DEPTNO`;
27. 列出所有部门的详细信息和人数
SELECT dept.`DEPTNO` AS '部门编号', dept.`DNAME` AS '部门名称', dept.`LOC` AS '部门地址', tt.num AS '部门人数'
FROM dept LEFT JOIN
(
SELECT emp.`DEPTNO`, COUNT(emp.`EMPNO`) AS 'num'
FROM emp
GROUP BY emp.`DEPTNO`
) AS tt
ON dept.`DEPTNO` = tt.`DEPTNO`;
28. 列出各种工作的最低工资及从事此工作的雇员姓名
SELECT emp.`SAL`, emp.`ENAME`
FROM emp,
(
SELECT emp.`JOB`, MIN(emp.`SAL`) AS 'min_sal'
FROM emp
GROUP BY emp.`JOB`
)AS tt
WHERE emp.`JOB` = tt.job AND emp.`SAL` = tt.min_sal;
29. 列出各个部门 MANAGER 的最低薪金
SELECT dept.`DNAME` AS '部门名称', MIN(tt.sal) AS '最低薪资'
FROM dept LEFT JOIN
(
SELECT emp.`DEPTNO`, emp.`SAL`
FROM emp
WHERE emp.`JOB` = 'manager'
) AS tt
ON dept.`DEPTNO` = tt.deptno
GROUP BY dept.`DEPTNO`;
30. 列出所有员工的年工资,按年薪从低到高排序
SELECT emp.`EMPNO` AS '员工编号', emp.`ENAME` AS '员工名称', (emp.`SAL`*12) AS '年薪'
FROM emp
ORDER BY 年薪 ASC;
31. 求出员工领导的薪水超过 3000 的员工名称和领导名称
SELECT tt.ename AS '领导名称', emp.`ENAME` AS '雇员名称'
FROM emp RIGHT JOIN
(
SELECT emp.`EMPNO`, emp.`ENAME`
FROM emp
WHERE emp.`EMPNO` IN
(
SELECT DISTINCT emp.`MGR`
FROM emp
WHERE emp.`MGR` IS NOT NULL
) AND emp.`SAL` >= 3000
) AS tt
ON emp.`MGR` = tt.empno
32. 求部门名称中带“S”字符的部门员工的工资合计、部门人数
SELECT tt.dname AS '部门名称', SUM(emp.`SAL`) AS '工资合计', COUNT(emp.`EMPNO`) AS '部门人数'
FROM emp RIGHT JOIN
(
SELECT dept.`DEPTNO`, dept.`DNAME`
FROM dept
WHERE dept.`DNAME` LIKE '%s%'
) AS tt
ON emp.`DEPTNO` = tt.deptno
GROUP BY emp.`DEPTNO`;
32+.查询名字中有s的员工的工资总和(按照部门进行分组)
SELECT dept.`DNAME` AS '部门名称', SUM(tt.`SAL`) AS '工资'
FROM dept LEFT JOIN
(
SELECT emp.`EMPNO`, emp.`ENAME`, emp.`SAL`, emp.`DEPTNO`
FROM emp
WHERE emp.`ENAME` LIKE '%s%'
) AS tt
ON dept.`DEPTNO` = tt.deptno
GROUP BY dept.`DEPTNO`;
33. 给任职日期超过 30 年的员工加薪 10%
UPDATE emp SET emp.`SAL` = emp.`SAL` + emp.`SAL`*10
WHERE emp.`EMPNO` IN(
SELECT tt.`EMPNO`
FROM (
SELECT emp.`EMPNO`, ROUND((TO_DAYS(NOW())-TO_DAYS(emp.`HIREDATE`))/366, 0) AS 'work_year'
FROM emp
) AS tt
WHERE tt.work_year > 30
);
关于oracle样例数据库emp、dept、salgrade的mysql脚本复杂查询分析的更多相关文章
- oracle学习创建和准备Oracle样例数据库
此处有一个Oracle创建数据库和表和插入数据的脚本: http://www.forta.com/books/0672336073/
- mysql样例数据库employees
Oracle和sqlserver都有基于员工信息的样例数据库,同样mysql上也是有的. 给出一个连接地址https://github.com/datacharmer/test_db. 下载后直接调用 ...
- Sakila——MySQL样例数据库解析(已经迁移)
一.Introduction Sakila可以作为数据库设计的参考,也可作为实验数据.我是用作数据仓库和ODI学习的实验数据. The Sakila sample database was devel ...
- SQL-AdventureWorks样例数据库
1. 下载样例数据库文件 输入网址:http://www.codeplex.com/ , 搜索:microsoft sql server product samples 下载对应数据库的Adventu ...
- SQL SEVER 2008中的演示样例数据库
SQL SEVER 2008数据库是什么我就不说了,我在这里分享一下怎样学习SQL SEVER 2008数据库,假设是对数据库或是SQL SEVER 数据库全然陌生或是不熟悉的人来说,建议看看一些视频 ...
- Android SqlDelight具体解释和Demo样例
一.简单介绍 SQLDelight 和 SqlBrite 是 Square 公司推出的一个 Android 平台数据库解决方式. 在了解这个两个东西前,必须先得有Andorid的Sqlite的知识(S ...
- MySQL、SqlServer、Oracle 三种数据库的优缺点
MySQL.SqlServer.Oracle 三种数据库的优缺点 一.MySQL 优点: 缺点: 二.SqlServer 优点: 缺点: 三.Oracle 优点: 缺点: 一.MySQL 优点: 体积 ...
- Deinstall卸载RAC之Oracle软件及数据库+GI集群软件
Deinstall卸载Oracle软件及数据库+GI集群软件 1. 本篇文档应用场景: 需要安装新的ORACLE RAC产品,系统没有重装,需要对原环境中的RAC进行卸载: #本篇文档,在AIX 6. ...
- [Oracle]数据库的Control File 取Dump后的样例
[Oracle]数据库的Control File 取Dump后的样例: 片段截取-------------------------------(size = 40, compat size = 40, ...
随机推荐
- Spring常见问题(五)
1.静态资源访问配置 绝对路径:访问静态资源. <mvc:resources location="/js/" mapping="/js/**">&l ...
- 关于java.lang.IllegalMonitorStateException异常说明(四)
1.异常原因及解释 首先你要了解这个异常为什么会抛出,这个异常会在三种情况下抛出:1>当前线程不含有当前对象的锁资源的时候,调用obj.wait()方法;2>当前线程不含有当前对象的锁资源 ...
- Maven作用及应用
1.简介 Maven是一个项目管理的Java 工具,在JavaEE中,我们可以使用Maven方便地管理团队合作的项目,现在我们在学习JavaEE框架,使用Maven可以管理类库,有效方便地供团队中的其 ...
- 【Linux服务器双IP配置】如何实现不同IP的双网卡同时上网?
一.环境和知识预备 我遇到问题的生产机器是CentOS release 6.8系统,不过这并不影响问题的解决,本质上都是一样的. 网关:一个网络连接到另一个网络的关口,也就是实现网络互连,俗称网络连接 ...
- python3中文件/IO编程
python3的文件操作可谓是我见过所有语言中最舒服的,那我们来一起看一下py3中的文件操作. 1:文件的打开方式有以下几种: 注:以上图表参考菜鸟教程 2:定位读写文件 f = open(&quo ...
- Jmeter关联详解
关联的概念 从上一步操作中获取需要的值,传递给下一步操作中进行引用,形成自动关联,而不是 每次操作都去手动修改关联的值.常见的场景有SessionID.Session Token值的获取. 正则表达式 ...
- 等Excel工作簿关闭后自动加密压缩备份
;; 等Excel工作簿关闭后自动加密压缩备份.ahk;; 腾讯QQ号 595076941; 作者:boai; 写作日期:2019年5月15日; 版本号:第1版; 手机号(中国移动) 138##### ...
- 【Android】真机调试新姿势:无线连接
由于工作需要,需要无线连接手机调试,特意百度了一下 在进行Android开发时,一般我们都是用usb线把手机和电脑连接起来进行调试工作.但如果你觉得这样不够酷的话,可以尝试一下无线连接,颇简单,GO! ...
- vue 源码详解(二): 组件生命周期初始化、事件系统初始化
vue 源码详解(二): 组件生命周期初始化.事件系统初始化 上一篇文章 生成 Vue 实例前的准备工作 讲解了实例化前的准备工作, 接下来我们继续看, 我们调用 new Vue() 的时候, 其内部 ...
- awk-05-数组
数组 数组: 存储一系列相同类型的元素,键/值方式存储,通过下标(键)来访问值 awk 中数组称为关联数组,不仅可以使用数字作为下标,还可以使用字符串作为下标 数组元素的键和值存储在 awk 程序内部 ...