数据库相关子查询

来源:百度知道 编辑:UC知道 时间:2024/06/21 14:49:17
select sno,cno
from sc x
where grade>=(select avg(grade) from sc y where y.sno=x.sno)
这是一个嵌套的子查询,目的是:找出每个学生超过他选修课程平均成绩的课程号
他的执行的步骤是什么?
sno cno grade
200215121 1 92
200215121 2 85
200215121 3 88
200215122 2 90
200215122 3 80

先执行(select avg(grade) from sc y where y.sno=x.sno) ,获得平均成绩。

在执行:
select sno,cno
from sc x
where grade>='平均成绩'

顺序:
1. from sc x
2. where
3. from sc y
4 where y.sno=x.sno
5. select avg(grade)
6. select sno.cno

先看子查询,找出学生选修的每科平均成绩,主查询是拿每个学生的成绩和这个平均成绩比,大的话就输出。