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 正文 下图是示例用到的数据表信息 ...
随机推荐
- IOS 集成友盟分享
#import <Foundation/Foundation.h> @interface UMSocialSinaHandler : NSObject +(void)openSSOWith ...
- Arcgis Javascript中geometryEngine报错’hq‘of undefined的解决方法
这个问题困扰了我一个星期,原因是使用geomagicbuffer时候,有的线可正常使用,有的就直接报错,一直没有解决,后来发现是api自己的bug导致的 干脆直接读代码,在geometryEngine ...
- CF449C Jzzhu and Apples
嘟嘟嘟 这道题正解是怎么对的其实我也不清楚,总之靠感性理解吧. 首先当然要把1到n / 2的素数都筛出来,因为两两能配对的数一定都是这些素数的倍数.这也就说明对于(n / 2, n]的素数,他们一定不 ...
- 课堂笔记-------字符串类型string------练习
字符串类型 一.string //打出s.时就会出现一堆的方框,要找不带箭头的(不带箭头的是我们现在可以用的到的),不要找带箭头的(带箭头的是扩展,现在还用不到) //不带箭头的都是对s的操作(动作和 ...
- 给自己的网站加上robots.txt
今天给自己的网站加了一个robots.txt,在网上收集整理了一些资料,给自己网站也加上了robots.txt ! 顺便给大家分享一下! 一.robots.txt是什么? robots.txt是一个纯 ...
- Win7系统中哪些服务可以关闭?
Win7系统中很多服务和进程其实大家在平时都用不上,这些服务和进程不仅占用了很大部分的资源,而且还会影响系统的运行速度和安全.我们可以关闭那些不常用的服务和进程来加快系统运行速度,但对于一般的用户来说 ...
- mysql时间日期函数
now(), current_timestamp(); -- 当前日期时间 current_date(); -- 当前日期 current_time(); -- 当前时间 date('yyyy-mm- ...
- notepad++括号自动补全插件: XBracket Lite
1.4.5.1. 通过XBracket Lite实现括号的自动补全 先去打开相应的设置: 再根据自己的需要去设置: 其中解释一下相应的选项的含义: Treat'' as brackets 把单引号', ...
- 02_Linux 终端命令格式
01. 终端命令格式 command [-options] [parameter] 说明: command:命令名,相应功能的英文单词或单词的缩写 [-options]:选项,可用来对命令进行控制,也 ...
- Tomcat 启动速度优化
创建一个web项目 选择发布到 汤姆猫 的下面 deploy path: 表示发布到的文件名称 把项目添加到 tomcat 里,运行,我们可以在 tomcat里找到我们发布的项目: 现在启动时间: 现 ...