文章目录
7、查询非计算机科学系中比计算机学科系所有学生年龄都小的学生姓名和年龄
[8、基于 SC表,查询选修了全部课程的学生姓名](#8、基于 SC表,查询选修了全部课程的学生姓名)
[10、在 SC表中查询至少选修了1号学生选修的全部课程的学生的学号](#10、在 SC表中查询至少选修了1号学生选修的全部课程的学生的学号)
17、统计每个同学选修及格的课程的平均分,列出其学号、姓名及平均分。
19、查询选修了C1和C2课程,并且成绩均大于85分的同学的学号、课程号以及成绩
28、查询至少选修了2020003号学生所选的全部课程的学生的学号与姓名
1、查询选修三门以上课程的学生学号
SELECT Sno
FROM SC
GROUP BY Sno
HAVING COUNT(Cno)>3;
2、查询选修课程中至多一门>70分的学生学号
SELECT Sno
FROM SC
WHERE Sno NOT IN
(SELECT Sno
FROM SC
WHERE Grade >70)
UNION
SELECT Sno
FROM SC
WHERE Grade>70
GROUP BY Sno
HAVING count(Cno)=1 ;
3、查询平均成绩>=90分的学生学号和平均成绩
SELECT Sno,avg(Grade)
FROM SC
GROUP BY Sno
HAVING avg(Grade)>=90;
4、查询成绩都大于70分学生的成绩
SELECT distinct Sno
FROM SC
WHERE Grade NOT IN(
SELECT Grade
FORM SC
WHERE Grade<=70);
5、找出每个学生超过他自己选修课程平均成绩的课程号
SELECT Sno,Cno
FROM SC X
WHERE Grade>=(
SELECT avg(grade)
FROM SC Y
WHERE Y.Sno=X.Sno);
6、查询非计算机科学系某一个学生年龄小的学生姓名和年龄
SELECT Sname,Sage
FROM student
WHERE Sage<ANY(
SELECT Sage
FROM Student
WHERE Sdept='CS')
AND Sdept <>'CS';
7、查询非计算机科学系中比计算机学科系所有学生年龄都小的学生姓名和年龄
SELECT Sname,Sage
FROM student
WHERE Sage<ALL(
SELECT Sage
FROM Student
WHERE Sdept='CS')
AND Sdept <>'CS';
8、基于 SC表,查询选修了全部课程的学生姓名
SELECT Sname
FROM Student
WHERE NOT EXISTS
(SELECT *
FROM Course
WHERE NOT EXISTS
(SELECT *
FROM SC
WHERE Sno=Student.Sno AND Cno=Course.Cno)
);
9、查询选修了1号课程的学生姓名
SELECT Sname
FROM Student
WHERE EXISTS(
SELECT *
FROM SC
WHERE SC.Sno=Student.Sno AND Cno='1');
10、在 SC表中查询至少选修了1号学生选修的全部课程的学生的学号
SELECT DISTINCT SnO
FROM SC SCX
WEHRE NOT EXISTS
(SELECT *
FROM SC SCY
WHERE SCY.SnO='1' AND NOT EXISTS
(SELECT *
FROM SC SCZ
WHERE SCZ.Sno=SCX.Sno AND SCZ.Cno=SCY.Cno)
);
11、找出每个学生超过自己选修课程平均成绩的学号和课程号
SELECT Sno,Cno
FROM SC,(SELECT Sno,avg(Grade)
FROM SC
GROUP BY Sno) AS avgsc(avgsno,avggrade);
WHERE SC.Sno=avgsc.avgsno AND SC.Cno>avgsc.avggrade;
或
SELECT Sno,Cno
FROM SC x
WHERE Grade>=(SELECT avg(Grade)
FROM SC y
WHERE y.Sno=x.Sno);
12、查询所有选修1号课程的学生名字
SELECT Sname
FROM Student,(SELECT Sno
FROM SC
WHERE Cno='1') AS SC1
WHERE Student.Sno=SC1.Sno;
13、查询所有选课学生的学号、姓名、课程名称及成绩。
select SC.Sno,Sname,Cname,Grade
from SC,Course,Student
where SC.Cno=Course.Cno and Student.Sno=SC.Sno
14、查询每门课程的课程号、课程名及选课人数。
select SC.Cno,Course.Cname,count(Sno)
from SC, Course
where SC.Cno=Course.Cno
group by SC.Cno,Course.Cname
15、查询每个学生的学号、姓名及选课门数。
select SC.Sno,Sname,count(Cno)
from SC,Student
where SC.Sno=Student.Sno
group by SC.Sno,Sname
16、查询姓张的学生所选修的课程号、课程名称。
select distinct SC.Cno,Cname
from SC,Course,Student
where SC.Cno=Course.Cno and Student.Sno=SC.Sno and Sname like '张%'
17、统计每个同学选修及格的课程的平均分,列出其学号、姓名及平均分。
select SC.Sno,Sname,avg(Grade)
from SC,Student
where SC.Sno=Student.Sno and Grade>60
group by Sname,SC.Sno
18、查询选修了课程C1和C2的学生的学号和其各自成绩
select SC1.Sno,SC1.Grade Grade1,SC2.Grade Grade2
from SC SC1,SC SC2
where SC1.Sno=SC2.Sno and SC1.Cno='81001' and SC2.Cno='81002'
19、查询选修了C1和C2课程,并且成绩均大于85分的同学的学号、课程号以及成绩
select SC1.Sno,SC1.Cno Cno1,SC2.Cno Cno2,SC1.Grade Grade1,SC2.Grade Grade2
from SC SC1,SC SC2
where SC1.Sno=SC2.Sno and SC1.Cno='1' and SC2.Cno='2' and SC1.Grade>85 and SC2.Grade>85
20、查询每一门课程的间接先修课程号
select Course1.Cno,Course2.Cpno
from Course Course1,Course Course2
where Course1.Cpno=Course2.Cno
21、查询跟20201031号学生同学院的学生的学号与姓名
select S2.Sno,S2.Sname
from Student S1,Student S2
where S1.Smajor=S2.Smajor and S1.Sno='20180002'
22、查询选修了数据库这门课的学生的学号及姓名
select Student.Sno,Sname
from Student,SC,Course
where SC.Sno=Student.Sno and SC.Cno=Course.Cno and Course.Cname='数据库'
23、查询跟李四在同一个系学习的学生的学号和姓名
select Sno,Sname
from Student
where Sdept in (
select Sdept
from Student
where Sname='李四'
)
24、查询至少有一门课成绩大于70分的学生的学号与姓名
select distinct SC.Sno,Sname
from SC,Student
where SC.Sno=Student.Sno and SC.Grade >70
或者
select distinct Sno,Sname
from Student
where exists (
select*
from SC
where Student.Sno=SC.Sno and SC.Grade>70
)
25、查询既选修了C1课程又选修了C2课程的学生的学号
select SC.Sno
from SC,(
select Sno
from SC
where Cno='1'
) S1
where S1.Sno=SC.Sno and SC.Cno='2';
或者
select SC1.Sno
from SC SC1
where exists (
select *
from SC SC2
where SC1.Sno=SC2.Sno and SC1.Cno='1' and SC2.Cno='2'
)
26、查询所选课程成绩全部大于70分的学生的学号
select distinct SC1.Sno
from SC SC1
where not exists(
select *
from SC SC2
where SC1.Sno=SC2.Sno and (SC2.Grade<70 or SC2.Grade is null)
)
27、查询选修了全部课程的学生的学号和姓名
select Sname
from Student
where not exists(
select *
from Course
where not exists(
select *
from SC
where SC.Sno=Student.Sno and SC.Cno=Course.Cno
)
)
28、查询至少选修了2020003号学生所选的全部课程的学生的学号与姓名
select distinct SCx.Sno,Sname
from SC SCx,Student
where SCx.Sno=Student.Sno and not exists(
select *
from SC SCy
where SCy.Sno='2020003' and not exists(
select *
from SC SCz
where SCz.Sno=SCx.Sno and SCz.Cno=SCy.Cno
)
)