实验三 sql sever 数据查询操作

来源:互联网 发布:韩国淘宝女模特排行榜 编辑:程序博客网 时间:2024/05/02 04:47

实验目的:

1.掌握基本的查询、嵌套子查询及连接查询
2.学习数据库的部分保留字符的使用
3.学习部分统计函数的使用。

实验要求:

1.熟练掌握基本的查询、嵌套子查询及连接查询
2.体会各种查询的异同及相互之间的转换,体会各种查询的执行过程,为综合应用打下良好的基础。

实验内容:

1.投影查询:
查询Student表中所有记录的SName、Sex和Class列,并且改变查询结果中显示的列名(SName->姓名,Sex->性别,Class->班级)。
2.简单查询:
查询SC表中成绩在60到80之间的所有记录。
查询Student表中所有姓王的学生记录。
查询SC表中成绩为85、86、88或90的记录。
查询Student表中“95031”班或性别为“女”的学生记录。
查询SC表中,选修了课程但没有参加考试的学生学号。
3.对查询结果排序:
查询SC表中的所有记录,并且以Cno升序、Score降序显示。
4.使用集函数:
查询统计Student表中“95031”班学生人数。
查询统计SC表中,“3-105”号课程的平均分。
5.数据分组:
查询SC表中选修人数多于4人,并且课程号以3开头的课程的平均成绩。
查询至少有两门课程的成绩在80分以上学生的学号。
6.表的连接查询:
查询至少选修了两门以上课程的学生的学号、姓名和所在班级。
查询所有学生(包括未选课的学生)的学号、姓名(SName)、该学生所选课程的平均分。提示:用外连接
7.嵌套查询:(以下部分必须用嵌套查询来完成)
查询Student表中与“105”号学生在同一个班级的学生的SNo,SName和BirthDate。
查询修选课程名为“高等数学”的学生学号、姓名。
查询选修学生人数多于4人的某课程的任课教师姓名。
查询成绩低于该门课程的平均分的学生的学号。
查询所有任课教师的TName和DepID。
查询选修了所有课程的学生的学号,姓名,年龄
查询没有选修李诚老师所授课程的学生的学号、姓名、年龄
8.UNION操作:
查询所有教师和学生的编号、姓名、性别。

相关代码

