第三章 SQL高级查询总结

第三章 SQL高级查询总结

嵌套子查询

使用in 和 not in 完成子查询
in 存在   not in 不存在
在in 和 not in 后接的子查询中,可以有多个值出现,但必须只能有1列
例1:查询学号为1和3的学员信息
select * from stuinfo where stuid in (1,3)
例2: 查询Java分数大于80分的学员姓名
select stuname from stuinfo where stuid in
(select stuid from stumarks where subject='java' and score>80)


使用exists 和 not exists 完成子查询

exists 和not exists 表示存在和不存在的意思.
例:查询存在分数的学员的信息
select * from stuinfo where exists
(select * from stumarks where stumarks.stuid=stuinfo.stuid)

 

使用 some any all 进行子查询
some和any表示其中任何一项,all则表示其中的所有的项

 


聚合技术

使用compute 和 compute by 进行汇总查询
例: 对信息进行分组查询并统计
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

 

公式表表达式
例: 在一个批中建立一个临时表,保存所有学生的sql成绩
可以用别名,但要与查询的结果列一一对应(学号,姓名,成绩)

with stuinfo stumarks (stuid,stuname,score)
as
(
    select s1.stuid,s1.stuname,s2.score
    from stuinfo s1,stumarks s2
    where s1.stuid=s2.stuid and subject='SQL'
)
select * from stuinfo stumarks
go

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值