第 3 章 SQL 高级查询

一嵌套子查询

  1. 特点:使用灵活,可以成为sql语句的多个部分
  2. 使用范围:①子查询作为查询条件使用②子查询作为临时表使用③子查询作为列使用④降低sql的语句的复杂度,提高sql语句的可读性

例如:

一.子查询作为临时表使用

  当stuinfo作为子查询

           Select  stuname,subject,score from StuMarks s1,(select *from stuifo where stuname=’李四’) s2,where s1.stuid=s2.stuid and   s1.score>80

    当StuMarks 作为子查询

        select  stuname,subject,score from stuifo s1,(select *from StuMarks  where score>80) s2,where s1.stuid=s2.stuid and s1.stuname='李四'

二.子查询作为查询条件使用

   例如:查询学号在王五同学前面的学员信息

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

三.子查询作为列使用

 问题:查询所有学员的“HTML”成绩,如果没有成绩显示为null

              select *s,

                 (select score from StuMarks  where subject='HTML'  and s.stuid=StuMarks.stuid ) as '成绩'   from stuinfo s

四.使用in和not in 完成子查询

 例如:查询Java成绩大于80分学员的信息

       select *from StuInfo where stuid in (select stuid  from StuMarks where score>80 and subject='Java')

排序函数

1.row_number函数生成的排序根据排序子句给出递增连续的序号(不看值,显示行号)

2.rank函数生成的排序根据排序字句给出递增的序号,但是存在并列并且跳号(存在并列时跳空)

3.dense_rank函数生成的排序根据排序字句给出递增的序号但是存在并列不跳空(并列不跳空)

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

公式表达式

whith 别名

as(查询语句)

select  字段名 from 表名

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值