数据库例题二

select * From student;
select * From course ;
select * From score;
select * From teacher ;
select * From grade ;
-- 查询Student表中的所有记录的Sname、Ssex和Class列。
Select t.sname,t.ssex,t.class From student t;

-- 查询教师所有的单位,即不重复的Depart列。
Select Distinct t.depart From teacher t
-- 查询Student表的所有记录。
Select * From student
-- 查询Score表中成绩在60到80之间的所有记录。
Select * From score t Where t.degree Between 60 And 80;
-- 查询Score表中成绩为85,86或88的记录。
Select * From score t Where t.degree In(85,86,88)
-- 查询Student表中“95031”班或性别为“女”的同学记录。
Select * From student t Where t.class='95031' Or t.ssex='女'
-- 以Class降序查询Student表的所有记录。
Select * From student t Order By t.class Asc
-- 以Cno升序、Degree降序查询Score表的所有记录。
Select * From score s Order By s.cno Asc, s.degree Desc
-- 查询“95031”班的学生人数。
Select * Select t.class,count(t.sname)cou From student t Where t.class=95031 Group By t.class
-- 查询Score表中的最高分的学生学号和课程号。
Select e.sno,e.cno From score e Where e.degree=(Select Max(t.degree) From score t);
-- 查询‘3-105’号课程的平均分。
Select t.cno,Avg(t.degree)From score t Where t.cno='3-105' Group By t.cno
-- 查询Score表中至少有5名学生选修的并以3开头的课程的平均分数。
--13 查询最低分大于70,最高分小于90的Sno列。
Select * From score t Where t.degree Between 70 And 90

-- 查询所有学生的Sname、Cno和Degree列。
Select t.sname,t.sno,e.degree From Student t,score e Where t.sno=e.sno
-- 查询所有学生的Sno、Cname和Degree列。
Select e1.sno,e1.degree,e2.cname From score e1 Left Join course e2 On e1.cno=e2.cno
-- 查询所有学生的Sname、Cname和Degree列。
With rr As(Select e1.sno,e1.cno,e1.degree,e2.cname From score e1 Left Join course e2 On e1.cno=e2.cno)
Select t1.sname,t2.cname,t2.degree From student t1 Left Join rr t2 On t1.sno=t2.sno
-- 查询“95033”班所选课程的平均分。
-- 假设使用如下命令建立了一个grade表:

--现查询所有同学的Sno、Cno和rank列。
Select e.cno,t.sname,t.sno,e.degree From Student t,score e Where t.sno=e.sno

--19、查询选修“3-105”课程的成绩高于“109”号同学成绩的所有同学的记录。
Select * From score e Where e.cno='3-105' And
e.degree>(Select t.degree From score t Where t.cno='3-105' And t.sno=109)

--20、查询score中选学一门以上课程的同学中分数为非最高分成绩的记录。
With rr As (Select t.cno,Count(t.sno)Count From score t Group By t.cno)
Select * From score p,rr Where rr.count>1 And p.degree !=(Select Max(e.degree) From score e)

--21、查询成绩高于学号为“109”、课程号为“3-105”的成绩的所有记录。
--22、查询和学号为108的同学同年出生的所有学生的Sno、Sname和Sbirthday列。
Select e.sno,e.sname,e.sbirthday From student e Where e.sbirthday=to_date('1977','yyyy')
--23、查询“张旭“教师任课的学生成绩。
Select t.tno,t.tname,e.cno,e.degree,p.cno,p.tno From teacher t,course p,score e Where
t.tname='张旭' And t.tno=p.tno And p.cno=e.cno
--24、查询选修某课程的同学人数多于5人的教师姓名。
With rr As (Select t.tname,e.sno,e.cno,t.tno,p.cno From teacher t,course p,score e Where t.tno=p.tno And p.cno=e.cno)
Select rr.tname,Count(rr.sno) Count From rr Group By rr.tname Having Count(*)>5
--25、查询95033班和95031班全体学生的记录。
Select * From student t,score e,course p,teacher s Where
t.sno=e.sno And e.cno=p.cno And p.tno=s.tno
--26、查询存在有85分以上成绩的课程Cno.
Select e.cno From score e Where e.degree>85
--27、查询出“计算机系“教师所教课程的成绩表。
Select * From score e,teacher t,course p Where t.tno=p.tno And p.cno=e.cno And t.depart='计算机系'

