0
点赞
收藏
分享

微信扫一扫

数据库作业(3)数据查询(自用复习)

科牛 2022-04-03 阅读 53
数据库

#统计各门课程选修人数,要求输出课程代号,课程名,有成绩人数(grade不为NULL),最高分,最低分,平均分(取整round函数),按课程号排序。

select sc.cno,cname,count(*),max(grade),min(grade),round(avg(grade))
from sc,course
where grade is not null and sc.cno = course.cno
group by sc.cno
order by sc.cno

#查询同时选修了c01,c02,c03课程学生,输出学号,姓名

select sc.sno,sname
from student,sc
where sc.sno = student.sno and cno in('c01','c02','c03')
group by sc.sno
having count(distinct cno)=3

#查询没有同时选修“计算机导论”和“计算机网络”两门课的学生的学号,姓名

-- 不存在这两门课都选修的人
select sno,sname from student
where sno not in(SELECT sno from sc,course
                                  where sc.cno=course.cno and cname='计算机导论'
                                           and sno in(SELECT sno from sc,course
                                                               where sc.cno=course.cno and cname='计算机网络'))

#查询选修了全部课程的学生的学号,姓名,系名

SELECT sno,sname,sdept 
from student -- 不存在没有选修的课程
where not EXISTS (SELECT * from  course where not EXISTS(SELECT * from sc where course.cno = sc.cno and sc.sno = student.sno))

#查询与“王大力”同一个系的学生的基本信息。

select * from student
where sdept in (select sdept from student where sname = '王大力')
 and sname!='王大力'

#查询同时选修了“数据库基础”和“计算机网络”两门课的学生的学号,姓名

select sno,sname
from student
where sno in(select sno from sc,course
                     where sc.cno = course.cno  and cname='数据库基础' 
                                 and sno in(select sno from sc,course
                                                 where sc.cno = course.cno  and cname='计算机网络' ))

select sc.sno,sname 
from student,sc
where sc.sno = student.sno and sc.grade>=60
group by sc.sno 
having count(cno)>=2
select sc.sno,sname,count(cno)
from student,sc
where sc.sno = student.sno and sc.grade<60
group by sc.sno 
having count(cno)>=2
order by count(cno) desc,sc.sno asc


 

select sc.sno,sname,cname,grade
from student,sc,course
where student.sno = sc.sno and sc.cno = course.cno 
group by sc.sno
having avg(grade)>=60
order by sc.sno
select cno,cname 
from course 
where not exists (select * from student  where not exists
                                  (select * from sc where student.sno=sc.sno and  sc.cno = course.cno ))
select sc.sno,sname
from student,course,sc
where student.sno = sc.sno and course.cno =sc.cno
group by sc.sno
having count(sc.cno)>=all(select count(DISTINCT sc.cno)as t1 from sc,course where course.cno=sc.cno group by sno)
select distinct sc.sno,sname
from student,sc,course,(select cno from sc where sc.sno=9521102 group by cno) as g1
where g1.cno = course.cno and student.sno = sc.sno 
select sno,cno,grade
from sc g1
where grade >(select avg(grade) from sc g2 where g1.cno = g2.cno)

查询统计学生不及格门数大于等于2门的信息,输出系名,学号,姓名,不及格门数,按照系(升序)排序,不及格门数(降序)排序。

select sdept,sc.sno,sname,count(cno)
from student,sc
where grade<60 and student.sno = sc.sno
group by sc.sno
having count(cno)>=2
order by sdept,count(cno) desc;

select sc.sno,sname
from student,sc,course
where course.cno = sc.cno and sc.sno = student.sno and sc.cno ='c03'

select cno,cname
from course
where not exists(select * from  sc,student where student.sno = sc.sno and sc.cno = course.cno)

SELECT k.sno,k.cno,grade
from sc as k,course,student 
where student.sno = k.sno and k.cno = course.cno and grade in(SELECT max(grade) from sc where sno = k.sno group by sno)
-- 每个男生的选课
-- 没有选课的男生
-- 没有选课的不是男生
select sno,count(cno)
from sc
where not exists(select * from course where course.cno = sc.cno 
								and not exists(select * from student where ssex='男' and student.sno = sc.sno ))
group by sno

举报

相关推荐

0 条评论