赞
踩
目录
5、查询选修了3号课程的学生的学号和成绩,其结果按分数的降序排列。
6、查询全体学生的情况,查询结果按所在系升序排列,对同一系中的学生按年龄降序排列。
16、求基本表S中男同学的每一年龄组(超过50人)有多少人?要求查询结果按人数升序排列,人数相同按年龄降序排列。
学生关系S(Sno,Sname, Ssex,Sage,Sdept);
学习关系SC(Sno,Cno,Grade);
课程关系C(Cno,Cname,Cdept,Tname)
- select Sname,Sdept,Sage
- from S
- where Sage between 20 and 23
- select Sname,Ssex
- From S
- Where Sdept not in('CS','MA','IS')
- select Sname,Sno,Sex
- from S
- where Sname like "刘%"
- select Sno,Cno
- from SC
- where Grade is null
- select Sno,Grade
- from SC
- where Cno='3'
- order by Grade desc
- select *
- from S
- order by Sdept asc,Sage desc
- select count(*)
- from S
- select Sno
- from SC
- where Cno='2' or Cno='4'
- select Sno
- from SC
- where Cno='C2' and Sno in
- (select Sno
- from SC
- where Cno='C4')
- select Sno,Sage
- from S
- where Sno not in
- (select Sno from SC where Cno='C2')
- //Exists子句只返回true值,Not Exists只返回false值。
-
- select Sname
- from S
- where not exists
- (select * from C
- where not exists
- (select * from SC
- where S.Sno=SC.Sno and SC.Cno=C.Cno))
-
- //查找学习了所有课程的学生,即是在C中不存在一门课程没有学。
- select Sno
- from SC
- where Cno in
- (select Cno
- from SC
- where Sno='S3')
- select avg(Grade)
- from SC
- where Cno='C1'
- select Cno,count(Sno)
- from Sc
- group by Cno
- select Sno
- from S,SC
- where Sdept='CS' and S.Sno=SC.Sno
- group by Sno
- having count(*)>3
- select Sage,count(Sno)
- from S
- group by age
- having count(*)>50
- order by count(*) asc,Sage desc
- select Sno
- from SC
- where Cno='C2' and Grade>90
- select Sage,count(distinct S.Sno)
- from S,SC
- where S.Sno=SC.Sno
- group by age
- //嵌套查询
-
- select Sno,Sname
- from S
- where Sno in
- (select Sno
- from SC
- where Cno in
- (select Cno
- from C
- where Cname="数据库"));
-
-
- //连接查询
-
- select Sno,Sname
- from S join SC on S.Sno=SC.Sno
- join C on SC.Cno=C.Cno and C.Cname="数据库";
-
- select Sno,Sname
- from S,SC,C
- where S.Sno=SC.Sno and SC.Cno=C.Cno and C.Cname="数据库";

Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。