SQL第三章总结

版权声明:未经本人允许 不得转载 https://blog.csdn.net/zwz1998/article/details/82586194

本章目标

嵌套子查询 聚合技术 排序函数

例题:查询学号在王五前面的同学

select * from stuinfo where stuid<(select stuid from stuinfo where stuname='王五')

注意

在查询语句中使用 <,>,=符号后的子查询的结果只能有一个值

例题:查找JAVA分数大于80分的学员姓名

select stuname from stuinfo where stuid in (select stuid from stumarks where sybject = 'java')

NOT IN的用法和IN一样,唯一的区别就是意义相反

EXISTS 和 NOT EXISTS 表示存在和不存在的意思

例题:查询存在分数的学员的信息

select * from stuinfo where exists(select * from stumarks where stumarks.stuid = stuinfo. stuid)

在SQL查询中,SOME,ANY,ALL后必须跟子查询 并且SOME和ANY的作用是一样的表示其中的任何一项

ALL 表示所有的项

COMPUTE 和COMPUTE BY进行汇总查询

compute子句需要下列信息:

1.可选by关键字 2. 行聚合函数名称 3. 要对其执行行聚合函数的列

例题:对信息进行分组查询并统计

select * from stumarks order by stuid desc compute avg(score),sum(score) by stuid

排序函数 语法:排序函数 over([分组子句]排序子句[desc|asc])

ROW_NUMBER函数生成的排序根据排序子句给出递增连续的序号

RANK函数生成的排序根据子句给出递增的序号,但是存在并列并且跳空

DENSE_RANK函数生成的排序根据排序子句给出递增的序号但是存在并列不跳空

分组子句:PARTITION BY分组列

排序子句:ORDER BY排序列

例题:分科目进行不跳空排名

select dense_rank()over(partition by subject order by score desc)as'排名',

stuinfo.stuid,stuname,subject from stuinfo,stumarks

where stuinfo.stuid=stumarks.stuid

猜你喜欢

转载自blog.csdn.net/zwz1998/article/details/82586194