MySQL从基础到高级篇一
MySQL 服务的登录和退出
方式一:通过mysql 自动的客户端(仅限于root 用户)
方式二:通过windows 自动的命令符窗口
登录:mysql 【-h 主机名 -p 端口号】 -u 用户名 -p密码
退出:exit 或 Ctrl + C
1)、方式一,找到MySQL应用图标打开直接输入密码即可
2)、方式二:在命令行窗口使用命令方式打开 MySQL
2. MySQL 的常见命令
1、查看当前所有数据库
——show databases;
2、打开指定的数据库
——use 库名;
3、查看当前库的所有数据表
——show tables;
4、查看其它库的所有表
——show tables from 库名;
5、创建数据库
——create database 库名;
6、创建数据表
——create table 表名(
列名 类型,
列名 类型,
.......
);
7、查看数据表结构
——desc 表名;
8、查看服务器的版本
①、登录到mysql 服务端
——select version();
②、没有登录到mysql 服务端
——mysql --version/mysql --v
3. MySQL 的语法规范
1、不区别大小写,建议关键字大写,表名、列名小写
2、每条命令最好用分号结尾
3、每条命令根据需要,可以进行缩进,或换行
4、注释
单行注释:#注释文字
单行注释:-- 注释文字
多行注释:/* 注释文字 */
4. MySQL 基础查询语法——select 查询列表 from 表名
#1、查询数据表单个字段
——select 字段名 from 表名;
#2、查询表中多个字段名
——select 字段名1, 字段名2, 字段名3 from 表名;
#3、查询表中所有字段名
——select * from 表名;
#4、查询常量
——select 常量名;
#5、查询表达式
——select 表达式;
#6、查询函数
——select 函数名(参数列表);
#7、给字段起别名
①——select 字段名 as '别名' from 表名;
②——select 字段名 '别名' from 表名;
#8、去重
——select distinct 字段名 from 表名;
#9、“+”号的作用
①运算符:加号两边都是数字的时候(19+32=51)
②拼接符:加号两边只要有一边是字符串('123'+123=123123)
③变null:加号两边只要有一边是null(null+123=null)
#10、concat 的作用
——拼接符:select concat(字段1,字段2) from 表名;
#11、ifnull(参数1, 参数2)
——select ifnull(commission_pet, 0) ##判断commission_pet 是否为null,如果为null 返回0
#12、isnull(参数) ##判断某字段或表达式是否为null,如果是,则返回1,否则为0
——select isnull(commission_pet), commission_pet from employees; ##判断commission_pet 是否为null,如果是,则返回1,否则返回0
5. 先把后面学习要用到的例子表创建
1、departments 表
CREATE TABLE `departments` (
`department_id` int(11) NOT NULL AUTO_INCREMENT COMMENT '部门编号',
`department_name` varchar(255) DEFAULT NULL COMMENT '部门名称',
`manager_id` int(11) DEFAULT NULL COMMENT '部门领导员工号',
`location_id` varchar(255) DEFAULT NULL COMMENT '位置变化',
PRIMARY KEY (`department_id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COMMENT='部门表';
2、employees 表
CREATE TABLE `employees` (
`employee_id` int(11) NOT NULL AUTO_INCREMENT COMMENT '员工编号',
`first_name` varchar(255) DEFAULT NULL COMMENT '姓',
`last_name` varchar(255) DEFAULT NULL COMMENT '名',
`email` varchar(255) DEFAULT NULL COMMENT '邮箱',
`phone_number` varchar(255) DEFAULT NULL COMMENT '电话号码',
`job_id` varchar(255) DEFAULT NULL COMMENT '工种编号',
`salary` varchar(255) DEFAULT NULL COMMENT '月薪',
`commission_pet` varchar(255) DEFAULT NULL COMMENT '奖金率',
`manager_id` varchar(255) DEFAULT NULL COMMENT '上级领导员工编号',
`department_id` varchar(255) DEFAULT NULL COMMENT '部门编号',
`hire_date` varchar(255) DEFAULT NULL COMMENT '入职时间',
PRIMARY KEY (`employee_id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COMMENT='员工表';
3、jobs 表
CREATE TABLE `jobs` (
`jobs_id` int(11) NOT NULL AUTO_INCREMENT,
`job_title` varchar(255) DEFAULT NULL COMMENT '工种名称',
`min_salary` varchar(255) DEFAULT NULL COMMENT '最低薪资',
`max_salary` varchar(255) DEFAULT NULL COMMENT '最高薪资',
PRIMARY KEY (`jobs_id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COMMENT='工种表';
4、locations 表
CREATE TABLE `locations` (
`location_id` int(11) NOT NULL AUTO_INCREMENT COMMENT '位置编号',
`street_address` char(1) DEFAULT NULL COMMENT '街道',
`portal_code` varchar(255) DEFAULT NULL COMMENT '邮编',
`city` varchar(255) DEFAULT NULL COMMENT '城市',
`state_province` varchar(255) DEFAULT NULL COMMENT '州/省',
PRIMARY KEY (`location_id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COMMENT='位置表';
6. 条件查询——select 查询列表 from 表名 where 筛选条件
1)、简单条件运算符:>、<、=、!=、<>、>=、<=
2)、逻辑运算符:&&(and)、||(or)、!(not)
3)、模糊查询:like、between and、in、is null
#一、简单条件运算符筛选:
#案例1、查询工资>9000 的员工信息
——select * from employees where salary>9000;
#案例2、查询部门编号不等于100 号 的员工名和部门编号
——select last_name, department_id from employees where department_id<>100;
#案例3、查询工资=10000 的员工名和部门编号
——select last_name, department_id from employees where salary=10000;
#二、逻辑表达式筛选:
#案例1、查询工资在10000~20000 之间的员工名、工资以及奖金。
——select employee_name, salary, commission_pet from employees where salary>=10000 and salary<=20000;
#案例2、查询部门编号不是在90 到 110,或者工资高于15000 的员工信息
——select * from employees where not(department_id>=90 and department_id<=110) or salary>15000;
#三、模糊查询:
① like(像)
特点:
a:一般和通配符搭配使用
通配符:
% 任意多个字符,包含0个字符
_ 任意单个字符
#案例1、查询员工名中包含字符a 的员工信息
——select * from employees where last_name like '%a%';
#案例2、查询员工名中第三个字符为‘u’,第六个字符为‘a’ 的员工信息
——select * from employees where last_name like '__u__a%';
#案例三、查询员工名中第四个字符为‘_’ 的员工信息(escape定义@为转义符)
——select * from employees where last_name like '___@_%' escape '@';
② between and(在什么之间,包含边界)
特点:
a:提高了SQL 语句的简洁度,方便开发
b:包含临界值
c:不要调换顺序,效果不一样
d:id between A and B 等价于 id >= A and id <= B
e:小值在前,大值在后
#案例1、查询员工编号在100~200 之间 的员工信息
——select * from employees where employee_id between 100 and 200;
③ in (在列表之中)
含义:判断某字段的值是否属于in 列表中的某一项
特点:
a:使用in 提高语句简洁度
b:in 列表的值类型必须一致或兼容
c:in 列表中的内容不支持转义字符和通配符
#案例1、查询员工的工种编号是 ‘abc’,‘aca’,‘tes’,‘ahk’ 之间的员工信息
——select * from employees where job_id in('abc', 'aca', 'tes', 'ahk');
④ is null
特点:
a:简单条件运算符不能判断 null
b:判断null 使用 is null 或 is not null
#案例1、查询没有奖金的员工名和薪资
——select last_name, salary from employees where commission_pet is null;
#案例2、查询有奖金的员工名和奖金率
——select last_name, commission_pet from employees where commission_pet is not null;
⑤ <=> 安全等于(类似于 is null)
特性:
a:可读性比较差
b:扩展了is null,不仅仅可以判断null,也可以判断其他
#案例1、查询没有奖金的员工名和薪资
——select last_name, salary from employees where commission_pet <=> null;
#案例2、查询薪资为12000的员工名和奖金率
——select last_name, commission_pet from employees where salary <=> 12000;
7. 复习数据库理论
一、数据库的好处
1、可以持久化数据到本地
2、结构化操作(CRUD)
二、数据库的常见概念
1、DB(DataBase):数据库
2、DBMS(DataBase Manage System):数据库管理系统
3、SQL(Structured Query Language):结构化查询语言,用于和数据库通信的语言,不是某个数据库软件特有的,而是几乎所有的主流数据库软件通用的语言。
三、数据库存储数据的特点
1、数据存放到表中,然后表在存放到数据库中
2、一个数据库中可以有多张表,每张表具有唯一的表名用来表示自己(主键)
3、表中有一个或多个列,列又称为“字段”,类似于Java中的“属性”
4、表中的每一行数据,相当于Java中的“对象”
四、MySQL 的背景
前身属于瑞典的一家公司,MySQL AB,08年被sun公司收购,09年sun公司被oracle公司收购
五、MySQL 的优点
1、开源、免费、成本低
2、性能高、可移植性强
3、体积小,便于安装
六、MySQL 服务的启动与停止
1、通过命令行
net start 服务名
net stop 服务名
2、计算机——右击——管理——服务——关闭/启动
七、MySQL 服务的登录和退出
1、登录:mysql -h主机名 -p端口号 -u用户名 -p密码
2、退出:exit、Ctrl + C
8. 排序查询——在查询过程中把数据进行排序处理
语法:
select 查询列表
from 表名
【where 筛选条件】
order by 排序列表【asc | desc】
特点:
1、asc 代表的是升序,desc 代表的是降序,如果不写,默认升序。
2、order by 子句中可以支持单个字段,多个字段,表达式,函数,别名均可。
3、order by 子句一般是放在查询语句的后面,limit 子句除外
#案例1、查询员工信息,要求工资从高到底排序【添加筛选条件】
——select * from employees order by salary DESC;
#案例2、查询员工编号>40 的员工信息,并按照入职时间的先后进行排序
——select * from employees employee_id < 40 order by hire_date ASC;
#案例3、按年薪的高低显示员工的信息和年薪【按表达式排序】
——select *, salary*(1+ifnull(commission_pet , 0))*12
from employees salary*(1+ifnull(commission_pet , 0))*12
order by asc;
#案例4、按年薪的高低显示员工的信息和年薪【按别名排序】
——select *, salary*(1+ifnull(commission_pet , 0))*12 as 年薪 from employees order by 年薪 asc;
#案例5、按姓名的长度显示员工的姓名和工资【按函数排序】
——select length(last_name), salary
from employees
order by length(last_name) asc;
#案例6、查询员工信息,要求先按工资升序,再按员工编号降序【多个字段排序】
——select *
from employees
order by salary asc, employee_id desc;
#案例7、查询员工的姓名和部门号和年薪,按年薪降序,按姓名升序
——select last_name, department_id, salary*12*(1+ifnull(commission_pet, 0)) 年薪
from employees
order by 年薪 desc, last_name asc;
#案例8、选择工资不在8000~13000 的员工姓名和工资,按工资降序
——select last_name, salary
from employees
where salary not between 8000 and 13000
order by salary desc;
#案例9、查询邮箱字段中包含‘e’字符的员工信息,并先按邮箱的字节长度降序,再按部门号升序
——select *
from employees
where email like '%e%'
order by length(email) desc, department_id asc;
9. 常见函数
功能:
类似于java的方法,将一组逻辑语句封装在方法体重,对外暴露方法名
优点:
1、隐藏了实现细节
2、提高了代码的重用性
调用:
select 函数名(参数列表) 【from 表】 ;
特点:
1、叫什么(函数名)
2、干什么(函数功能)
分类:
1、单行函数
如:concat、length、ifnull等
2、分组函数
功能:做统计使用,又称为统计函数、聚合函数、组函数
#一、字符函数
① length('参数')——返回参数的的字节长度
——select length('参数');
② concat(参数1, 参数2, 参数3)——把参数1、参数2、参数3 拼接起来并返回
——select concat(last_name, '_', first_name) 姓名 from employees;
③ upper('字符串')、lower('字符串')——把参数列表中的字符串全大写/小写
——select upper('adfdsfFSDF');
——select lower('AHJFKsadf');
#案例:将员工的姓变大写,员工的名表小写,然后在拼接起来
——select concat(upper(last_name), lower(first_name)) from employees;
④ substr/substring('元字符串', 起始位, 截取长度)
###注意:SQL 中的索引都是从1 开始的
——select substr('小酒窝真的是个混蛋', 8); ##混蛋
——select substr('小酒窝真的是个混蛋', 1, 3); ##小酒窝
#案例:姓名中首字符大写其余字符小写,然后用‘_’拼接显示
——select concat(upper(substr(last_name, 1, 1), '_', lower(substr(last_name, 2)), lower(first_name)) from employees;
⑤ instr('元字符串', '子字符串')——返回子串在元串中第一次出现的索引
——select instr('小酒窝真的是个混蛋', '混蛋') as out_put; ##8
⑥ trim('子串', '元串')——去除元串中两边的子串,不加子串参数就是默认‘空格’作为子串
——select trim(' 小酒窝 '); ##小酒窝
——select trim('a', 'aaaaaaaaa小aa酒aa窝aaaaaaaaaa'); ##小aa酒aa窝(只能去除两边)
⑦ lpad('元串', length, '子串'); ##若是元串的长度<length,就以子串往元串左边填充,若是元串的长度>length,就会截取元串的length 返回
——select lpad('小酒窝是个混蛋', 10, '*'); ##***小酒窝是个混蛋
——select lpad('小酒窝是个混蛋', 4, '*'); ##小酒窝是
⑧ rpad('元串', length, '子串'); ##若是元串的长度<length,就以子串往元串右边边填充,若是元串的长度>length,就会截取元串的length 返回
——select lpad('小酒窝是个混蛋', 10, '*'); ##小酒窝是个混蛋***
——select lpad('小酒窝是个混蛋', 4, '*'); ##小酒窝是
⑨ replace('元串', '目标串', '代替串') ##把元串中的目标串替换成代替串
——select replace('小酒窝是个混蛋', '混蛋', '美女'); ##小酒窝是个美女
—— select replace('小酒窝是个混蛋混蛋混蛋混蛋混蛋混蛋', '混蛋', '美女'); ##小酒窝是个美女美女美女美女美女美女
二、数学函数
① round(参数) ##四舍五入
——select round(1.30); ##1
——select round(1.79); ##2
——select round(-1.45); ##-1
——select round(-1.89); ##-2
② ceil(参数) ##向上取整,返回>=参数的最小整数
——select ceil(1.30); ##2
——select ceil(1.79); ##2
——select ceil(-1.45); ##-1
——select ceil(-1.89); ##-1
③ floor(参数) ##向下取整,返回<=参数的最小整数
——select floor(1.30); ##1
——select floor(1.79); ##1
——select floor(-1.45); ##-2
——select floor(-1.89); ##-2
④ truncate(元数字, 位数) ##截断元数字的有效数字的位数位
——select truncate(123.2344, 3); ##123.234
⑤ mod(元数字, 模) ##与元数字%模的结果一样【元数字 - 元数字 / 模 * 模】
——select mod(10, 3); ##1
——select 10%3; ##1
三、日期函数
① now() ##返回当前系统日期(yyyy-MM-dd hh:mm:ss)
——select now(); ##2019-06-22 13:50:19
#案例:获取指定的部分,年,月,日,小时,分钟,秒
——select year(now()) as 年; ##2019
——select year('2015-06-15') as 年; ##2015
——select year(hire_date) as 年 from employees;
——select month(now()) as 年; ##06
——select month('2015-06-15') as 年; ##06
② str_to_data(日期串, 日期格式) ##将字符日期转换成日期
——select str_to_data('1996-02-13', '%Y-%c-%d'); ##1996-02-13
#案例:查询入职日期为2018-03-23 的员工信息
——select * from employees where hire_date = str_to_date('2018-03-23', '%Y-%c-%d');
③ date_format(时间, '日期串格式') ##将日期时间转换成字符串格式日期
——select date_format('now(), %Y年 %m月 %d日') as 当前日期; ##2019年 06月 22日
#案例:查询有奖金的员工名和入职日期(xx月/xx日/xx年)
——select last_name, date_format(hire_date, '%m月%d日%Y年') as 入职日期
from employees
where commission_pet is not null;
四、其他函数
① select version()——查询当前MySQL服务的版本号
② select database()——查询当前数据库的名称
③ select user()——查询当前登录用户名
五、流程控制函数
① if(表达式, 结果1, 结果2) ##判断表达式是否成立,成立则返回结果1,不成立就返回结果2
——select if(10>5, '大于', '小于') as 结果;
② case 要判断的字段或表达式
when 常量1 then 要显示的值1 或语句1
when 常量2 then 要显示的值2 或语句2
..........
else 要显示的值n 或语句n
end
#案例:查询员工的工资,要求
位置编号=10,显示的工资为1.1倍
位置编号=20,显示的工资为1.2倍
位置编号=30,显示的工资为1.3倍
其他标号,显示的工资为1倍
end;
——select salary as 原始工资, location_id,
case location_id
when 10 then salary*1.1
when 20 then salary*1.2
when 30 then salary*1.3
else salary
end as 实发工资
from employees;
③ case
when 条件1 then 要显示的值1 或语句1
when 条件2 then 要显示的值2 或语句2
.。。。。。。
else 要显示的值n 或语句n
end
#案例:查询员工的工资情况
工资>20000,显示A级别
工资>15000,显示B级别
工资>10000,显示C级别
否则,显示级别
——select salary,
case
when salary>20000 then 'A'
when salary>15000 then 'B'
when salary>10000 then 'C'
else 'D'
end as '工资级别';
#案例1、显示系统时间(备注:日期+时间)
——select str_to_date(now(), '%Y-%m-%d hh:mm:ss');
#案例2、查询员工编号,姓名,工资,以及工资提高20%之后的结果(new salary)
——select employee_id, last_name, salary, salary*1.2 as 'new salary'
from employees
#案例3、将员工的姓名按首字母排序,并写出姓名的长度(length)
——select length(last_name) as 长度, substr(last_name, 1, 1) as 首字符
from employees
order by 首字符 asc;
#案例4、做一个查询,产生这样的结果:<last_name> earns <salary> monthly but wants <salary*3> dream salary King earns 24000 monthly but wants 72000
——select concat(last_name, ' earns ', salary, ' monthly but wants ', salary*3, ' as 'dream salary'
from employees
where salary=24000;
#案例5、使用case-when,按照下面的条件:
job grade
AD_PRES A
ST_MAN B
IT_PROG C
other D
-> last_name job grade
king AD_PRES ‘A’
——select last_name as last_name, job_id as job,
case job_id
when AD_PRES then 'A'
when ST_MAN then 'B'
when IT_PROG then 'C'
end as 工种级别
from employees
where job_id='AD_PRES';
二、分组函数
功能:作用于统计使用,又称为聚合函数或统计函数或数组
分类:sum 求和、avg 平均值、max 最大值、min 最小值、count 计算个数
特点:
① sum、avg一般用于处理数值类型
max、min、count 处理任何类型
② 以上函数都是忽略null
③ 可以搭配distinct 去重使用
④ count 函数的单独理解,一般使用count(*)用作统计行数
⑤ 和分组查询一同查询的字段要求是group by 后的字段
1、sum(字段名) ##查询表中该字段名下的所有数据总和
#案例:查询得到employees 表中salary 字段下的所有数据总和
——select sum(salary) from employees;
2、avg(字段名) ##查询表中该字段名下的所有数据平均值
#案例:查询得到employees 表中salary 字段下的所有数据的平均值
——select avg(salary) from employees;
3、max(字段名) ##查询表中该字段名下的所有数据中最大值
#案例:查询得到employees 表中salary 字段下的所有数据的最大值
——select max(salary) from employees;
4、min(字段名) ##查询表中该字段名下的所有数据中最小值
#案例:查询得到employees 表中salary 字段下的所有数据的最小值
——select min(salary) from employees;
5、count(字段名) ##查询表中该字段名下的所有数据的个数
#案例:查询得到employees 表中salary 字段下的所有数据的个数
——select count(salary) from employees;
——select count(*) from employees; ##获取当前表的行数
变多次为一次:
——select sum(salary) as 总和值, avg(salary) as 平均值, max(salary) as 最大值, min(salary) as 最小值, count(salary) as 个数
from employees;
练习题:
#案例1、查询公司员工工资的最大值,最小值,平均值,总和。
——select max(salary) as 最大值, min(salary) as 最小值, round(avg(salary), 2) as 平均值, sum(salary) as 总和 from employees;
#案例2、查询员工表中的最早入职时间和最晚入职时间之间的相差天数(DIFFRENCE)
——select datediff(max(hire_date), min(hire_date)) as DIFFRENCE from employees;
#案例3、查询部门编号为90的员工个数
——select count(*) as 个数
from employees
where department_id=90;
10. 分组查询
语法:
select 分组函数,列(要求出现在group by 的后面,【】标识可选项)
from 表
【where 筛选条件】
group by 分组的列表
【having 分组后的筛选】
【order by 子句】
【limit 起始行号, 截取宽度】
注意:
查询列表必须特殊,要求是分组函数和group by 后出现的字段
特点:
1)、分组查询分为两大类
数据源 | 位置 | 关键字 | |
---|---|---|---|
分组前筛选 | 原始表 | group by 子句的前面 | where |
分组后筛选 | 分组后的结果集 | group by 子句的后面 | having |
总结:
① 分组函数做条件肯定是放置having子句中
② 能用分组前筛选的,就优先考虑使用分组前筛选
2)、group by 子句支持单个字段分组,多个字段分组(多个字段之间使用逗号隔开没有顺序要求),表达式或函数分组
3)、也可以添加排序,放置group by 子句的后面
进阶5:分组查询
1、简单的分组查询
#案例1、查询每个部门的平均工资
——select avg(salary) as 平均工资, department_id as 部门编号
from employees
group by department_id;
#案例2、查询每个工种的最高工资
——select max(salary) as 最高工资, job_id as 工种编号
from employees
group by job_id;
#案例3、查询每个位置上的部门个数
——select count(*) as 部门个数, location_id as 位置编号
from departments
group by location_id;
2、在分组前添加筛选条件的查询
#案例1、查询邮箱中包含a 字符的,每个部门的平均工资
——select avg(salary) as 平均工资, department_id as 部门编号
from employees
where email like '%a%'
group by department_id;
#案例2、查询有奖金的每个领导手下员工的最高工资
——select max(salary) as 最高工资, employee_id as 员工编号, manager_id as 领导编号
from employees
where commission_pet is not null
group by manager_id;
3、在分组后添加筛选条件的查询
#案例1、查询哪个部门的员工个数>2(先查出每个部门的员工个数,在此基础上进行筛选条件的判断)
——select department_id as 部门编号, count(*) as 员工个数
from employees
group by department_id
having count(*)>2;
#案例2、查询每个工种有奖金的员工的最高工资>12000 的工种编号和最高工资
——select max(salary) as 最高工资, employee_id as 员工编号, job_id as 工种编号
from employees
where commission_pet is not null
group by job_id
having max(salary)>12000;
#案例3、查询领导编号>110 的每个领导手下的最低工资>5000 的领导编号,以及其员工的最低工资
——select min(salary) as 最低工资, employee_id as 员工编号, manager_id as 领导编号
from employees
where manager_id>110
group by manager_id
having min(salary)>5000;
3、按表达式或函数分组
#案例1、按员工姓名的长度分组,查询每一组的员工个数,筛选员工个数>5的有哪些
——select length(last_name) as 姓名长度, count(*) as 员工个数
from employees
group by 姓名长度
having 员工个数>5;
4、按多个字段分组
#案例、查询每个部门每个工种的员工的平均工资
——select avg(salary) as 平均工资, department_id as 部门编号, job_id as 工种编号
from employees
group by department_id, job_id;
5、在分组中添加排序
#案例:查询每个部门每个工种的员工的平均工资,并且按平均工资的高低显示出来
——select avg(salary) as 平均工资, department_id as 部门编号, job_id as 工种编号
from employees
where department_id is not null
group by department_id, job_id;
order by avg(salary) desc;
练习题:
#案例1、查询各个job_id 的员工工资的最大值,最小值,平均值,总和,并按job_id 升序
——select max(salary) as 最高工资, min(salary) as 最低工资, avg(salary) as 平均工资, sum(salary) as 工资总和
from employees
group by job_id
order by job_id asc;
#案例2、查询各个部门员工最高工资和最低工资之间的差值(DIFFERENCE)
——select max(salary)-min(salary) as different, department_id as 部门编号
from employees
group by department_id;
#案例3、查询各个领导手下员工的最低工资,其中最低工资不能低于6000,没有管理者的员工不计算在内
——select min(salary) as 最低工资, employee_id as 员工编号, manager_id as 领导编号
from employees
where manager_id is not null
group by manager_id
having min(salary)>6000;
#案例4、查询所有部门的编号,员工数量和工资平均值,并按平均工资降序
——select department_id as 部门编号, count(*) as 员工数量, avg(salary) as 平均工资
from employees
where department_id is not null
group by department_id
order by avg(salary) desc;
#案例5、选择具有各个job_id 的员工个数
——select count(*) as 员工个数, job_id as 工种编号
from employees
where job_id is not null
group by job_id
这篇就到这里结束了,请继续观看接下来章节《MySQL从基础到高级篇二》。