排序问题 asc 升序 , desc 降序
select * from emp order by deptno desc , sal asc;

select * from emp where deptno >= 20 order by deptno desc , sal asc ;

— 查询所有数据,在年龄降序排序的基础上,如果年龄相同再以数学成绩升序排序
select * from student3 order by age desc, math asc;

五个聚合函数 count, avg, sum, max, min,
select count(empno) from emp;

select count(ifnull(comm, 0)) from emp ;

select count(*) from emp;

— 查询年龄大于 20 的总数
select count(*) from student3 where age>20;

— 查询数学成绩总分
select sum(math) as 数学总分 from student3;

— 查询数学成绩平均分
select ROUND(avg(math), 1) 平均分 from student3;

— 查询数学成绩最高分
select name,math from student3;

— 查询数学成绩最低分
select min(math) 最低分 from student3;

select查询中如果使用了聚合函数就不能写其它的字段,除非该字段是group by子句

使用group by 分组查询时.只能使用聚合函数与group by子句
select sex , count(*) , avg(math) from student3 GROUP BY sex;

select deptno , avg(sal) from emp GROUP BY deptno

select job , count(*) from emp GROUP BY job ;

select sex , count(*) from student3 where age > 25 group by sex

select sex , count(*) as 人数 from student3 where age > 25 group by sex order by 人数 desc

查询年龄大于 25 岁的人,按性别分组,统计每组的人数,并只显示性别人数大于 2 的数据
# having 是使用在group by之后的. 用于用组成功后继续进行条件筛选
select sex , count() from student3 where age > 25 GROUP BY sex having count() > 2

查询emp表,工资大于1000的员工,并且对部分编号进行分组.求出来每组员工工资总和..并且降序显示
select deptno , sum(sal) from emp where sal > 1000 GROUP BY deptno order by sum(sal) desc ;

查询 sum 大于 8600
select deptno , sum(sal) from emp where sal > 1000 GROUP BY deptno HAVING sum(sal) > 8600 order by sum(sal) desc ;

————————————————————————————————————————————————————————

INSERT INTO student3(id,NAME,age,sex,address,math,english) VALUES
(9,’唐僧’,25,’男’,’长安’,87,78),
(10,’孙悟空’,18,’男’,’花果ft’,100,66),
(11,’猪八戒’,22,’男’,’高老庄’,58,78),
(12,’沙僧’,50,’男’,’流沙河’,77,88),
(13,’白骨精’,22,’女’,’白虎岭’,66,66),
(14,’蜘蛛精’,23,’女’,’盘丝洞’,88,88);

offset:起始行数,从 0 开始计数,如果省略,默认就是 0
# length: 返回的行数
# 用户会提交参数 pageNumber = 5
# int pageSize = 5 ;每页显示的记录数

1页 : 0 , 5
# 2页 : 5 , 5
# 3页 : 10, 5
# 4页 : 15, 5
# 5页 : 20, 5

select from student3 limit (pageNumber - 1) pageSize, pageSize ;

select * from student3 limit 0 , 3 ;

作业答案
# 作业
#1.列出员工表中每个部门的员工数,和部门 no
select deptno , count(*) from emp GROUP BY deptno

2.列出员工表中每个部门的员工数(员工数必须大于 3),和部门名称 [超纲不做]

3.找出工资比 jones 多的员工 [超纲不做]

4.列出所有员工的姓名和其上级的姓名 [超纲不做]

5.以职位分组,找出平均工资最高的两种职位
select job ,avg(sal) from emp GROUP BY job order by avg(sal) desc limit 0,2;

6.查找出不在部门 20,且比部门 20 中任何一个人工资都高的员工姓名、部门名称 [超纲不做]

7.得到平均工资大于 2000 的工作职种
select job ,avg(sal) from emp GROUP BY job HAVING avg(sal) > 2000

8.分部门得到工资大于 2000 的所有员工的平均工资,并且平均工资还要大于 2500
select deptno,avg(sal) from emp where sal > 2000 GROUP BY deptno having avg(sal) > 2500

9.分组统计各部门下工资>500 的员工的平均工资
select deptno,avg(sal) from emp where sal > 500 GROUP BY deptno

10.统计各部门下平均工资大于 500 的部门
select deptno,avg(sal) from emp GROUP BY deptno having avg(sal) > 500

11.算出部门 30 中得到最多奖金的员工奖金
select max(comm) from emp where deptno = 30

select * from emp where deptno = 30 order by comm desc limit 0,1

12.算出部门 30 中得到最多奖金的员工姓名 (超纲不做)

13.算出每个职位的员工数和最低工资
select job,count(*) , min(sal) from emp GROUP BY job;

14.列出员工表中每个部门的员工数,和部门 no
select deptno , count(*) from emp GROUP BY deptno

15.得到工资大于自己部门平均工资的员工信息(超纲不做)

— avg(ifnull(comm,0)), sum(sal + ifnull(comm,0))
# 16.分组统计每个部门下,每种职位的平均奖金(也要算没奖金的人)和总工资(包括奖金) —ifnull()
select deptno,job , avg(ifnull(comm,0)), sum(sal + ifnull(comm,0)) from
emp GROUP BY deptno,job order by deptno asc;