1、实验报告第四次实验名称 :SQL数据查询一、实验目的 :数据查询语句是SQL语句的重要组成部分,合理使用数据查询语句,可以极大的简化应用程序编制、快速的定位分析数据库系统的故障,查询语句是编程人员与数据库管理人员必不可少的工具,通过实验达到以下目的:(1)加深学生对查询语句基本概念的理解与掌握,最终达到灵活应用。(2)掌握SELECT 语句的基本语法。(3)掌握简单单表查询、连接查询、嵌套查询。(4)学会使用聚函数和进行分组查询。二、实验内容:1、单表查询:2、连接查询3、嵌套查询 三、实验环境 Windows xp系统 SQL Server2000 服务器四、程序源码与运行结果 1、单表查询
2、:设计查询语句完成对 *、distinct、like(%和_)、in 、not in 、between and、order by 、group by等的应用。(1)检索出学生信息表中所有女生的记录。Select * from student where sex=女(2)从选课成绩表中检索出所有学生的成绩,并去除重复值。select distinct grade from cs(3)从课程表中检索出全部数据的信息。select * from course where cname like 数据%(3)从学生信息表中检索出姓王的学生的信息。select * from student where s
3、name like 王_(4)从成绩表中找出成绩等于60分的学生的性别。select sex from student where sno in (select sno from cs where grade=60)(5)找出不在成绩表中的学生的所有信息。select * from student where sno not in (select sno from cs)(6)在成绩表中找出成绩从70到85分的所有信息。select * from cs where grade between 70 and 85(7)将学生表中的所有学生的年龄按升序排列。select * from studen
4、t order by age(8)检索出没门课程的平均分。select cno,avg(grade) from cs group by cno2、连接查询设计查询语句,分别用两种方式(where+连接条件和joinon)表示连接条件实现连接查询(1)找出成绩大于90分的姓名和他们所在的专业。(where+连接条件)select distinct sname,dept from student,cs where (grade90)(2)找出成绩大于85分的姓名和他们所在的专业。(joinon)select distinct sname,deptfrom student join cs on(st
5、udent.sno=cs.sno) where (grade85)3、嵌套查询具体要完成的任务如下:1. 查询全体学生的学号与姓名select sno,sname from student2. 查询全体学生的全部信息,并为学生表命名别名。select * from student W,course E ,cs B where W.sno=B.sno and E.cno=B.cno3. 查全体学生的出生年份,并为年份加上标题select 出生日期 from student4. 查询选修了课程的学生学号,要求消除重复行select sno from student where sno in(sel
6、ect sno from cs)5. 查询所有年龄在20岁以下的学生姓名及其年龄。select sname,age from student where age=20 and age=23) (第二种)7. 使用IN关键字查询信息系(IS)、数学系(MA)和计算机科学系(CS)的学生select * from student where sno in(select sno from student where dept=IS)select * from studentwhere sno in(select sno from student where dept=MA)select * from
7、 studentwhere sno in(select sno from student where dept=CS)8. 查询既不是信息系、数学系,也不是计算机科学系的学生的姓名和性别。select sname,sex from student where dept!=MAand dept!=CSand dept!=IS9. 查询所有姓刘学生的姓名、学号和性别。select sname,sno,sex from student where sname like 刘%10. 查询名字中第2个字为阳字的学生的姓名和学号。select sname,sno from student where sn
8、ame like _阳11. 查询DB_Design课程的课程号和学分(先在Course表中插入“DB_Design”课程信息)。select cname,score from course where cname=DB_Design12. 查询没有考试成绩的学生学号和课程号。select sno,cno from cs where grade is null13. 查询计算机系年龄在20岁以下的学生姓名。select sname from student where age=321、 查询有3门以上课程是90分以上的学生的学号及(90分以上的)课程数。select sno,count(cno
9、) from cs where grade=90group by sno having count(cno)=322、 查询全体学生与选课表的笛卡尔积。select * from student cross join course 23、 查询每个学生及其选修课程的情况。select distinct * from student cross join cs where student.sno=cs.sno24、 查询每个学生及其选修课程的情况(去掉重复属性)select a.sno,sname,sex,dept,age,o,cname,score,c.gradefrom student a,
10、course b,cs c where a.sno=c.sno and o=o25、 查询某门课程考试成绩相同的学生学号和课程信息select a.sno,o,ame,b.score from cs a,course bwhere o=o and (select count(*) from cs where cno=cno and grade=grade)=226、 查询每个学生的选修课程包括没有选修课程的学生(外连接)select * from student a,cs b where a.sno*=b.sno27、 查询每个学生的选修课程包括没有被学生选修的课程(外连接)select *
11、from student ,cs where student.sno =*cs.sno28、 查询每个学生的选修课程即包括没有被学生选修的课程又包括没有被学生选修的课程(全连接)select * from student full join cs on student.sno=cs.sno29、 查询选修2号课程且成绩在90分以上的所有学生的学号、姓名select sno,sname from studentwhere sno in(select sno from cs where grade=90and cno=C002)30、 查询每个学生的学号、姓名、选修的课程名及成绩select st
12、udent.sno,sname,cname,grade from student,course,cswhere (student.sno=cs.sno) and (o=o)31、 查询与“张三”在一个系学习的学生(IN)select * from studentwhere dept in(select dept from student where sname=张三)32、 查询选修了课程名为“信息系统”的学生学号和姓名。select sno,sname from studentwhere sno in (select sno from cs where cno in(select cno f
13、rom course where cname=信息系统)33、 查询与“张三”在同一个系学习的学生select * from studentwhere dept in (select dept from student where sname=张三)34、 查询选修了课程1或者选修了课程2的学生(要求消除重复组UNION)(select sno from cs where cno=C001 ) UNION(select sno from cs where cno=C002)35、 查询选修了课程1或者选修了课程2的学生(要求不消除重复组UNION ALL)(select sno from cs where cno=C001 ) UNION all(select sno from cs where cno=C002)五、实验总结 通过本次试验,掌握了使用SQL语句查询的技巧。
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1