mysql学习第四天(高级查询)
-- 第七章
-- 1、查询入职日期最早和最晚的日期
select min(hiredate),max(hiredate)
from emp
-- 2、查询职位以SALES开头的所有员工平均工资,最低工资,最
-- 高工资,工资和
select avg(sal),min(sal),max(sal),sum(sal)
from emp
where job like 'SALES%'
-- 3、查询部门30有多少个员工
select count(*)
from emp
where deptno = '30'
-- 4、查询有员工的部门数量
select count(distinct deptno)
from emp
-- 5、求奖金的平均值(包括没有奖金的人)
select avg(ifnull(comm,0))
from emp
-- 练习1
-- 1、查询部门20的员工,每个月的工资总和及平均工资
select sum(sal),avg(sal)
from emp
where deptno = 20
-- 2、查询工作在CHICAGO的员工人数,最高工资及最低工资
select count(*),max(sal),min(sal)
from emp,dept
where emp.deptno = dept.deptno
and loc = 'CHICAGO'
-- 3、查询员工表中一共有几种岗位类型
select count(distinct job)
from emp
-- 6、查询每个部门的编号,平均工资
select deptno,avg(sal)
from emp
group by deptno
-- 7、查询每个岗位的工资总和
select deptno,job,sum(sal)
from emp
group by deptno,job
-- 1、查询每个部门的部门号,部门名称,部门人数,最高工资,
-- 最低工资,工资总和,平均工资
select emp.deptno,dname,count(*),max(sal),min(sal),sum(sal),avg(sal)
from emp,dept
where emp.deptno = dept.deptno
group by emp.deptno,dname
-- 2、查询每个部门,每个岗位的部门编号,部门名称,
-- 岗位名称,部门人数,最高工资,最低工资,工资总和,
-- 平均工资
select emp.deptno,dname,job,count(*),max(sal),min(sal),sum(sal),avg(sal)
from emp,dept
where emp.deptno = dept.deptno
group by emp.deptno,dname,job
-- 3、查询每个经理所管理的人数,经理编号,经理姓名,
-- 要求包括没有经历的人员信息
select count(*),t1.mgr,t2.ename
from emp t1
left join emp t2
on t1.mgr = t2.empno
group by t1.mgr,t2.ename
-- 8、查询每个部门工资大于2900的部门编号,最高工资
select deptno,max(sal)
from emp
group by deptno
having max(sal) > 2900
-- 9、查询不是以SALES开头,工资总和大于5000的职位名称及
-- 工资总和以工资总和升序排序
select job,sum(sal)
from emp
where job not like 'SALES%'
group by job
having sum(sal) > 5000
order by sum(sal)
-- 练习3
-- 1、查询部门人数大于2的部门编号,部门名称,部门人数
select dept.deptno,dname,count(*)
from dept,emp
where dept.deptno = emp.deptno
group by dept.deptno,dname
having count(*) > 2
-- 2、查询部门平均工资大于2000,并且人数大于2的部门编号
-- 部门名称,部门人数,部门平均工资,并按照部门人数升序排列
select dept.deptno,dept.dname,count(*),avg(sal)
from dept,emp
where dept.deptno = emp.deptno
group by dept.deptno,dept.dname
having avg(sal) > 2000
and count(*) >2
order by count(*)
-- 10、查出比JONES工资高的其它员工
select *
from emp
where sal >(select sal
from emp
where ename = 'JONES')
-- 11、显示和雇员7369从事相同工作并且工资大于雇员7876的雇员的姓名和工作
select ename,job
from emp
where job = (select job
from emp
where empno = 7369)
and sal > (select sal
from emp
where empno = 7876)
-- 12、查询工资最低的员工姓名,岗位及工资
select ename,job,sal
from emp
where sal = (select min(sal)
from emp)
-- 13、查询部门最低工资比20部门最低工资高的部门编号及最低工资
select deptno,min(sal)
from emp
group by deptno
having min(sal) > (select min(sal)
from emp
where deptno = 20)
-- 练习4
-- 1、查询入职日期最早的员工姓名,入职日期
select ename,hiredate
from emp
where hiredate = (select min(hiredate)
from emp)
-- 2、查询工资比SMITH工资高并且工作地点在CHICAGO的员工姓名,工资,部门名称
select ename,sal,dname
from emp,dept
where emp.deptno = dept.deptno
and loc = 'CHICAGO'
and sal > (select sal
from emp
where ename = 'SMITH')
-- 3、查询入职日期比20部门入职日期早的员工还要早的员工姓名,入职日期
select min(hiredate)
from emp
group by deptno
having min(hiredate) < (select min(hiredate)
from emp
where deptno = 20)
-- 4、查询部门人数大于所有部门平均人数的部门编号,部门名称,部门人数
select dept.deptno,dept.dname,count(*)
from emp,dept
where emp.deptno = dept.deptno
group by deptno
having count(*) > (select count(*)/count(distinct deptno)
from emp)
select dept.deptno,dept.dname,count(*)
from emp,dept
where emp.deptno = dept.deptno
group by deptno
having count(*) > (select avg(t.count)
from (select count(*) count
from emp
group by deptno)t)
-- 14、查询是经历的员工姓名,工资
select distinct t2.ename,t2.sal
from emp t1,emp t2
where t1.mgr = t2.empno
select ename,sal
from emp
where empno in (select mgr
from emp)
-- 15、查询部门编号不为10,且工资比10部门任意一名员工工资高的员工编号,姓名,职位,工资
select empno,ename,job,sal
from emp
where deptno <> 10
and sal >any (select sal
from emp
where deptno = 10)
-- 16、查询部门编号不为20,且工资比20部门所有员工工资高的员工编号,姓名,职位,工资
select empno,ename,job,sal
from emp
where sal > all(select sal
from emp
where deptno = 20)
and deptno <> 20
-- 练习5
-- 1、查询入职日期比10部门任意一个员工晚的员工姓名、入职日期,不包括10部门员工
select ename,hiredate
from emp
where hiredate <any (select hiredate
from emp
where deptno = 10)
and deptno <> 10
-- 2、查询入职日期比10部门所有员工晚的员工姓名,入职日期,不包括10部门员工
select ename,hiredate
from emp
where hiredate <all (select hiredate
from emp
where deptno = 10)
and deptno <> 10
-- 3、查询职位和10部门任意一个员工职位相同的员工姓名,职位,不包括10部门员工
select ename,job
from emp
where job in (select job
from emp
where deptno = 10)
and deptno <> 10
-- 17、查询不是经理的员工姓名
select ename
from emp
where empno not in(select mgr
from emp
where mgr is not null)
-- 18、查询比自己部门平均工资高的员工姓名,工资,部门编号,部门平均工资
select ename,sal,emp.deptno,avgsal
from emp,(select deptno,avg(sal) avgsal
from emp
group by deptno) t
where emp.deptno = t.deptno
and emp.sal > t.avgsal
select ename,sal,deptno
from emp t
where sal > (select avg(sal)
from emp
where deptno = t.deptno)
select ename,sal,deptno,(select avg(sal)
from emp
where deptno = t.deptno)
from emp t
where sal > (select avg(sal)
from emp
where deptno = t.deptno)
-- 查询员工姓名及其查询所在部门的人数
select ename,c
from emp,(select deptno,count(*) c
from emp
group by deptno) t
where emp.deptno = t.deptno
select ename,(select count(*)
from emp
where emp.deptno = t.deptno
group by emp.deptno)
from emp t
-- 查询是所在部门工资最低的员工信息
select *
from emp
where (deptno,sal) in (select deptno,min(sal)
from emp
group by deptno)
select *
from emp,(select deptno,min(sal) minsal
from emp
group by deptno)t
where emp.deptno = t.deptno
and sal = minsal
select *
from emp t
where sal = (select min(sal)
from emp
where deptno = t.deptno
group by deptno)
mysql学习第四天(高级查询)的更多相关文章
- JAVA / MySql 编程—— 第四章 高级查询(二)
1. EXISTS和NOT EXISTS子查询:EXISTS关键字用来检测数数据库对象是否存在. ★EXISTS和NOT EXISTS的结果只取决于是否 ...
- 我的MYSQL学习心得(七) 查询
我的MYSQL学习心得(七) 查询 我的MYSQL学习心得(一) 简单语法 我的MYSQL学习心得(二) 数据类型宽度 我的MYSQL学习心得(三) 查看字段长度 我的MYSQL学习心得(四) 数据类 ...
- MySql学习笔记四
MySql学习笔记四 5.3.数据类型 数值型 整型 小数 定点数 浮点数 字符型 较短的文本:char, varchar 较长的文本:text, blob(较长的二进制数据) 日期型 原则:所选择类 ...
- 转MYSQL学习(四) 查询
MySQL中select的基本语法形式: select 属性列表 from 表名和视图列表 [where 条件表达式] [group by 属性名[having 条件表达式]] [order by 属 ...
- MySQL学习(四) SQL连接查询
更多情况下,我们查询的数据来源于多张表,所有有必要了解一下MySQL中的连接查询. SQL中将连接查询分成四类:交叉连接,内连接,外连接和自然连接. 数据准备 student表 -- -------- ...
- MySQL学习(四)查询
一.group_concat()函数.把groupby的分组中字段数据组合显示出来 select s_id , GROUP_CONCAT(要显示的字段名) from table group by 分 ...
- Mybatis学习(四)————— 高级映射,一对一,一对多,多对多映射
一.单向和双向 包括一对一,一对多,多对多这三种情况,但是每一种又分为单向和双向,在hibernate中我们就详细解析过这单向和双向是啥意思,在这里,在重复一遍,就拿一对多这种关系来讲,比如有员工和部 ...
- MySQL数据库实验四:嵌套查询
实验四 嵌套查询 一.实验目的 掌握SELECT语句的嵌套使用,实现表的复杂查询,进一步理解SELECT语句的高级使用方法. 二.实验环境 三.实验示例 1. 查询与“刘晨”在同一 ...
- mysql学习2:模糊匹配查询like,regexp,in
mysql模糊匹配查询like,regexp,in 摘要 内容比较简单,无摘要. 关键词 模糊查询 like regexp in contact 正文 下图是示例用到的数据表信息 ...
随机推荐
- tensorflow ImportError: libmklml_intel.so: cannot open shared object file: No such file or directory
通过whl文件安装 tensorflow,显示缺少libmklml_intel.so 需要 1)安装intel MKL库 https://software.intel.com/en-us/articl ...
- python创建项目
一.准备下载 python3.6.6 https://www.python.org/downloads/windows/(需要注意你的电脑是32位还是64位) mysql 5.1.72 https:/ ...
- 从产品展示页面谈谈Hybris的特有概念和设计结构
今天这篇文章来自我的同事,SAP成都研究院Hybris开发团队的开发人员Zhang Jonathan(张健).需要特别介绍的是,张健和成都研究院的其他开发同事不同,张健毕业于电子科技大学,读的专业是英 ...
- Hybris ECP(Enterprise Commerce Platform)的调试
This blog is written to demonstrate how to setup debug environment for Hybris ECP(Enterprise Commerc ...
- 开源重磅,java内容管理系统CMS,点击就可以编辑,保存,轻松构建自己的站点
买的暂时空间不给力.内存不足,老给关闭,先转到京东云上了,免费的,也不知免费多久. 这是地址2 http://java4cms.jd-app.com/index.html 这是地址 http:// ...
- BZOJ3680:吊打XXX(模拟退火)
Description gty又虐了一场比赛,被虐的蒟蒻们决定吊打gty.gty见大势不好机智的分出了n个分身,但还是被人多势众的蒟蒻抓住了.蒟蒻们将 n个gty吊在n根绳子上,每根绳子穿过天台的一个 ...
- CGAL 4.6 - Surface Reconstruction from Point Sets
http://doc.cgal.org/latest/Surface_reconstruction_points_3/ The following example reads a point set, ...
- Entity Framework 五
连接情景中的CRUD操作: 连接场景中的CRUD操作是一项相当简单的任务,因为默认情况下,上下文会自动跟踪实体在其生命周期中发生的更改,前提是AutoDetectChangesEnabled为true ...
- C#基础 一(方法详解)
需要知道:类和方法的关系 方法和参数修饰符 自定义方法可以有或没有参数,也可以有或没有返回值.可以被各种关键字(static.virtual.public.new等)修饰以限制其行为. C#参数修饰符 ...
- Javafxml
FXML入门教程 本部分教程包括两部分内容: 为什么使用FXML(基本介绍以及用FXML创建用户界面的好处): 使用FXML创建用户界面(通过创建简单登录应用来完成本教程部分). 1.1 为何使用FX ...