--28、查询“计算机系”与“电子工程系“不同职称的教师的Tname和Prof。
Select t.tname,t.prof,t.depart From teacher t Order By t.depart

--29、查询选修编号为“3-105“且成绩至少高于选修编号为“3-245”的同学的Cno、Sno和Degree,并按Degree从高到低次序排序。
Select * From score e Where e.degree>(Select Max(t.degree) From score t Where t.cno='3-245')And e.cno='3-105' Order By e.degree

--30、查询选修编号为“3-105”且成绩高于选修编号为“3-245”的同学的Cno、Sno和Degree.
Select * From score e Where e.degree>(Select Max(t.degree) From score t Where t.cno='3-245')And e.cno='3-105'
--31、查询所有教师和同学的name、sex和birthday.
Select e.sname,e.ssex,e.sbirthday,t.tname,t.tsex,t.tbirthday From student e,teacher t
--32、查询所有“女”教师和“女”同学的name、sex和birthday.
Select t.tname,t.tsex,t.tbirthday From teacher t Where t.tsex='女'
Union All
Select e.sname,e.ssex,e.sbirthday From student e Where e.ssex='女'
--33、查询成绩比该课程平均成绩低的同学的成绩表。
With rr As(select t.cno,Avg(t.degree)Avg From score t group By t.cno)
Select e.degree,e.sno,e.cno,r.avg From score e,rr r Where e.cno=r.cno And e.degree<r.avg

--34、查询所有任课教师的Tname和Depart.
Select t.tno,t.tname,t.depart From teacher t Join course e On t.tno=e.tno
--35、查询所有未讲课的教师的Tname和Depart.
Select s.tname,s.depart From teacher s Where tno Not In
(Select t.tno From teacher t Join course e On t.tno=e.tno)
--36、查询至少有2名男生的班号。
Select t.class,Count(t.sno)cou From student t Where t.ssex='男' Group By t.class
--37、查询Student表中不姓“王”的同学记录。
Select * From student t Where t.sname Not Like '王%'
--38、查询Student表中每个学生的姓名和年龄。
Select t.sname,Trunc((Sysdate-t.sdirthday)/365)||'岁'age From student t
--39、查询Student表中最大和最小的Sbirthday日期值。
Select Max(t.Sbirthday),Min(t.Sbirthday) From student t
--40、以班号和年龄从大到小的顺序查询Student表中的全部记录。
Select * From student t Order By t.class Desc,t.sbirthday Desc
--41、查询“男”教师及其所上的课程。
Select * From teacher t,course e Where e.tno=t.tno And t.tsex='男'
--42、查询最高分同学的Sno、Cno和Degree列。
Select * From score t Where t.degree=(Select Max(e.degree) From score e)
--43、查询和“李军”同性别的所有同学的Sname.
Select t.sname From student t Where t.ssex=(Select e.ssex From student e Where e.sname='李军')
--44、查询和“李军”同性别并同班的同学Sname.
Select t.sname From student t Where t.ssex=(Select e.ssex From student e Where e.sname='李军') And t.class=(Select s.class From student s Where s.sname='李军')
--45、查询所有选修“计算机导论”课程的“男”同学的成绩表
Select * From student t,course e,score s Where s.cno=e.cno And s.sno=t.sno And e.cname='计算机导论' And t.ssex='男'

Commit;

posted @ 2017-08-14 11:20  挽你何用  阅读(215)  评论(0编辑  收藏  举报