Cno:课程编号; Sno:学号
Sname:姓名; Sno:学号
Cno:课程编号
SELECT Sname
FROM student
WHERE Sno IN (SELECT Sno FROM SC WHERE Cno = 'C1');
SELECT Sname
FROM student
WHERE EXISTS
(
SELECT *
FROM SC
WHERE student.Sno=SC.Sno AND Cno='C1'
);
相关子查询执行过程:
先在外层查询中取student表的第一个元组(记录),用该记录的相关的属性值(在内层WHERE子句中给定的)处理内层查询,若外层的WHERE子句返回‘TRUE’值,则此元组送入结果的表中。然后再取下一个元组;重复上述过程直到外层表的记录全部遍历一次为止。
不关心子查询的具体内容,因此用 SELECT *, Exists + 子查询用来判断该子查询是否返回元组。 Exists:当子查询的结果集非空时,返回为‘True’;当子查询的结果集为空时,Exists为‘False’ 。 NOT EXISTS :若子查询结果为空,返回‘TRUE’值, 否则返回 ‘FALSE。
SELECT Sname
FROM student
WHERE NOT EXISTS
(
SELECT *
FROM SC
WHERE student.Sno = SC.Sno AND Cno = 'C1'
);
SELECT Sname
FROM student
WHERE NOT EXISTS
(
SELECT *
FROM Course
WHERE NOT EXISTS
(
SELECT *
FROM SC
WHERE student.sno=SC.sno AND Course.Cno=SC.Cno
)
);
SELECT Sname
FROM student
WHERE NOT EXISTS
(
SELECT *
FROM SC AS SCX
WHERE SCX.Sno='s1' AND NOT EXISTS
(
SELECT *
FROM SC AS SCY
WHERE student.Sno=SCY.Sno AND SCX.Cno=SCY.Cno
)
);
SELECT Sno, AVG_G
FROM
(
SELECT Sno,AVG(Grade)
FROM SC
GROUP BY Sno
) AS RA(Sno, AVG_G)
WHERE AVG_G > 80