实验三 SQL查询语句

  • 格式:doc
  • 大小:40.00 KB
  • 文档页数:6

实验三 SQL查询语句一、实验目的:熟练掌握对基本表的查询,为后继学习作准备。

二、实验属性(验证性)1、了解并掌握SQL查询分析器及企业管理器的使用;2、掌握基本表的定义、删除与修改。

三、实验仪器设备及器材1.安装有windows操作系统计算机。

2.安装有Oracle11g和SQL Server的计算机。

3.安装有Visual Studio .net和Java编译器(eclipse、Netbean等)的编译器。

4.计算机具备网络环境。

四、实验要求(预习、实验前、实验中、实验后等要求)1.预习教材第三章,熟悉SQL语句。

2.熟悉.net、Java、PowerBuilder和Delphi 开发环境。

3.能够熟练掌握.net、Java、PowerBuilder和Delphi环境下的数据库的编程。

4.掌握单表查询语句的一般格式。

;5.掌握单表无条件、有条件查询及查询结果排序与分组。

6.掌握多表连接、嵌套和集合查询语句的一般格式及各种使用方法。

五、实验原理SQL语言应用。

六、实验步骤:(1)启动Oracle的SQL Developer或者SQL Plus,或者SQL Server 查询分析器;(2)对于Oracle11g的SQL Plus需要进行登录,对于Oracle11g的SQL Developer需要进行建立连接。

如果选择SQL SERVER查询分析器,需要选择数据库;1、查询李大鹏同学所学课程门数及平均成绩(该题为例8做准备)。

select count(*),avg(grade)from system.student,system.scwhere student.sno=sc.snoand sname='李大鹏'group by sname;2、查询平均成绩大于李大鹏同学平均成绩的所有学生的学号、姓名和平均成绩。

select student.sno,sname,avg(grade)from system.sc,system.studentwhere student.sno=sc.snogroup by student.sno,snamehaving avg(grade)>( select avg(grade)from system.sc,system.studentwhere student.sno=sc.snoand sname='李大鹏');3、查询选修课程门数和平均成绩都大于李大鹏同学所选课程门数和平均成绩的所有的学生姓名。

select student.snamefrom system.sc,system.studentwhere student.sno=sc.snogroup by snamehaving avg(grade)>(select avg(grade)from system.scwhere sno=(select snofrom system.studentwhere sname='李大鹏'))and count(*)>(select count(*)from system.scwhere sno=(select snofrom system.studentwhere sname='李大鹏'));4、查询数据结构课程的成绩大于全班平均成绩的学生姓名。

select snamefrom system.student,system.sc,system.coursewhere sc.sno=student.sno and o=oand cname='数据结构'and grade>( select avg(grade)from system.course,system.scwhere o=o and cname='数据结构'group by cname);5、查询数据结构课程的成绩大于全班平均成绩的学生姓名、本人的数据结构成绩及全班的平均成绩。

select Sname as 姓名,grade as 成绩,temp.ag as 平均成绩from system.student,system.sc,system.course,(select AVG(grade) as agfrom system.sc,system.coursewhere o=o and Cname='数据结构'group by Cname)tempwhere student.Sno=sc.Sno and o=oand Cname='数据结构' and grade > temp.ag;6、查询所有选修课程的成绩大于全班平均成绩的学生姓名、课程名、本课程成绩。

select distinct sname,grade,cnamefrom system.student,system.course,system.sc,(select avg(grade) as agfrom system.sc,system.coursewhere o=ogroup by cname)tempwhere (student.sno=sc.sno)and o=oand (grade>temp.ag)order by sname;7、查询计科系年龄最大的学生姓名。

select snamefrom system.studentwhere sage =(select max(sage)from system.studentwhere sdept='计科系')and sdept='计科系';8、查询选修了计算机网络课程的学生姓名及该课程的成绩,要求分别使用外连接、左外连接、右外连接、内连接、嵌套查询、复合条件查询实现。

内连接:select Sname as 姓名,grade as 成绩from system.student,system.sc,system.coursewhere student.Sno=sc.Sno and o=oand Cname='计算机网络';嵌套查询:select Sname as 姓名,grade as 成绩from system.student,system.scwhere student.Sno=sc.Sno and Cno in(select ofrom system.sc,system.coursewhere o=o and Cname='计算机网络' );9、查询选修了计算机网络课程这些学生的选课门数及平均成绩。

select count(*),avg(grade)from system.scwhere sno in(select snofrom system.sc,system.coursewhere o=oand cname='计算机网络')group by sno;10、查询选修了计算机网络课程且成绩高于该课程平均成绩的学生姓名及成绩。

select sname,gradefrom system.student,system.sc,system.course,( select avg(grade) as agfrom system.sc,system.coursewhere o=o and cname='计算机网络')tempwhere sc.sno=student.sno and o=oand cname='计算机网络' and grade>temp.ag;11、查询选修了计算机网络课程且成绩高于该课程平均成绩的学生姓名、计算机网络课程成绩、计算机网络课程平均成绩。

select sname as 姓名,grade as 成绩,temp.ag as 平均成绩from system.student,system.sc,system.course,( select avg(grade) as agfrom system.sc,system.coursewhere o=o and cname='计算机网络')tempwhere sc.sno=student.sno and o=oand cname='计算机网络' and grade>temp.ag;12、查询没有选修了计算机网络课程的学生的学号及姓名。

select sno,snamefrom system.studentwhere sno not in( select snofrom system.sc,system.coursewhere o=o and cname='计算机网络');13、查询选修了全部课程的学生的学号及姓名。

select sno,snamefrom system.studentwhere exists( select*from system.coursewhere exists( select*from system.scwhere sno=student.sno and cno=o));14、查询平均成绩大于学号'200400812140'学生平均成绩的所有学生的姓名select Sname as 姓名from system.student,system.scwhere student.Sno=sc.Snogroup by Snamehaving AVG(grade)>(select AVG(grade)from system.scwhere Sno='200400812140');15、查询每一位同学的平均成绩及选修课程的门数。

select avg(grade) as 平均成绩,count(*) as 选课门数from system.scgroup by snoorder by 平均成绩;。