![b279dfbf71f06a75fee6b3a9cc592598.png](https://img-blog.csdnimg.cn/img_convert/b279dfbf71f06a75fee6b3a9cc592598.png)
1、汇总查询
汇总是数据分析中常用的基础操作,所谓汇总就是 求和、求均值、求中无数、计数、最大值、最小值等等,这些操作在excel中都有对应的函数去实现,那在SQL中如何实现呢?
也是通过函数来实现的;
SQL常用的聚合函数:
求和 sum
计数 count
求均值 avg
最大值 max
最小值 min
- 案例:
![386cd3489e640391a9a6fcb176720045.png](https://img-blog.csdnimg.cn/img_convert/386cd3489e640391a9a6fcb176720045.png)
![eebb6bdb0715327de3d007ad411de30f.png](https://img-blog.csdnimg.cn/img_convert/eebb6bdb0715327de3d007ad411de30f.png)
2、分组 group by
直接上练习:
![623f39f0f6ea1472920c34a15f2e042f.png](https://img-blog.csdnimg.cn/img_convert/623f39f0f6ea1472920c34a15f2e042f.png)
![18ce2fddfe399bad42e5cfd2775cf84c.png](https://img-blog.csdnimg.cn/img_convert/18ce2fddfe399bad42e5cfd2775cf84c.png)
![1d09fe0fcba2e68c6f449c46d8777ff9.png](https://img-blog.csdnimg.cn/img_convert/1d09fe0fcba2e68c6f449c46d8777ff9.png)
3、分组 + 条件筛选
这里涉及到2个重要的知识点:
第一,这里的条件筛选为什么不用where;
首先,where 子句不能与聚合函数一起使用,因为where 后面只能跟数据表中的原有的字段,通过聚合函数计算过的字段,不是数据表中原有的字段;
其次,where 的执行顺序是紧跟在from之后的,也就是说where是在数据从磁盘读取到内存中时,对原始表数据进行一条条的过滤;
那为什么用having呢?
having是对 group by 已经分组的数据进行条件过滤;
与 where 相反,having可以搭配聚合函数一起使用;
第二,为什么 having子句 后面不能直接引用 select 中的别名;
这里是SQL语句执行顺序的问题,having的执行优先级在 select 之前,所以having完全不知道select所引用的别名是什么;
group by ... having子句
![92819117cd2085a83e4b4ad53fb3adf5.png](https://img-blog.csdnimg.cn/img_convert/92819117cd2085a83e4b4ad53fb3adf5.png)
![fb6475ba7700bfaa13bf3c9838fce10e.png](https://img-blog.csdnimg.cn/img_convert/fb6475ba7700bfaa13bf3c9838fce10e.png)
![b54577764a418c5d8fd07a7c4d3abd89.png](https://img-blog.csdnimg.cn/img_convert/b54577764a418c5d8fd07a7c4d3abd89.png)
4、SQL语句的执行顺序
from - where - group by - having - select - order by - limit
5、排序 order by 子句
order by <列名> desc
升序 asc
降序 desc
6、limit 分页查询
limit m,n 从第 m+1 条记录开始, 返回 n 条记录
select * from student limit 10 显示前10行记录(默认m = 0)
select * form student limit 9,10 显示第10~20行记录
7、SQL处理业务问题,遇到难点时:
把问题翻译成大白话
写出分析思路
根据思路写出对应的SQL语句
8、sqlzoo练习
![73b904a3f64e811d48911e1ffcf44c61.png](https://img-blog.csdnimg.cn/img_convert/73b904a3f64e811d48911e1ffcf44c61.png)
![5fcaf96f36fa626a0f74c46699e4714e.png](https://img-blog.csdnimg.cn/img_convert/5fcaf96f36fa626a0f74c46699e4714e.png)
![59fecd9f8197022afa3e9364334361ee.png](https://img-blog.csdnimg.cn/img_convert/59fecd9f8197022afa3e9364334361ee.png)
![b44a7d664a0b0d649f0abfc6b499eb33.png](https://img-blog.csdnimg.cn/img_convert/b44a7d664a0b0d649f0abfc6b499eb33.png)
![1ef38ee83429ed3a149fc8d43f286191.png](https://img-blog.csdnimg.cn/img_convert/1ef38ee83429ed3a149fc8d43f286191.png)
![773e9e19a6ba2437dad1830e57a31b77.png](https://img-blog.csdnimg.cn/img_convert/773e9e19a6ba2437dad1830e57a31b77.png)
![a25f7628ccdabd399db1fd7bfaf149de.png](https://img-blog.csdnimg.cn/img_convert/a25f7628ccdabd399db1fd7bfaf149de.png)
count和sum练习
![683409af702654f42a2ed9ae078986ca.png](https://img-blog.csdnimg.cn/img_convert/683409af702654f42a2ed9ae078986ca.png)
![8ed9bc3a1510e0760b4108d0eb98b22f.png](https://img-blog.csdnimg.cn/img_convert/8ed9bc3a1510e0760b4108d0eb98b22f.png)
![9579119a2c96ce1ae64ca23895ef61b4.png](https://img-blog.csdnimg.cn/img_convert/9579119a2c96ce1ae64ca23895ef61b4.png)
![d963d61fcfe9bd1408a518a672cf5466.png](https://img-blog.csdnimg.cn/img_convert/d963d61fcfe9bd1408a518a672cf5466.png)
![34263051412911c853f7b4280b28d384.png](https://img-blog.csdnimg.cn/img_convert/34263051412911c853f7b4280b28d384.png)
![8ad50e6fde15d56fa71a8cd42224efc9.png](https://img-blog.csdnimg.cn/img_convert/8ad50e6fde15d56fa71a8cd42224efc9.png)
![900a4ea8369d213f936bf4486da94587.png](https://img-blog.csdnimg.cn/img_convert/900a4ea8369d213f936bf4486da94587.png)
![db88e92575f24c00355c6d1eba1cb6c7.png](https://img-blog.csdnimg.cn/img_convert/db88e92575f24c00355c6d1eba1cb6c7.png)
还是比较简单滴~