关于SQL练习2的一些写法。。。查询选修人数超过2人且成绩都在60分以上的课程 查询选修张老师讲授所有课程的学生 计算并填写学生获得的总学分

10-1 查询没有选修’C语言’课程的学生 (10分)
本题目要求编写SQL语句, 检索出没有选修’C语言’课程的学生记录,输出结果集按照学号升序排序。

提示:请使用SELECT语句作答。请使用not exist实现。

select sno as 学号,sname as 姓名 from stu  
where  sno  not in
(select distinct sno from sc where  
  not exists(select * from cou where cou.cno = sc.cno and cname != 'C语言') )  
			 order by sno asc;

10-2 查询S001学生选修而S003学生未选修的课程 (10分)
本题目要求编写SQL语句, 检索出 sc表中学号为S001的学生选修的而S003学号学生未选修的课程号。

提示:请使用SELECT语句作答。MySQL不允许使用 except语句。

select cno as 课程号 from sc
where cno not in(
select s1.cno as 课程名  from sc s1 join sc s2
on s1.cno = s2.cno
where
(s1.sno = 's001' and s2.sno = 's003')) and sno = 's001';

10-4 查询平均分高于80分的学生 (10分)
本题目要求编写SQL语句, 查询平均分高于80分的学生姓名。

提示:请使用SELECT语句作答。

select  sname  from stu 
where sno in
(
select sno from sc group by sno having avg(grade) >= 80);

10-5 查询选修张老师讲授所有课程的学生 (10分)
本题目要求编写SQL语句, 查询选修了张老师所讲授的所有课程的学生。

提示:请使用SELECT语句作答。

select sname
from stu
where not exists
(select * 
from cou
where not exists
(select *
from sc 
where sc.sno=stu.sno and sc.cno=cou.cno) and cou.teacher = '张老师')

10-6 计算并填写学生获得的总学分 (10分)
本题目要求编写UPDATE语句, 计算每位学生已获得的总学分并填写在stu表中的totalcredit字段。

其中,总学分为每个学生通过的选修课程的学分数总和,注意:只有在60分以上的选课成绩才能获得该门课程的学分数,每门课程的学分数在cou表中credit字段。

update stu,(select sno sno,sum(credit) total 
from
(select sc.sno sno,case when sc.grade>=60 then credit else NULL end credit
from stu
join sc on sc.sno = stu.sno
join cou on sc.cno = cou.cno
group by sc.sno,credit,grade) nstu1
group by sno) nstu2
set stu.totalcredit = nstu2.total
where stu.sno = nstu2.sno;

这题放这儿感觉有点超标,用到后面的when then等了。

10-7 通过图书表和借阅表,查询图书的借阅情况,要求结果中包括以下几列:账号,条形码,书名和借书日期 (10分)
本题目要求编写SQL语句,通过图书表和借阅表,查询图书的借阅情况,要求结果中包括以下几列:账号,条形码,书名和借书日期

提示:请使用SELECT多表查询的方法。

select 借阅.账号,借阅.条形码,图书.书名,借阅.借书日期 from 图书 join
借阅
on 图书.条形码 = 借阅.条形码

10-8 查询软件工程专业中年龄最大的同学姓名 (10分)
本题目要求编写SQL语句, 查询软件工程专业中年龄最大的同学姓名.

提示:请使用SELECT语句作答。

select sname from stu 
where mno = (select mno from major where mname = '软件工程')
and birdate = 
(select min(birdate) from stu)

10-9 查询选修了“C语言”课程,但是没有选修“数据结构”课程的学生 (10分)
本题目要求编写SQL语句,查询选修了“C语言”课程,但是没有选修“数据结构”课程的学生姓名。

提示:请使用SELECT语句作答。

select sname from stu where sno in(
select distinct sno from sc where sno in(
select sno from sc 
where sno not in
(select sno from sc where cno = 'c003')  and cno = 'c002'))

(select sno from sc where cno = ‘c003’) and cno = ‘c002’)) 中也可以用子查询查出等于
“C语言“以及不等于“数据结构”的cno。

10-10 查询选修课程超过2门且成绩都在80分以上的学生 (10分)
本题目要求编写SQL语句,查询选修课程超过2门且成绩都在80分以上的学生的姓名、专业及总学分。

提示:请使用SELECT语句作答。

select sname 姓名,mno 专业,sum(credit) 总学分 from stu join
sc on stu.sno = sc.sno join cou
on sc.cno = cou.cno
group by sname,mno
having count(sc.cno) >= 2 and min(grade) >=80;

10-11 查询选修人数超过2人且成绩都在60分以上的课程 (10分)
本题目要求编写SQL语句,查询选修人数超过2人且成绩都在60分以上的课程的课程名、最高成绩、最低成绩和平均成绩。

提示:请使用SELECT语句作答。

select sc.cno 课程号,cname 课程名,max(grade) 最高成绩,min(grade) 最低成绩,avg(grade) 平均成绩 from sc join
cou on sc.cno = cou.cno
group by sc.cno,cou.cname having count(sc.cno) > 2 and min(grade) >= 60 and count(*) = count(grade )
;

猜你喜欢

转载自blog.csdn.net/zheziu/article/details/111563788