SQL高级查询:嵌套和分页

1.嵌套子查询

--查询最近一次oop考试没有参加考试的学生
select
StudentName from Student where StudentNo not in( select StudentNo from Result where SubjectId=( select SubjectId from Subject where SubjectName='oop' ) and ExamDate=( select MAX(ExamDate) from Result where SubjectId= (select SubjectId from Subject where SubjectName='oop' ) ) )

2.相关子查询

SElECT name,id,price
  FROM commodity As c
  WHERE  price>
  (
    SELECT AVG(price)
    FROM commodity AS a
    WHERE c.类编号=a.类编号
  )

3.分页查询

第一种:

--跳过五行取两行
select top 2 * from Student
where StudentNo not in
(
  select top 5 StudentNo from Student
  order by StudentNo
)
order by StudentNo

第二种:

--取第四行到第六行
select * from
(select *,ROW_NUMBER() over(order by StudentNo) as myid 
from Student) as temp
where myid between 4 and 6
原文地址:https://www.cnblogs.com/hr1997/p/5262961.html