SELECT sno FROM student WHERE sno IN ( SELECT "first".sno FROM sc AS "first" INNERJOIN sc AS "second" ON "first".sno = "second".sno WHERE "first".cno ='CS3121014' OR "second".cno ='CS3221018' ); -- 更简洁的写法: SELECT sno FROM sc WHERE cno ='CS3121014'UNION SELECT sno FROM sc WHERE cno ='CS3221018';
SELECT sno FROM student WHERE sno IN ( SELECT "first".sno FROM sc AS "first" INNERJOIN sc AS "second" ON "first".sno = "second".sno WHERE "first".cno ='CS3121014' AND "second".cno ='CS3221018' ); -- 更简洁的写法: SELECT sno FROM sc WHERE cno ='CS3121014'INTERSECT SELECT sno FROM sc WHERE cno ='CS3221018';
6.查询不学 CS3121014 课的学生学号
1 2 3 4 5 6 7
SELECT sno FROM student WHERE sno !=ANY ( SELECT sno FROM sc WHERE cno ='CS3121014'AND sno = student.sno ); -- 一定要连接表 sno=student.sno
7.查询 CS3121014 的先修课的课程号
1 2 3 4 5 6 7
SELECT pcourse.cpno FROM pcourse WHERE pcourse.cno ='CS3121014';
8.查询选修了全部课程的学生学号
1 2 3 4 5 6 7 8 9 10 11 12 13
SELECT sno FROM student WHERE sno IN ( SELECT sno FROM sc GROUPBY sno HAVINGCOUNT ( * ) = ( SELECTCOUNT ( * ) FROM course ) );
9.查询选修了学号为“03051066”的学生所选全部课程的学生学号和姓名
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
SELECT sno,sname FROM student WHERE NOTEXISTS ( SELECT * FROM sc AS sc_1 WHERE sno ='03051066' ANDNOTEXISTS ( SELECT*FROM sc AS sc_2 WHERE sc_2.sno = student.sno AND sc_2.cno = sc_1.cno ) );
10.查询所有姓刘学生的姓名、学号和性别。
1 2 3 4 5 6
SELECT sname,sno,ssex -- 注意顺序,错了没有分(比如写 * )555 FROM student WHERE student.sname LIKE'刘%';
11.查询姓名中第二个字为“阳”字的学生姓名和学号。
1 2 3 4 5 6 7
SELECT student.sno, student.sname FROM student WHERE student.sname LIKE'_阳%';
12.查询选修了课程的学生人数
1 2 3 4
SELECT COUNT(DISTINCT sno) FROM sc;
13.查询选修 CS3121014 课程的学生最高分数
1 2 3 4 5 6
SELECT MAX(grade) FROM sc WHERE sc.cno ='CS3121014';
14.查询学生 03051014 选修课程的总学分数
1 2 3 4 5
SELECTSUM(ccredit) FROM course WHERE cno IN ( SELECT cno FROM sc WHERE sno ='03051014' );
15.查询选修了 3 门以上 (>=) 课程的学生学号
1 2 3 4 5 6 7 8
SELECT sno FROM sc GROUPBY sno HAVING COUNT ( * ) >3;
16.查询有 4 门以上 (>=) 课程是 90 分以上的学生的学号及 (90 分以上的) 课程数
1 2 3 4 5 6 7 8 9 10 11
SELECT sno, COUNT ( * ) FROM sc WHERE grade >=90 GROUPBY sno HAVING COUNT ( * ) >=4;