三、实验主要仪器设备和材料
1.计算机及操作系统:PC机,Windows 2000/xp/win7;
2.数据库管理系统:SQL sever 2005/2008;
四、实验方法、步骤及结果测试
实验题目:
1、对实验一建立的数据库表进行查询
简单查询:
1 题目1、查询学生的系别,用小写字母表示。
2 SQL语句
3 select distinct LOWER(Sdept) from S;
1 题目2、查询叫“刘%”的学生学号、系别
2 SQL语句
3 select Sno Sdept from S where Sname like '刘\%' escape'\';
1 题目3、查询教师姓名第二个字是“一”的教师姓名,所教课程名。
2 SQL语句
3 select Tname,Cname from C where Tname like '_一_';
1 题目4、查询没有成绩的学生学号,学号降序排列。
2 SQL语句
3 select S.Sno from S where not exists(
4 select * from Sc where S.Sno=Sc.Sno
5 )
6 order by S.Sno desc;
1 题目5、查询选修课程的学生人数
2 SQL语句
3 select count(distinct Sno) As '学生人数' from Sc
4 --select count(*)
5 --from S
6 --where exists( select * from Sc where Sc.Sno = S.Sno )
1 题目6、各科选修课成绩及格的选修人数。
2 SQL语句
3 select count(distinct Sno)As'及格人数' from Sc where Grade>=60
4 group by Cno;
1 题目7、各学院男生人数,超过2人的输出学院名和人数,并按男生人数降序排列
2 SQL语句
3 select Ssp,count(*) from S where S.Ssex='男'
4 group by Ssp
5 having count(*)>2
6 order by count(*) desc;
1 题目8、查询IS系男女学生人数各是多少SQL语句
2 select S.Ssex As'性别',count(*) As'IS系人数' from S where S.Sdept='IS'
3 group by Ssex;
1 题目9、找出年龄>23岁,平均成绩不及格的学生学号和平均分
2 SQL语句
3 select sno,avg(grade) from SC
4 where sno in(
5 select sno from S where getdate()-sbirth>23
6 )
7 group by sno
8 having avg(grade)23
14 group by S.Sno
15 having AVG(Sc.Grade)(Select cCredit from C where Cname='计算机应用')
5 group by Cname,cCredit;
1 题目13、查询CS系中年龄比IS系所有人年龄都小的学生学号和姓名
2 SQL语句
3 use Student
4 Select Sno As'学生学号',Sname As'姓名' from S
5 where Sdept='CS' and Sbirth>(Select Max(sBirth) from S where Sdept='IS')
6 group by Sno,Sname;
7 或者:
8 use Student
9 Select Sno As'学生学号',Sname As'姓名' from S
10 where Sdept='CS' and Sbirth>all(Select sBirth from S where Sdept='IS')
11 group by Sno,Sname;
1 题目14、is系没有选修02号课程的学生学号和学生姓名
2 SQL语句
3 Select Sno As'学生学号',Sname As'学生姓名' from S
4 where S.Sdept='IS'and not exists (select * from Sc where Sc.Sno=S.Sno and Sc.Cno=02)
5 group by Sno,Sname;
1 题目15、被全部学生都选修的课程号、课程名
2 SQL语句
3 select Cno As'课程号',Cname As'课程名' from C
4 where not exists(select * from S where not exists (select * from Sc where Sc.Sno=S.Sno and Sc.Cno=C.Cno))
1 题目16、选修了01号课又选修了02号课的学生的学号和姓名
2 实现代码及查询结果截图:
3 SQL语句
4 select S.Sno As'学生学号',S.Sname As'学生姓名' from S,Sc
5 where Sc.Sno=S.Sno and Sc.Cno=01
6 intersect
7 select S.Sno As'学生学号',S.Sname As'学生姓名' from S,Sc
8 where Sc.Sno=S.Sno and Sc.Cno=02;
9 或者:
10 select Sname,Sno from S
11 where Sno in(
12 select Sno from SC where cno='01' and Sno in(
13 select Sno from SC where Cno='02'))
1 题目17、被全部IS系的学生都选修的课程号和课程名
2 SQL语句
3 select Cno As'课程号',Cname As'课程名' from C
4 where not exists(select * from S where S.Sdept='IS'and not exists (select * from Sc where Sc.Sno=S.Sno and Sc.Cno=C.Cno))
1 题目18、查询选修高等数学与选修数据库的学生差集
2 SQL语句
3 select * from S
4 where exists (select *from Sc where Sc.Sno=S.Sno and exists(select * from C where Sc.Cno=C.Cno and C.Cname='高等数学'))
5 except
6 select * from S
7 where exists (select *from Sc where Sc.Sno=S.Sno and exists(select * from C where Sc.Cno=C.Cno and C.Cname='数据库'));
8 或者:
9 select Sno from SC,C where
10 C.Cname='高等数学' and C.Cno=SC.Cno and SC.Sno not in(
11 select Sno from SC,C where SC.Cno=C.Cno and C.Cname='数据库')
12
1 题目19、没有选修“李一”老师开设的课程的学生学号、姓名、系别
2 SQL语句
3 select S.Sno As'学生学号',S.Sname As'学生姓名',S.Ssp As'系别' from S
4 where not exists(
5 select * from SC,C where SC.cno=C.cno and SC.sno=S.sno
6 and tname='李一')
1 题目20、查询各选修学生最高分最低分之差大于30分的学生学号
2 SQL语句
3 select Sc.Sno As'学生学号' from Sc
4 group by Sno
5 having max(Grade)-min(Grade)>30;