当前位置:文档之家› 数据库上机实验题目和答案

数据库上机实验题目和答案

试用SQL的查询语句表达下列查询:1.检索王丽同学所学课程的课程号和课程名。

select Cno ,Cname from c where Cno in(select cno from sc where sno in (select sno from s where sname='王丽' ))2.检索年龄大于23岁的男学生的学号和姓名。

select sno,sname from swhere sex='男' and age>233.检索‘c01’课程中一门课程的女学生姓名select sname from swhere sex='女' and sno in(select sno from sc where cno='c01')4.检索s01同学不学的课程的课程号。

select cno from cwhere cno not in (select cno from sc where sno ='s01')5.检索至少选修两门课程的学生学号。

select sc.sno from s,scwhere s.sno=sc.snogroup by sc.snohaving count(o)>=26.每个学生选修的课程门数。

解法一:select so.sno sno,ount,s.snamefrom(select sc.sno sno,count(sc.sno) ccountfrom sc,swhere s.sno=sc.snogroup by sc.sno ) so,swhere s.sno=so.sno解法二:select sc.sno sno,s.sname,count(sc.sno) ccountfrom sc,swhere s.sno=sc.snogroup by sc.sno,sname7.求选修C4课程的学生的平均分。

select avg(grade) 'c04平均成绩'from scwhere cno='c04'8.求每个学生平均成绩。

select s.sno,s.sname,avg(sc.grade) '平均成绩'from sc,swhere s.sno=sc.snogroup by s.sno,s.sname9.统计每门课程的学生选修人数(超过3人的课程才统计)。

询结果按人数降序排列,若人数相同,按课程号升序排列。

select o,ame,count(o) numberfrom c,scwhere o=ogroup by o,o,amehaving count(*)>3order by number desc,o10.检索学号比王丽同学大,而年龄比他小的学生姓名。

select sno,sname from swhere sno>(select sno from s where sname='王丽') andage<(select age from s where sname ='王丽')11.检索姓名以王打头的所有学生的姓名和年龄。

select sname,age from swhere sname like '王%'12.在SC中检索成绩为空值的学生学号和课程号。

select sno,cno from scwhere grade is NULL13.输出c01 课程的成绩单,要求成绩按从高到低排序。

select sname,grade 'c01成绩' from sc,swhere s.sno=sc.sno and cno='c01'order by grade desc14.查询姓名为‘X国X’同学。

select sname from swhere sname like '_国_'15.查询没有选修c02,c03,c04的同学的学号。

select snofrom swhere not exists(select snofrom scwhere s.sno=sc.sno and cno in('c02','c03','c04'))16.查询没有参加考试的同学的学号。

select snofrom swhere exists(select *from scwhere s.sno=sc.sno and grade is null)17.检索计算机系开课程的课程号和课程名。

select cno,cname from cwhere dept in(select dept from d where dname='计算机')18.检索至少选修计算机系所开课程2门以上的女学生姓名select snamefrom s,sc,c,dwhere s.sex='女'and s.sno=sc.sno and o=o and dname='计算机' and d.dept=c.deptgroup by snamehaving count(s.sno)>219.检索王丽同学不学的课程的课程号。

select cno from c where cno not in(select cno from sc where sno in (select sno from s where sname='王丽'))20.检索选修计算机系所开全部课程的学生的学号与姓名。

select s.sno,snamefrom s,sc,c,dwhere s.sno=sc.sno and o=o and d.dname='计算机' and d.dept=c.dept group by sname,s.sno21.检索选修C语言且成绩大于80分的计算机系的学生的姓名与学号。

select sname,s.snofrom sc,s,d,cwhere dname='计算机' and s.dept=d.dept and ame='C语言' and o=o and s.sno=sc.sno and sc.grade>8022.查询选修3门以上课程且平均成绩大于80的学生的学号、姓名、所在系名。

select s.sno,sname,dname from sc,s,d where s.sno=sc.sno and s.dept=d.dept group by s.sno,sname,dnamehaving count(cno)>3 and avg(sc.grade)>8023.求计算机系所开课程的每门课程的学生平均成绩,按平均成绩从高到低排序输出。

select o,avg(sc.grade)from sc, d,cwhere d.dname='计算机' and o=o and c.dept=d.deptgroup by oorder by avg(sc.grade) desc24.检索缺考2门以上的学生姓名。

select s.snamefrom s,scwhere s.sno=sc.sno and sc.grade is nullgroup by snamehaving count (*)>225.检索缺考或不及格们数3门以上的学生学号和姓名。

select distinct s.sno,s.snamefrom s,scwhere s.sno=sc.sno and (sc.grade<60 or sc.grade is null) and s.sname in( select s.snamefrom s,scwhere s.sno=sc.sno and (sc.grade<60 or sc.grade is null) and s.sname in( select s.snamefrom s,scwhere s.sno=sc.sno and (sc.grade<60 or sc.grade is null)group by s.snamehaving count (*)>3))26.检索每个学生的总学分select s.sno,sum(c.credit) '总学分'from s,c,scwhere s.sno=sc.sno and o=ogroup by s.sno27.查询既选修了‘数据库’又选修了‘操作系统’的学生的姓名及学号from s,sc,cwhere ame='数据库'and s.sno=sc.sno and o=o and s.sno in( select s.snofrom s,sc,cwhere ame='操作系统'and s.sno=sc.sno and o=o)28.查询即没有选修‘数据库’又没有选修‘操作系统’的学生的姓名及学号。

select sname,s.snofrom s,sc,cwhere s.sno=sc.sno and o=o and s.sno not in(select s.snofrom s,sc,cwhere ame='操作系统'and s.sno=sc.sno and o=o or s.sno in( select s.snofrom s,sc,cwhere ame='数据库'and s.sno=sc.sno and o=o))。

相关主题