SQL语句如何查询成绩第二高的学生?

2025-02-07 11:58:17
推荐回答(2个)
回答1:

假设学生成绩表为xscj,里面有若干个字段,其中包括具体成绩得分字段df,那么,查询所有成绩第二高学生的SQL语句如下:
select * from xscj where df in (
select max(df) from xscj where df not in (
select max(df) from xscj))

该语句嵌套基层,最内层的语句查询最高分,第二层的语句查询除了最高分以外后剩下的最高分(即第二高分),最外层即是查询第二高分有哪些人(可能存在多人的情况)。

回答2:

select top 1 * from XXX where XXX.ID not in (select top 1 ID from XXX order by XXX.score desc) order by XXX.score desc