一嵌套子查询
- 特点:使用灵活,可以成为sql语句的多个部分
- 使用范围:①子查询作为查询条件使用②子查询作为临时表使用③子查询作为列使用④降低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 表名