- 查询没有选修1号课程的学生姓名。
SELECT sname
FROM student
WHERE NOT EXISTS(
SELECT *
FROM sc
WHERE sno = student.sno
AND cno ='1');
- 查询所有选修了1号课程的学生姓名。
SELECT sname
FROM student
WHERE EXISTS(
SELECT *
FROM sc
WHERE sc.sno =student.sno
AND sc.cno = '1');
- 查询非计算机科学系中比计算机科学系所有学生年龄都小的学生姓名及年龄。
SELECT sname,sage
FROM student
WHERE sage<ALL(
SELECT sage
FROM student
WHERE sdept='CS')
AND sdept<>'CS';
- 查询非计算机科学系(CS)中比计算机科学系任意一个学生年龄小的学生姓名和年龄。
SELECT sname,sage
FROM student
WHERE sage<ANY(
SELECT sage
FROM student
WHERE sdept='CS'
)
AND sdept<>'CS';
-
找出每个学生超过他选修课程平均成绩的课程号。 注意:不包括相等 select cno
FROM sc x
WHERE grade<(
SELECT AVG(grade)
FROM sc y
WHERE y.sno=x.sno
);
-
查询选修了课程名为“信息系统”的学生学号和姓名。
SELECT sno,sname
FROM student
WHERE sno IN(
SELECT sno
FROM sc
WHERE cno IN(
SELECT cno
FROM course
WHERE cname='信息系统'
));
- 查询与“刘晨”在同一个系学习的学生(学号,姓名,系)。
SELECT sno,sname,sdept
FROM student
WHERE sdept IN(
SELECT sdept
FROM student
WHERE sname='刘晨');
- 查询选修了全部课程的学生姓名。
SELECT sname
FROM student
WHERE NOT EXISTS(
SELECT *
FROM course
WHERE NOT EXISTS(
SELECT *
FROM sc
WHERE sno = student.sno
AND cno = course.cno
));
- 查询每个学生及其选修课程的情况,保留学生学号,姓名,课程号,成绩。
SELECT student.sno,
sname,course.cno,grade
FROM student,course,sc
WHERE student.sno = sc.sno
AND sc.cno = course.cno;
- 查询选修了1号课程的学生及其选修课程的情况,保留学生学号,姓名,课程名,成绩。
SELECT student.sno,sname,
cname,
grade
FROM student,course,sc
WHERE sc.sno = course.cno
AND sc.cno = course.cno
AND course.cno='1';
- 查询每个学生及其选修课程的情况,保留学生学号,姓名,课程号,课程名,成绩。
SELECT student.sno,sname,
course.cno,cname,grade
FROM student,course,sc
WHERE student.sno = sc.sno
AND sc.cno = course.cno;
-
查询每一门课的间接先修课(即先修课的先修课),保留课程号,先修课的课程号。 1. SELECT x.cno,y.cpno
FROM course x,course y
WHERE x.cpno = y.cno;
|