数据查询
在表S、C、SC上完成以下查询:
1、查询学生的基本信息;
select * from S;
2、查询“CS”系学生的基本信息;
select * from S where Sdept ='CS';
3、查询“CS”系学生年龄不在19到21之间的学生的学号、姓名;
select Sno,Sname from S where Sdept ='CS' and Sage>19 and Sage<21;
4、找出最大年龄;
select MAX (Sage)from S;
5、找出“CS”系年龄最大的学生,显示其学号、姓名;
select Sno, Sname from S where Sdept ='CS'and Sage in (select MAX (Sage)from S where Sdept ='CS');
6、查询所有姓王的学生的学号、姓名、所在系;
select Sno ,Sname,Sdept from S where Sname like '王%';
7、统计“CS”系学生的人数;
select COUNT (*) from S where Sdept ='CS';
8、统计各系学生的人数,结果按升序排列;
select Sdept ,COUNT (Sno) from S group by Sdept order by COUNT (Sno)asc;
9、按系统计各系学生的平均年龄,结果按降序排列;
select Sdept, AVG (Sage) from S group by Sdept order by AVG(Sage)desc;
10、查询每门课程的课程名;
select Cname from C;
11、统计无先修课的课程的学分总数;
select SUM (Ccridit) from C where Cpno is NULL;
12、查询选修了“1”或“2”号课程的学生学号和姓名;
select S.Sno,S.Sname from S where Sno in(select Sno from SC where Cno='1'or Cno='2');
13、查询选修了“1”和“2”号课程的学生学号和姓名;
select S.Sno,S.Sname from S where Sno in(select Sno from SC where Cno='1'and Sno in(select Sno from SC where Cno ='2'));
14、查询选修了课程名为“数据库系统”且成绩在60分以下的学生的学号、姓名和成绩;
select S.Sno ,Sname,Grade from SC ,S where Cno in(select C.Cno from C where Cname ='数据库系统'and SC.Cno in (select SC .Cno from SC where Grade <60))and S.Sno =SC .Sno;
15、查询每位选修了课程的学生信息(显示:学号,姓名,课程号,课程名,成绩);
select S.Sno ,S.Sname ,C.Cno ,C.Cname ,SC.Grade from S,C,SC where S.Sno =SC.Sno and C.Cno =SC.Cno ;
16、查询每位学生的信息(显示:学号,姓名,所在系,课程号,课程名,成绩。包括没选课的学生);
select S.Sno ,S.Sname ,S.Sdept ,C.Cno ,C.Cname ,SC.Grade from S,C,SC;
select S.Sno ,Sname ,Sdept ,SC.Cno,C.Cname,Grade
from S LEFT OUTER JOIN SC on (S.Sno =SC.Sno) LEFT OUTER JOIN C on (C.Cno=SC.Cno);
17、查询没有选修课程的学生的基本信息;
select * from S where Sno not in(select SC.Sno from SC );
18、查询各系年龄最大的学生,显示其学号、姓名;
select S.Sno ,S.Sname from S where Sage in (select MAX(Sage) from S group by Sdept);
19、查询选修了3门以上课程的学生学号;
select Sno from SC group by Sno having COUNT (*)>3;
20、查询选修课程成绩至少有一门在80分以上的学生学号;
select Sno from SC where Sno>=1(select sno from SC where Grade >=80);
21、查询选修课程成绩均在80分以上的学生学号;
select Sno from SC where Grade>80;
22、查询选修课程平均成绩在80分以上的学生学号;
select sno from SC group by Sno having AVG(grade)>80;
23、统计每位学生选修课程的门数、学分及其平均成绩;
select Sno,COUNT(Sno),sum(Ccridit),AVG(grade) from C,SC group by Sno;
24、统计选修每门课程的学生人数及各门课程的平均成绩。
select cno,COUNT(Sno),AVG(grade)from SC group by Cno;
25、找出平均成绩在85分以上的学生,结果按系分组,并按平均成绩的升序排列。
select S.Sdept ,AVG(grade)from S,SC,(select sno ,AVG(grade)from SC group by Sno)as avg_sc(avg_sno,avg_grade)
where avg_sc.avg_grade >85 group by S.Sdept order by AVG(Grade)desc;