简单查询:
![c3ef50df33cd0797cf7f924d6169a9fb.png](https://img-blog.csdnimg.cn/img_convert/c3ef50df33cd0797cf7f924d6169a9fb.png)
![f9f2fd04694078eb6181afd8dc54cdfb.png](https://img-blog.csdnimg.cn/img_convert/f9f2fd04694078eb6181afd8dc54cdfb.png)
![d3da67c2ee305307c6a1faa012ed845d.png](https://img-blog.csdnimg.cn/img_convert/d3da67c2ee305307c6a1faa012ed845d.png)
汇总查询:
![7877b4f1db0c69799c683cfdc9695c02.png](https://img-blog.csdnimg.cn/img_convert/7877b4f1db0c69799c683cfdc9695c02.png)
![54d25a55a6f6eea44f37a8a96a805e0b.png](https://img-blog.csdnimg.cn/img_convert/54d25a55a6f6eea44f37a8a96a805e0b.png)
分组:
![0959ec4fe4b1a38acbb87715257acb98.png](https://img-blog.csdnimg.cn/img_convert/0959ec4fe4b1a38acbb87715257acb98.png)
![b8be0bbc28d2e9875518a84a21e28e30.png](https://img-blog.csdnimg.cn/img_convert/b8be0bbc28d2e9875518a84a21e28e30.png)
![5bc6f38f44370918dffbc08e7c85cf38.png](https://img-blog.csdnimg.cn/img_convert/5bc6f38f44370918dffbc08e7c85cf38.png)
分组结果的条件:
![cf5d172c767d1589106b0d599a78b0a8.png](https://img-blog.csdnimg.cn/img_convert/cf5d172c767d1589106b0d599a78b0a8.png)
![c87a0fd0655931f75f005a7fee3e0510.png](https://img-blog.csdnimg.cn/img_convert/c87a0fd0655931f75f005a7fee3e0510.png)
![a20cbc6826fc89355383d18d920a16da.png](https://img-blog.csdnimg.cn/img_convert/a20cbc6826fc89355383d18d920a16da.png)
![3b39b197b62c333a66268c4d22d41385.png](https://img-blog.csdnimg.cn/img_convert/3b39b197b62c333a66268c4d22d41385.png)
![439fce447d627ce2d0e1c64e12790ea4.png](https://img-blog.csdnimg.cn/img_convert/439fce447d627ce2d0e1c64e12790ea4.png)
![f62dccf49425288121d197914aaf83c4.png](https://img-blog.csdnimg.cn/img_convert/f62dccf49425288121d197914aaf83c4.png)
![4fa10b2004a3f222b97306cba67ed3a3.png](https://img-blog.csdnimg.cn/img_convert/4fa10b2004a3f222b97306cba67ed3a3.png)
![4f5af7393212eff9d039df2881a5a62e.png](https://img-blog.csdnimg.cn/img_convert/4f5af7393212eff9d039df2881a5a62e.png)
![e160cb1ed21b37b29dde211510120287.png](https://img-blog.csdnimg.cn/img_convert/e160cb1ed21b37b29dde211510120287.png)
日期函数查询:
![2db4d6720f506cc8a34991f8c5420ec3.png](https://img-blog.csdnimg.cn/img_convert/2db4d6720f506cc8a34991f8c5420ec3.png)
![e5686b77c33c088f816425f4b604c350.png](https://img-blog.csdnimg.cn/img_convert/e5686b77c33c088f816425f4b604c350.png)
面试题类型:
- topN类型
工作中会经常遇到这样的业务问题:
如何找到每个类别下用户最喜欢的产品是哪个?
如果找到每个类别下用户点击最多的5个商品是什么?
这类问题其实就是常见的:分组取每组最大值、最小值,每组最大的N条(top N)记录。
分组求每组最值:
案例:按课程号分组取成绩最大值所在行的数据
![12942bde7e20d18e05ca352522d1598e.png](https://img-blog.csdnimg.cn/img_convert/12942bde7e20d18e05ca352522d1598e.png)
![74d4aba56f2976e1cb70daba6793478a.png](https://img-blog.csdnimg.cn/img_convert/74d4aba56f2976e1cb70daba6793478a.png)
每组最大的N条记录:
案例:查询各科成绩前两名的记录
第1步,查出有哪些组
我们可以按课程号分组,查询出有哪些组,对应这个问题里就是有哪些课程号
![98a6ce0eec7a47855d224cd6dc579934.png](https://img-blog.csdnimg.cn/img_convert/98a6ce0eec7a47855d224cd6dc579934.png)
- 多表联结查询
![e3115b9f4f69237061207abe21a584cd.png](https://img-blog.csdnimg.cn/img_convert/e3115b9f4f69237061207abe21a584cd.png)
![01035f3d96ffed8e0f2949f939927728.png](https://img-blog.csdnimg.cn/img_convert/01035f3d96ffed8e0f2949f939927728.png)
![34ad3e24dc984c9b0fcf2d8e0a4322c6.png](https://img-blog.csdnimg.cn/img_convert/34ad3e24dc984c9b0fcf2d8e0a4322c6.png)
![9305a01e51cb26577acd1505836e17bf.png](https://img-blog.csdnimg.cn/img_convert/9305a01e51cb26577acd1505836e17bf.png)
![080d8f9ed646010ac0686e738c8b7f0b.png](https://img-blog.csdnimg.cn/img_convert/080d8f9ed646010ac0686e738c8b7f0b.png)
![a347d412fcffa17ce34d3cb9c6af1432.png](https://img-blog.csdnimg.cn/img_convert/a347d412fcffa17ce34d3cb9c6af1432.png)
![16f2b71ed99c5b23abc1645a94acdc89.png](https://img-blog.csdnimg.cn/img_convert/16f2b71ed99c5b23abc1645a94acdc89.png)
![29f54c5f8937334cc2e7419f4c72aff7.png](https://img-blog.csdnimg.cn/img_convert/29f54c5f8937334cc2e7419f4c72aff7.png)
-- 提高SQL的执行效率-尽量避免全表搜索
1. select子句中尽量避免使用*
2. where子句比较符号左侧避免函数:尽量避免在where条件子句中,比较符号的左侧出现表达式、函数等操作。因为这会导致数据库引擎进行全表扫描,从而增加运行时间。
3. 尽量避免使用in和not in:in和not in也会导致数据库进行全表搜索,增加运行时间。
4. 尽量避免使用or:or同样会导致数据库进项全表搜索。
5.使用limit子句限制返回的数据行数