[求助]新人求助查询语句
表结构如下:
Students(SNO,SNAME,SEX,BDATE,HEIGHT,DEPARTMENT)
Courses(CNO,CNAME,LHOUR,CREDIT,SEMESTER)
SC(SNO,CNO,GRADE)
列出平均成绩最高的学生名字和成绩。(SELECT句中不得使用TOP n子句)
请大家帮忙哦~~~
表结构如下:
Students(SNO,SNAME,SEX,BDATE,HEIGHT,DEPARTMENT)
Courses(CNO,CNAME,LHOUR,CREDIT,SEMESTER)
SC(SNO,CNO,GRADE)
SELECT STUDENTS.*,AvgG
FROM STUDENTS
INNER JOIN
/*以下部分为一级子查询*/
(
SELECT SNO,AvgG
FROM
(
SELECT SNO,avg(GRADE) as AvgG
FROM
SC
Group by SNO
) Avg1 --该部分与下面Avg2均为子查询,同表比较
WHERE 1> --就是下面的二级子查询的结果为0,也就是最高平均分的情况,这个算是Top语句的替换(1可以随便改,改成几就算是Top几,),不过比Top好的地方是,并列的时候这样写可以全部选出
(
SELECT COUNT(*)
FROM
(
SELECT SNO,avg(GRADE) as AvgG
FROM
SC
Group by SNO
) Avg2
WHERE Avg1.AvgG<Avg2.AvgG
)
) MAVG
/*一级子查询尾*/
ON
STUDENTS.SNO=MAVG.SNO
[此贴子已经被作者于2006-3-26 11:54:01编辑过]