一、
1、查询7号课程没有考试成绩的学生学号。
Select snofrom sc where cno='7' and grade is null
2、查询7号课程成绩在90分以上或60分以下的学生学号。
Select snofrom sc where cno='7' and grade not between 60 and 90
3、查询课程名以“数据”两个字开头的所有课程的课程号和课程名。
Selectcno,cname from c where cname like '数据%'
4、查询每个学生所有课程的平均成绩,输出学生学号和平均成绩。
Selectsno,avg(grade) from sc group by sno
5、查询每门课程的选修人数,输出课程号和选修人数。
Selectcno,count(*) from sc group by cno
6、查询选修7号课程的学生的学号、姓名、性别。
Selects.sno,sname,ssex from s,sc where s.sno=sc.sno and cno='7'
或: Selectsno,sname,ssex from s where sno in
( Select sno from sc where cno='7' )
7、查询选修7号课程的学生的平均年龄。
Selectavg(sage) from s,sc where s.sno=sc.sno and cno='7'
或: Selectavg(sage) from s where sno in
( Select sno from sc where cno='7' )
8、查询有30名以上学生选修的课程号。
Select cnofrom sc group by cno having count(*)>30
9、查询至今没有考试不及格的学生学号。
Selectdistinct sno from sc where sno not in
( Select sno from sc wheregrade<60 )
或: Selectsno from sc group by sno having min(grade)>=60
二、
1、找出选修课程号为C2的学生学号与成绩。
Selectsno,grade from sc where cno='C2'
2、找出选修课程号为C4的学生学号与姓名。
Selects.sno,sname from s,sc where s.sno=sc.sno and cno='C4'
注意本题也可以用嵌套做
思考本题改为“找出选修课程号为C4的学生学号、姓名与成绩”后还能用嵌套做吗?
3、找出选修课程名为 Maths 的学生学号与姓名。
Selects.sno,sname from s,sc,c
where s.sno=sc.sno and c.cno=sc.cno andcname='Maths'
注意本题也可以用嵌套做
4、找出选修课程号为C2或C4的学生学号。
Selectdistinct sno from sc where cno in ('C2','C4')
或: Selectdistinct sno from sc where cno='C2' or cno='C4'
5、找出选修课程号为C2和C4的学生学号。
Select snofrom sc where cno='C2' and sno in
( Select sno from sc where cno='C4' )
注意本题也可以用连接做
思考:Selectdistinct sno from sc where cno='C2' and cno='C4'正确吗?
6、找出不学C2课程的学生姓名和年龄。
Selectsname,sage from s where sno not in
( Select sno from sc where cno='C2' )
或: Selectsname,sage from s where not exists
( Select * from sc where sno=s.sno and cno='C2')
7、找出选修了数据库课程的所有学生姓名。(同3)
Select snamefrom s,sc,c
where s.sno=sc.sno and c.cno=sc.cno andcname='数据库'
8、找出数据库课程不及格的女生姓名。
连接:Selectsname from s,sc,c
where s.sno=sc.sno and c.cno=sc.cno andcname='数据库'
and grade<60 and ssex='女'
嵌套:Selectsname from s where ssex='女' and sno in
( Select sno from sc where grade<60 and cno in
( Select cno from c where cname='数据库' )
)
9、找出各门课程的平均成绩,输出课程名和平均成绩。
Selectcname,avg(grade) from sc,c
wherec.cno=sc.cno group by c.cno,cname
思考本题也可以用嵌套做吗?
10、找出各个学生的平均成绩,输出学生姓名和平均成绩。
Selectsname,avg(grade) from s,sc
wheres.sno=sc.sno group by s.sno,sname
思考本题也可以用嵌套做吗?
11、找出至少有30个学生选修的课程名。
Select cnamefrom c where cno in
( Select cno from sc group by cno havingcount(*)>=30 )
注意本题也可以用连接做
12、找出选修了不少于3门课程的学生姓名。
Select snamefrom s where sno in
( Select sno from sc group by sno having count(*)>=3)
注意本题也可以用连接做
13、找出各门课程的成绩均不低于90分的学生姓名。
Select snamefrom s where sno not in
( Select sno from sc where grade<90 )
或: Selectsname from s,sc where s.sno=sc.sno
group by s.sno,sname having min(grade)>=90
14、找出数据库课程成绩不低于该门课程平均分的学生姓名。
Select snamefrom s,sc,c
where s.sno=sc.sno and sc.cno=c.cno andcname='数据库' and grade>
( Select avg(grade) from sc,c