use JiaoXuego--投影查询--询Student表中所有记录的SName、Sex和Class列,并且改变查询结果中显示的列名select SName 姓名,sex 性别,Class 班级 from student--简单查询:--查询SC表中成绩在6080之间的所有记录select * from sc  where Score between 60 and 80--询Student表中所有姓王的学生记录select * from student where SName like'王%'--询SC表中成绩为85868890的记录select * from sc where Score=85 or Score=86 or Score=88 or Score =90--或select sno,CNo  ,score from SC where score in(85,86,88,90)--查询Student表中“95031”班或性别为“女”的学生记录select * from student  where Class=95031 or Sex ='女'--或select * from student where class=95031unionselect * from student where sex='女'--查询SC表中,选修了课程但没有参加考试的学生学号select * from sc;select sno from sc where score is null--排序查询--查询SC表中的所有记录,并且以Cno升序、Score降序显示select sno 学生学号,CNo 课程编号 ,score 考试成绩 from SC order by CNo asc ,score desc--使用聚集函数--查询统计Student表中“95031”班学生人数select count( sno) as 总人数 from student where class=95031select count(distinct sno) as 总人数 from student where class=95031--查询统计SC表中,“3-105”号课程的平均分select AVG(score) as 平均成绩 from SC where CNo='3-105'--数据分组--查询SC表中选修人数多于4人,并且课程号以3开头的课程的平均成绩select CNo, AVG(score) 平均成绩 from SC where CNo like '3%' group by CNo having count(sno)>4--查询至少有两门课程的成绩在80分以上学生的学号select SNo from sc where Score>80 group by SNo having COUNT (*)>=2--表的连接--l 查询至少选修了两门课程的学生的学号、姓名和所在班级select distinct s.sno,sname,class     from (select sc1.sno from sc sc1,sc sc2 where sc1.sno=sc2.sno and sc1.cno<>sc2.cno) as scc,student s    where s.sno=scc.sno--或select SNo ,SName,class from student     where SNo in(select Sno from sc group by Sno  having COUNT (*)>=2)select * from scselect * from student--查询所有学生(包括未选课的学生)的学号、姓名(SName)、该学生所选课程的平均分select st.SNo 学号,SName 姓名,avgScore 平均分 from student st left  outer join  (select SNo,avg(Score)avgScore from sc group by SNo )scc  on scc.SNo=st. SNo--嵌套查询:--查询Student表中与“105”号学生在同一个班级的学生的SNo,SName和BirthDateselect SNo ,SName  ,BirthDate from student where Class in (select Class  from student  where SNo ='105')and SNo ! ='105'--查询修选课程名为“高等数学”的学生学号、姓名。insert into sc (SNo ,CNo)values (103,'9-888')select sno,sname from student swhere exists(select * from sc ,Course c where sc.SNo=s.SNo and sc.CNo=c.CNo and CName ='高等数学');--或select sno,sname from studentwhere SNo in( select SNo from sc ,Course  where sc.CNo=Course.CNo and CName='高等数学');--或select SNo ,SName from student  where SNo in(select SNo from sc where CNo in (select CNo from Course where CName ='高等数学') )select s.SNo ,SName ,CName from student s ,sc ,Course c where s.SNo=sc.SNo and sc.CNo=c.CNodelete from sc where SNo =103 and CNo ='9-888'--查询选修学生人数多于4人的某课程的任课教师姓名select TName from teacher where Tno in (select TNo from course where CNo in(select CNo from sc group by CNo having count(*)>4))--或     select tname from teacher t ,course c,(select sc.CNo from SC sc group by sc.CNo having count(sc.sno)>=4) sc2 where t.tno=c.tno and sc2.CNo=c.CNoselect TName 教师姓名,count(*)学生人数 from Teacher t     inner join Course c on c.Tno=t.TNo    inner join sc on sc.CNo=c.CNo    inner join student s on s.SNo=sc.SNo     group by TName--查询所有任课教师的TName和DepIDselect distinct tname,DepID from teacher t where t.tno in(select c.tno from course c where c.cno in(select sc.cno from sc))--查询成绩比该课程平均成绩低的学生成绩select sc1.sno ,t.sname,sc1.score,sc3.average     from SC sc1,student t,        (select sc2.CNo,avg(score) as average from SC sc2 group by sc2.CNo) sc3            where sc1.CNo=sc3.CNo and sc1.sno=t.sno and sc1.score<sc3.average select CName,average,SNo,SCore    from Course c,sc,    (select CNo,AVG(score) average from sc group by CNo) sc2    where c.CNo=sc2.CNo and sc.CNo =sc2.CNo order by CName --查询选修了所有课程的学生的学号,姓名,年龄select sno,sname,datediff(yy,birthdate,getdate()) as age from studentwhere not exists    (select * from course    where not exists        (select * from sc            where student.sno=sc.sno and course.cno=sc.cno))select SNo ,CNo from sc order by SNo--查询没有选修李诚老师所授课程的学生的学号、姓名、年龄select sno,sname,datediff(yy,birthdate,getdate()) as age from student swhere not exists    (select * from course c,teacher t,sc where sc.sno=s.sno and c.cno=sc.cno                and c.tno=t.tno and tname='李诚')--查询结果的并、交、差--查询所有教师和学生的编号、姓名、性别select sno,sname,sex  from studentunionselect tno,tname,sex from teacherselect SNo from sc where sc.CNo='3-105'union allselect SNo from sc where sc.CNo='3-245'select SNo from sc where sc.CNo='3-105'exceptselect SNo from sc where sc.CNo='3-245'select SNo from sc where sc.CNo='3-105'intersectselect SNo from sc where sc.CNo='3-245'
0 0