Oracle sql练习题.docx
《Oracle sql练习题.docx》由会员分享,可在线阅读,更多相关《Oracle sql练习题.docx(16页珍藏版)》请在冰豆网上搜索。
Oraclesql练习题
createtablestudent(
snovarchar2(10)primarykey,
snamevarchar2(20),
sagenumber
(2),
ssexvarchar2(5)
);
createtableteacher(
tnovarchar2(10)primarykey,
tnamevarchar2(20)
);
createtablecourse(
cnovarchar2(10),
cnamevarchar2(20),
tnovarchar2(20),
constraintpk_courseprimarykey(cno,tno)
);
createtablesc(
snovarchar2(10),
cnovarchar2(10),
scorenumber(4,2),
constraintpk_scprimarykey(sno,cno)
);
/*******初始化学生表的数据******/
insertintostudentvalues('s001','张三',23,'男');
insertintostudentvalues('s002','李四',23,'男');
insertintostudentvalues('s003','吴鹏',25,'男');
insertintostudentvalues('s004','琴沁',20,'女');
insertintostudentvalues('s005','王丽',20,'女');
insertintostudentvalues('s006','李波',21,'男');
insertintostudentvalues('s007','刘玉',21,'男');
insertintostudentvalues('s008','萧蓉',21,'女');
insertintostudentvalues('s009','陈萧晓',23,'女');
insertintostudentvalues('s010','陈美',22,'女');
commit;
/******************初始化教师表***********************/
insertintoteachervalues('t001','刘阳');
insertintoteachervalues('t002','谌燕');
insertintoteachervalues('t003','胡明星');
commit;
/***************初始化课程表****************************/
insertintocoursevalues('c001','J2SE','t002');
insertintocoursevalues('c002','JavaWeb','t002');
insertintocoursevalues('c003','SSH','t001');
insertintocoursevalues('c004','Oracle','t001');
insertintocoursevalues('c005','SQLSERVER2005','t003');
insertintocoursevalues('c006','C#','t003');
insertintocoursevalues('c007','JavaScript','t002');
insertintocoursevalues('c008','DIV+CSS','t001');
insertintocoursevalues('c009','PHP','t003');
insertintocoursevalues('c010','EJB3.0','t002');
commit;
/***************初始化成绩表***********************/
insertintoscvalues('s001','c001',78.9);
insertintoscvalues('s002','c001',80.9);
insertintoscvalues('s003','c001',81.9);
insertintoscvalues('s004','c001',60.9);
insertintoscvalues('s001','c002',82.9);
insertintoscvalues('s002','c002',72.9);
insertintoscvalues('s003','c002',81.9);
insertintoscvalues('s001','c003','59');
commit;
练习:
注意:
以下练习中的数据是根据初始化到数据库中的数据来写的SQL语句,请大家务必注意。
1、查询“c001”课程比“c002”课程成绩高的所有学生的学号;
2、查询平均成绩大于60分的同学的学号和平均成绩;
selectsno,avg(score)
fromsc
groupbysno
havingavg(score)>60
3、查询所有同学的学号、姓名、选课数、总成绩;
selectstudent.sno,sname,count(cno),sum(score)
fromstudent,sc
wherestudent.sno=sc.sno
groupbystudent.sno,sname;
4、查询姓“刘”的老师的个数;
selectcount(tno)
fromteacher
wheretnamelike'刘%';
5、查询没学过“谌燕”老师课的同学的学号、姓名;
select*fromstudentstwherest.snonotin
(selectdistinctsnofromscsjoincoursecono=o
jointeachertonc.tno=t.tnowheretname='谌燕')
6、查询学过“c001”并且也学过编号“c002”课程的同学的学号、姓名;
selectdistinctst.sno,sname
fromstudentstjoinscconst.sno=c.sno
wherecno='c001'orcno='c002';
7、查询学过“谌燕”老师所教的所有课的同学的学号、姓名;
8、查询课程编号“c002”的成绩比课程编号“c001”课程低的所有同学的学号、姓名;
9、查询所有课程成绩小于60分的同学的学号、姓名;
10、查询没有学全所有课的同学的学号、姓名;
11、查询至少有一门课与学号为“s001”的同学所学相同的同学的学号和姓名;
12、查询至少学过学号为“s001”同学所有一门课的其他同学学号和姓名;
13、把“SC”表中“谌燕”老师教的课的成绩都更改为此课程的平均成绩;
14、查询和“s001”号的同学学习的课程完全相同的其他同学学号和姓名;
15、删除学习“谌燕”老师课的SC表记录;
16、向SC表中插入一些记录,这些记录要求符合以下条件:
没有上过编号“c002”课程的同学学号、“c002”号课的平均成绩;
17、查询各科成绩最高和最低的分:
以如下形式显示:
课程ID,最高分,最低分
18、按各科平均成绩从低到高和及格率的百分数从高到低顺序
19、查询不同老师所教不同课程平均分从高到低显示
20、统计列印各科成绩,各分数段人数:
课程ID,课程名称,[100-85],[85-70],[70-60],[<60]
21、查询各科成绩前三名的记录:
(不考虑成绩并列情况)
22、查询每门课程被选修的学生数
23、查询出只选修了一门课程的全部学生的学号和姓名
24、查询男生、女生人数
25、查询姓“张”的学生名单
26、查询同名同性学生名单,并统计同名人数
27、1981年出生的学生名单(注:
Student表中Sage列的类型是number)
28、查询每门课程的平均成绩,结果按平均成绩升序排列,平均成绩相同时,按课程号降序排列
29、查询平均成绩大于85的所有学生的学号、姓名和平均成绩
30、查询课程名称为“数据库”,且分数低于60的学生姓名和分数
31、查询所有学生的选课情况;
32、查询任何一门课程成绩在70分以上的姓名、课程名称和分数;
33、查询不及格的课程,并按课程号从大到小排列
34、查询课程编号为c001且课程成绩在80分以上的学生的学号和姓名;
35、求选了课程的学生人数
36、查询选修“谌燕”老师所授课程的学生中,成绩最高的学生姓名及其成绩
37、查询各个课程及相应的选修人数
38、查询不同课程成绩相同的学生的学号、课程号、学生成绩
39、查询每门功课成绩最好的前两名
40、统计每门课程的学生选修人数(超过10人的课程才统计)。
要求输出课程号和选修人数,查询结果按人数降序排列,若人数相同,按课程号升序排列
41、检索至少选修两门课程的学生学号
42、查询全部学生都选修的课程的课程号和课程名
43、查询没学过“谌燕”老师讲授的任一门课程的学生姓名
44、查询两门以上不及格课程的同学的学号及其平均成绩
45、检索“c004”课程分数小于60,按分数降序排列的同学学号
46、删除“s002”同学的“c001”课程的成绩
答案:
1.
*********************************
selecta.*from
(select*fromscawhereo='c001')a,
(select*fromscbwhereo='c002')b
wherea.sno=b.snoanda.score>b.score;
*********************************
select*fromsca
whereo='c001'
andexists(select*fromscbwhereo='c002'anda.score>b.score
anda.sno=b.sno)
*********************************
2.
*********************************
selectsno,avg(score)fromscgroupbysnohavingavg(score)>60;
*********************************
3.
*********************************
selecta.*,s.snamefrom(selectsno,sum(score),count(cno)fromscgroupbysno)a,studentswherea.sno=s.sno
*********************************
4.
*********************************
selectcount(*)fromteacherwheretnamelike'刘%';
*********************************
5.
*********************************
selecta.sno,a.snamefromstudenta
wherea.sno
notin
(selectdistincts.sno
fromscs,
(selectc.*
fromcoursec,
(selecttno
fromteachert
wheretname='谌燕')t
wherec.tno=t.tno)b
whereo=o)
*********************************
select*fromstudentstwherest.snonotin
(selectdistinctsnofromscsjoincoursecono=o
jointeachertonc.tno=t.tnowheretname='谌燕')
*********************************
6.
*********************************
selectst.*fromsca
joinscbona.sno=b.sno
joinstudentst
onst.sno=a.sno
whereo='c001'ando='c002'andst.sno=a.sno;
*********************************
7.
*********************************
selectst.*fromstudentstjoinscsonst.sno=s.sno
joincoursecono=o
jointeachertonc.tno=t.tno
wheret.tname='谌燕'
*********************************
8.
*********************************
select*fromstudentst
joinscaonst.sno=a.sno
joinscbonst.sno=b.sno
whereo='c002'ando='c001'anda.score*********************************
9.
*********************************
selectst.*,s.scorefromstudentst
joinscsonst.sno=s.sno
joincoursecono=o
wheres.score<60
*********************************
10.
*********************************
selectstu.sno,stu.sname,count(o)fromstudentstu
leftjoinsconstu.sno=sc.sno
groupbystu.sno,stu.sname
havingcount(o)<(selectcount(distinctcno)fromcourse)
===================================
select*fromstudentwheresnoin
(selectsnofrom
(selectstu.sno,ofromstudentstu
crossjoincoursec
minus
selectsno,cnofromsc)
)
===================================
*********************************
11.
*********************************
selectst.*fromstudentst,
(selectdistincta.snofrom
(select*fromsc)a,
(select*fromscwheresc.sno='s001')b
whereo=o)h
wherest.sno=h.snoandst.sno<>'s001'
*********************************
12.
*********************************
select*fromsc
leftjoinstudentst
onst.sno=sc.sno
wheresc.sno<>'s001'
andoin
(selectcnofromsc
wheresno='s001')
*********************************
13.
*********************************
updatesccsetscore=(selectavg(c.score)fromcoursea,teacherb
wherea.tno=b.tno
andb.tname='谌燕'
ando=o
groupbyo)
wherecnoin(
selectcnofromcoursea,teacherb
wherea.tno=b.tno
andb.tname='谌燕')
*********************************
14.
*********************************
select*fromscwheresno<>'s001'
minus
(
select*fromsc
minus
select*fromscwheresno='s001'
)
*********************************
15.
*********************************
deletefromsc
whereoin
(
selectcnofromcoursec
leftjointeachertonc.tno=t.tno
wheret.tname='谌燕'
)
*********************************
16.
*********************************
insertintosc(sno,cno,score)
selectdistinctst.sno,o,(selectavg(score)fromscwherecno='c002')
fromstudentst,sc
wherenotexists
(select*fromscwherecno='c002'andsc.sno=st.sno)ando='c002';
*********************************
17.
*********************************
selectcno,max(score),min(score)fromscgroupbycno;
*********************************
18.
*********************************
selectcno,avg(score),sum(casewhenscore>=60then1else0end)/count(*)
as及格率
fromscgroupbycno
orderbyavg(score),及格率desc
*********************************
19.
*********************************
selectmax(t.tno),max(t.tname),max(o),max(ame),o,avg(score)fromsc,coursec,teachert
whereo=oandc.tno=t.tno
groupbyo
orderbyavg(score)desc
*********************************
20.
*********************************
selecto,ame,
sum(casewhenscorebetween85and100then1else0end)AS"[100-85]",
sum(casewhenscorebetween70and85then1else0end)AS"[85-70]",
sum(casewhenscorebetween60and70then1else0end)AS"[70-60]",
sum(casewhenscore<60then1else0end)AS"[<60]"
fromsc,coursec
whereo=o
groupbyo,ame;
*********************************
21.
*********************************
select*from
(selectsno,cno,score,row_number()over(partitionbycnoorderbyscoredesc)rnfromsc)
wherern<4
*********************************
22.
*********************************
selectcno,count(sno)fromscgroupbycno;
*********************************
23.
*********************************
selectsc.sno,st.sname,count(cno)fromstudentst
leftjoinsc
onsc.sno=st.sno
groupbyst.sname,sc.snohavingcount(cno)=1;
*********************************
24.
*********************************
selectssex,count(*)fromstudentgroupbyssex;
*********************************
25.