数据库上机实验题目和答案
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
试用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 s
where sex='男' and age>23
3.检索‘c01’课程中一门课程的女学生姓名
select sname from s
where sex='女' and sno in
(select sno from sc where cno='c01')
4.检索s01同学不学的课程的课程号。
select cno from c
where cno not in (select cno from sc where sno ='s01')
5.检索至少选修两门课程的学生学号。
select sc.sno from s,sc
where s.sno=sc.sno
group by sc.sno
having count(o)>=2
6.每个学生选修的课程门数。
解法一:
select so.sno sno,ount,s.sname
from(select sc.sno sno,count(sc.sno) ccount
from sc,s
where s.sno=sc.sno
group by sc.sno ) so,s
where s.sno=so.sno
解法二:
select sc.sno sno,s.sname,count(sc.sno) ccount
from sc,s
where s.sno=sc.sno
group by sc.sno,sname
7.求选修C4课程的学生的平均分。
select avg(grade) 'c04平均成绩'
from sc
where cno='c04'
8.求每个学生平均成绩。
select s.sno,s.sname,avg(sc.grade) '平均成绩'
from sc,s
where s.sno=sc.sno
group by s.sno,s.sname
9.统计每门课程的学生选修人数(超过3人的课程才统计)。询结果按人数降序排列,若
人数相同,按课程号升序排列。
select o,ame,count(o) number
from c,sc
where o=o
group by o,o,ame
having count(*)>3
order by number desc,o
10.检索学号比王丽同学大,而年龄比他小的学生姓名。
select sno,sname from s
where sno>(select sno from s where sname='王丽') and
age<(select age from s where sname ='王丽')
11.检索姓名以王打头的所有学生的姓名和年龄。
select sname,age from s
where sname like '王%'
12.在SC中检索成绩为空值的学生学号和课程号。
select sno,cno from sc
where grade is NULL
13.输出c01 课程的成绩单,要求成绩按从高到低排序。
select sname,grade 'c01成绩' from sc,s
where s.sno=sc.sno and cno='c01'
order by grade desc
14.查询姓名为‘X国X’同学。
select sname from s
where sname like '_国_'
15.查询没有选修c02,c03,c04的同学的学号。
select sno
from s
where not exists(
select sno
from sc
where s.sno=sc.sno and cno in('c02','c03','c04'))
16.查询没有参加考试的同学的学号。
select sno
from s
where exists(
select *
from sc
where s.sno=sc.sno and grade is null)
17.检索计算机系开课程的课程号和课程名。
select cno,cname from c
where dept in
(select dept from d where dname='计算机')
18.检索至少选修计算机系所开课程2门以上的女学生姓名
select sname
from s,sc,c,d
where s.sex='女'and s.sno=sc.sno and o=o and dname='计算机' and d.dept=c.dept
group by sname
having count(s.sno)>2
19.检索王丽同学不学的课程的课程号。
select cno from c where cno not in
(select cno from sc where sno in (select sno from s where sname='王丽'))