select * from Student where ClassId=(select ClassId from grade where classname='八期班')
--子查询返回的值不止一个。当子查询跟随在 =、!=、<、<=、>、>= 之后,或子查询用作表达式时,这种情况是不允许的。
select * from Student where ClassId=(select ClassId from grade)
--查询八期班以外的学员信息
--当子查询返回多个值(多行一列),可以使用in来指定这个范围
select * from Student where ClassId in(select ClassId from grade where classname<>'八期班')
--当没有用 EXISTS 引入子查询时,在选择列表中只能指定一个表达式。如果是多行多列或者一行多列就需要使用exists
--使用 EXISTS 关键字引入子查询后,子查询的作用就相当于进行存在测试。外部查询的 WHERE 子句测试子查询返回的行是否存在
select * from Student where EXISTS(select * from grade)
select * from Student where ClassId in(select * from grade)
--2.子查询做为结果集--
select top 5 * from Student --前五条
--使用top分页
select top 5 * from Student where StudentNo not in(select top 5 studentno from Student)
--使用函数分页 ROW_NUMBER() over(order by studentno),可以生成行号,排序的原因是因为不同的排序方式获取的记录顺序不一样
select ROW_NUMBER() over(order by studentno),* from Student
--查询拥有新生成行号的结果集 注意:1.子查询必须的别名 2.必须为子查询中所有字段命名,也就意味着需要为新生成的行号列命名
select * from (select ROW_NUMBER() over(order by studentno) id,* from Student) temp where temp.id>0 and temp.id<=5
select * from (select ROW_NUMBER() over(order by studentno) id,* from Student) temp where temp.id>5 and temp.id<=10
select * from (select ROW_NUMBER() over(order by studentno) id,* from Student) temp where temp.id>10 and temp.id<=15
--3.子查询还可以做为列的值
select (select studentname from student where studentno=result.studentno),(select subjectname from subject where subjectid=result.SubjectId), StudentResult from Result
--使用Row_number over()实现分页
--1.先写出有行号的结果集
select ROW_NUMBER() over(order by studentno),* from Student
--2.查询有行号的结果集 子查询做为结果集必须添加别名,子查询的列必须都有名称
select * from (select ROW_NUMBER() over(order by studentno) id,* from Student) temp where id>0 and id<=5
--查询年龄比“廖杨”大的学员,显示这些学员的信息
select * from Student where BornDate<(select BornDate from Student where StudentName='廖杨')
--查询二期班开设的课程
select * from Subject where ClassId=(select ClassId from grade where classname='二期班')
--查询参加最近一次“office”考试成绩最高分和最低分
--1查询出科目 ID










