use StudentDB ;/**********************实验四 数据查询实验******************************/-- 1. 查询学生的基本信息;select *from Student;-- 2. 查询“CS”系学生的基本信息;select *from Student where Sdept='CS';-- 3. 查询“CS”系学生年龄不在19到21之间的学生的学号、姓名;select Sno,Sname from Studentwhere Sdept='CS' and Sage not between 19 and 21 ;-- 4. 找出最大年龄;select max(Sage) 最大年龄 from Student ;-- 5. 找出“CS”系年龄最大的学生,显示其学号、姓名;select Sno,Sname from Studentwhere Sdept='CS' and Sage= ( select max(Sage) from Student where sdept='CS');-- 6. 找出各系年龄最大的学生,显示其学号、姓名;************-- 本题用到了相关子查询,比较的难,需要多加练习update student set sage=23 where sno='200215125' ;select *from student;select sno,sname,sdeptfrom student awhere sage = ( select max(sage) from student b where a.sdept=b.sdept );-- 7. 统计“CS”系学生的人数;select count(*) 计算机系人数 from Studentwhere Sdept='CS';-- 8. 统计各系学生的人数,结果按升序排列;select Sdept,count(*) cntfrom SC,Studentwhere Student.Sno=SC.Sno group by Sdeptorder by cnt;-- 9. 按系统计各系学生的平均年龄,结果按降序排列;select Sdept,avg(Sage) agefrom SC,Studentwhere Student.Sno=SC.Snogroup by Sdeptorder by age desc;-- 10. 查询每门课程的课程名;select Cname from Course;-- 11. 查询无先修课的课程的课程名和学分数;select Cname,Ccreditfrom Coursewhere Cpno is null;-- 12. 统计无先修课的课程的学分总数;select sum(Ccredit) from Coursewhere Cpno is null ;-- 13. 统计每位学生选修课程的门数、学分及其平均成绩;select SC.sno,count(SC.Cno),sum(Ccredit),avg(Grade)from SC,Coursewhere SC.cno=Course.cnogroup by Sno;-- 14. 统计选修每门课程的学生人数及各门课程的平均成绩;select Cno,count(Cno),avg(Grade)from SCgroup by Cno;-- 15. 找出平均成绩在85分以上的学生,结果按系分组,并按平均成绩的升序排列;select Student.Sno,avg(grade) avg_grade,Sdeptfrom SC,Studentwhere SC.Sno=Student.Snogroup by Student.Sno,Sdepthaving avg(grade)>85order by avg_grade;select Student.Sno,avg(grade) avg_grade,Sdeptfrom SC join Studentusing(sno)group by Student.Sno,Sdepthaving avg_grade>85order by avg(grade) ; -- 注意在order by 后面可以用聚集函数-- 16. 查询选修了“1”或“2”号课程的学生学号和姓名;*********-- 方法一select distinct Student.Sno,Snamefrom Student,SCwhere SC.Sno=Student.Sno and (Cno='1' or Cno='2' );-- 方法二select distinct Student.Sno,Snamefrom Student,SCwhere SC.Sno=Student.Sno and Cno in('1','2');-- 方法三select distinct Student.Sno,Snamefrom Student,SCwhere SC.Sno=Student.Sno and Cno='1'unionselect distinct Student.Sno,Snamefrom Student,SCwhere SC.Sno=Student.Sno and Cno='2' ;-- 17. 查询选修了“1”和“2”号课程的学生学号和姓名;*********select distinct Student.Sno,Snamefrom Student,SCwhere SC.Sno=Student.Sno and Cno='1' and Student.Sno in (select distinct Sno from SC where Cno='2' );/*-- intersect 在MySQL中没有select distinct Student.Sno,Snamefrom Student,SCwhere SC.Sno=Student.Sno and Cno='1'intersectselect distinct Student.Sno,Snamefrom Student,SCwhere SC.Sno=Student.Sno and Cno='2' ;*/-- 18. 查询选修了课程名为“数据库”且成绩在60分以下的学生的学号、姓名和成绩;insert into SC values('200215125','1',58);select Student.Sno,Sname,Gradefrom SC,Course,Studentwhere SC.Sno=Student.Sno and SC.Cno=Course.Cno and Cname='数据库' and grade<60 ;delete from SC where Sno='200215125' and Cno='1' ;-- 19. 查询每位学生选修了课程的学生信息(显示:学号,姓名,课程号,课程名,成绩);select Student.Sno,Sname,Course.Cno,Cname,gradefrom Student,Course,SCwhere Student.Sno=SC.Sno and Course.Cno=SC.Cno;select sno,sname,cno,cname,gradefrom student join sc using(sno)join course using(cno);-- 20. 查询没有选修课程的学生的基本信息;*************-- 方法一select Student.* from Studentwhere Sno not in ( select Sno from SC );-- 方法二select Student.* from studentwhere not exists ( select * from SC where sno=Student.sno );-- 21. 查询选修了3门以上课程的学生学号;select Sno from SCgroup by Snohaving count(Sno)> 2;-- 22. 查询选修课程成绩至少有一门在85分以上的学生学号;select Sno from SCgroup by Snohaving max(Grade)>85 ;-- 23. 查询选修课程成绩均在85分以上的学生学号;select Sno from SCgroup by Snohaving min(Grade)>85 ;-- 24. 查询选修课程平均成绩在85分以上的学生学号;select Sno from SCgroup by Snohaving avg(Grade)>85 ;