Oracle SQL题目及其解答(学生、课程、成绩、教师)

转载于:https://blog.csdn.net/tu451953337/article/details/45147757

题目来源于http://blog.csdn.net/lifetragedy/article/details/9935699,由于原文题目中的数据在我自己建的DB里没有数据,所以解答中的条件可能是符合我DB的条件,而不是符合题目的条件。但解答应该符合题目的意思要求。但是文中的解答肯能会有错误,但都是经过我DB中实际跑过的。欢迎指出错误。

select * from t_student 学生表


select * from t_course 课程表

select * from t_sc 学生成绩表



select * from t_teacher 老师表



  1. --http://blog.csdn.net/lifetragedy/article/details/9935699
  2. --create table t_student(stuid number(10,2) primary key,name varchar2(100), age number(3,0), sex char(1));
  3. --create table t_course(cid number(10,2) primary key, name varchar2(100), teacherid number(10, 2));
  4. --alter table t_course
  5. --add constraint tcourse_fk foreign key (teacherid) references t_teacher(tid);
  6. --create table t_sc(stuid number(10, 2), cid number(10,2), score number(10,2));
  7. --alter table t_sc
  8. --add constraint t_sc_pk primary key (stuid, cid);
  9. --alter table t_sc
  10. --add constraint t_sc_stuid_fk foreign key (stuid) references t_student(stuid);
  11. --alter table t_sc
  12. --add constraint t_sc_cid_fk foreign key (cid) references t_course(cid);
  13. --create table t_teacher(tid number(10,2) primary key, tname varchar2(100));
  14. /*
  15. select * from t_student for update;
  16. select * from t_course for update;
  17. select * from t_sc for update;
  18. select * from t_teacher for update;
  19. */
  20. --查询“1”课程比“2”课程成绩高的所有学生的学号
  21. select a.stuid
  22. from ( select * from t_sc s where s.cid = 1) a,
  23. ( select * from t_sc s where s.cid = 2) b
  24. where a.stuid = b.stuid
  25. and a.score > b.score;
  26. /*
  27. select a.S#
  28. from (select s#, score from SC where C# = '001') a,
  29. (select s#, score from SC where C# = '002')
  30. where a.score > b.score
  31. and a.s# = b.s#;
  32. */
  33. --2、查询平均成绩大于80分的同学的学号和平均成绩;
  34. select stuid, avg(score) from t_sc group by stuid having avg(score)> 80;
  35. --3、查询所有同学的学号、姓名、选课数、总成绩;
  36. select s.stuid, s.name, c.xks, c.zcj
  37. from t_student s left outer join
  38. ( select stuid, count(cid) xks, sum(score) zcj
  39. from t_sc
  40. group by stuid) c
  41. on s.stuid = c.stuid;
  42. select s.stuid, s.name, count(c.cid), sum(c.score)
  43. from t_student s
  44. left Outer join t_sc c
  45. on s.stuid = c.stuid
  46. group by s.stuid, s.name;
  47. --4、查询姓“李”的老师的个数;
  48. select count(tid) from t_teacher where tname like '李%';
  49. --5、查询没学过“叶平”老师课的同学的学号、姓名;
  50. select s.stuid,s.name from t_student s, t_course c, t_teacher t, t_sc sc where s.stuid=sc.stuid and c.cid=sc.cid and c.teacherid=t.tid and t.tname!= '叶平';
  51. select Student.S#, Student.Sname
  52. from Student
  53. where S# not in
  54. ( select distinct (SC.S#) from SC,
  55. Course,
  56. Teacher where SC.C# = Course.C# and Teacher.T# = Course.T# andTeacher.Tname = '叶平');
  57. --6、查询学过“001”并且也学过编号“002”课程的同学的学号、姓名;
  58. select s.stuid,s.name from t_student s, t_sc c where c.cid= 1 and s.stuid=c.stuid and exists ( select * from t_sc sc where sc.stuid=s.stuid and sc.cid= 2);
  59. -- select Student.S#,Student.Sname fromStudent,SC where Student.S#=SC.S# andSC.C#='001'and exists( Select * from SC as SC_2 where SC_2.S#=SC.S# and SC_2.C#='002');
  60. --7、查询学过“叶平”老师所教的所有课的同学的学号、姓名;
  61. select s.stuid,s.name from t_student s, t_sc sc where s.stuid = sc.stuid and sc.cid in(
  62. select c.cid from t_teacher t, t_course c where t.tid=c.teacherid and t.tname= 'Mr.kay');
  63. select S#, Sname
  64. from Student
  65. where S# in
  66. ( select S#
  67. from SC, Course, Teacher
  68. where SC.C# = Course.C# andTeacher.T# = Course.T#
  69. and Teacher.Tname = '叶平'
  70. group by S#
  71. having count(SC.C#) = ( select count(C#) from Course,
  72. Teacher where Teacher.T# = Course.T# and Tname = '叶平'));
  73. --8、查询课程编号“002”的成绩比课程编号“001”课程低的所有同学的学号、姓名;
  74. Select S#, Sname
  75. from ( select Student.S#,
  76. Student.Sname,
  77. score,
  78. ( select score
  79. from SC SC_2
  80. where SC_2.S# = Student.S# and SC_2.C# = '002') score2
  81. from Student, SC
  82. where Student.S# = SC.S# andC# = '001') S_2
  83. where score2 < score;
  84. --9、查询所有课程成绩小于60分的同学的学号、姓名;
  85. select s.stuid, s.name from t_student s where s.stuid not in ( select sc.stuid from t_student s, t_sc sc where sc.score>= 60 and s.stuid=sc.stuid);
  86. select s.stuid, s.name from t_student s where not exists ( select * from t_sc sc where sc.stuid=s.stuid and sc.score>= 60);
  87. --10、查询没有学全所有课的同学的学号、姓名;
  88. select s.stuid, s.name from t_student s,t_sc sc where s.stuid=sc.stuid group by s.stuid,s.name having count(sc.cid) != ( select count(cid) from t_course);
  89. select Student.S#, Student.Sname
  90. from Student, SC whereStudent.S# = SC.S#
  91. group by Student.S#, Student.Sname
  92. having count(C#) < ( select count(C#) from Course);
  93. --11、查询至少有一门课与学号为“1001”的同学所学相同的同学的学号和姓名;
  94. select distinct s.stuid,s.name from t_student s, t_sc sc where s.stuid=sc.stuid and s.stuid!= 1 and sc.cid in ( select cid from t_sc where stuid= 1)
  95. --select s.stuid,s.name from t_Student s, t_SC sc where s.stuid=SC.Stuid and sc.cid in (select Cid from t_SC where stuid='1');
  96. --13、把“t_SC”表中“叶平”老师教的课的成绩都更改为此课程的平均成绩;
  97. update t_sc sc1 set sc1.score =
  98. ( select avg(score) from t_sc group by cid having cid in ( select cid from t_teacher t, t_course c where t.tid=c.teacherid and t.tname= 'Mr.mao') and cid=sc1.cid);
  99. /*
  100. update t_sc sc_1
  101. set sc_1.score =
  102. (select avg(sc_2.score) from t_sc sc_2 where sc_2.cid = sc_1.cid)
  103. from t_course,
  104. t_teacher
  105. where t_course.Cid = t_sc.cid and t_course.tid = t_teacher.tid
  106. and t_teacher.tname = 'Mr.mao');
  107. */
  108. select * from t_sc sc, t_course c, t_teacher t where sc.cid=c.cid and c.teacherid=t.tid and t.tname= 'Mr.mao'
  109. --14、查询和“1002”号的同学学习的课程完全相同的其他同学学号和姓名;
  110. select * from t_sc where stuid in(
  111. select stuid
  112. from t_sc
  113. group by stuid
  114. having count(*) = ( select count(*) from t_sc where stuid = 6)
  115. )
  116. and cid in ( select cid from t_sc where stuid= 6);
  117. --15、删除学习“叶平”老师课的SC表记录
  118. delete t_sc where cid in(
  119. select cid from t_course c, t_teacher t where c.teacherid=t.tid and t.tname= 'Mr.mao');
  120. --16、向SC表中插入一些记录,这些记录要求符合以下条件:没有上过编号“5”课程的同学学号、2号课的平均成绩;
  121. insert into t_sc
  122. select stuid, '5' cid, ( select avg(score) from t_sc sc where sc.cid= 2) score
  123. from t_student s where not exists
  124. ( select * from t_sc sc where sc.cid= 5 and sc.stuid=s.stuid);
  125. --17、按平均成绩从高到低显示所有学生的“数据库”、“企业管理”、“英语”三门的课程成绩,按
  126. --如下形式显示: 学生ID,,数据库,企业管理,英语,有效课程数,有效平均分
  127. select t.stuid,
  128. max( case t.cname when 'Chinese' then t.score end) Chinese,
  129. max( case t.cname when 'Math' then t.score end) Math,
  130. max( case t.cname when 'English' then t.score end) English,
  131. round( avg(t.score), 2) avgscore,
  132. count(t.cid) kcs
  133. from ( select s.stuid, s.name stuname, sc.cid, sc.score, c.name cname
  134. from t_student s, t_sc sc, t_course c
  135. where s.stuid = sc.stuid
  136. and sc.cid = c.cid
  137. and c.name in ( 'Chinese', 'Math', 'English')) t
  138. group by t.stuid
  139. order by avgscore desc;
  140. /*
  141. select sc.stuid,
  142. (select score from t_sc where cid=1 and stuid=sc.stuid) Chinese,
  143. (select score from t_sc where cid=2 and stuid=sc.stuid) Math,
  144. (select score from t_sc where cid=3 and stuid=sc.stuid) English,
  145. round(avg(sc.score),2) avgscore,
  146. count(c.cid) kcs
  147. from t_sc sc, t_course c where sc.cid=c.cid and c.name in ('Chinese', 'Math', 'English')
  148. group by sc.stuid
  149. order by avgscore desc;
  150. */
  151. --18、查询各科成绩最高和最低的分:以如下形式显示:课程ID,最高分,最低分
  152. select cid, max(score) maxscore, min(score) minscore from t_sc group by cid order by cid;
  153. /*
  154. SELECT L.Cid As 课程ID, L.score AS 最高分, R.score AS 最低分
  155. FROM t_SC L, t_SC R
  156. WHERE L.Cid = R.Cid
  157. and L.score = (SELECT MAX(IL.score)
  158. FROM t_SC IL, t_Student IM
  159. WHERE L.Cid = IL.Cid
  160. and IM.Stuid = IL.Stuid
  161. GROUP BY IL.Cid)
  162. AND R.Score =
  163. (SELECT MIN(IR.score) FROM t_SC IR WHERE R.Cid = IR.Cid GROUP BY IR.Cid);
  164. */
  165. --19、按各科平均成绩从低到高和及格率的百分数从高到低顺序
  166. select sc.cid, ( select name from t_course where cid=sc.cid) cname,
  167. round( avg(sc.score), 2) avgscore,
  168. round(( select count(*) from t_sc where cid = sc.cid and score>= 70)/
  169. ( select count(*) from t_sc where cid = sc.cid)* 100, 2)|| '%' jgl
  170. from t_sc sc
  171. group by sc.cid
  172. order by avgscore, jgl desc;
  173. /*
  174. SELECT t.C# AS 课程号,max(course.Cname)AS 课程名,isnull(AVG(score),0) AS平均成绩
  175. ,100 * SUM(CASE WHEN isnull(score,0)>=60 THEN 1 ELSE 0 END)/COUNT(*) AS 及格百分数
  176. FROM SC T,Course
  177. where t.C#=course.C#
  178. GROUP BY t.C#
  179. ORDER BY 100* SUM(CASE WHEN isnull(score,0)>=60 THEN 1 ELSE 0 END)/COUNT(*) DESC
  180. */
  181. --20、查询如下课程平均成绩和及格率的百分数(用"1行"显示):
  182. --企业管理(001),马克思(002),OO&UML (003),数据库(004)
  183. select * from t_sc sc group by sc.cid
  184. select sc.cid, ( select name from t_course where cid=sc.cid) cname,
  185. case sc.cid when 1 then round( avg(sc.score), 2) end chinese_avgscore,
  186. case sc.cid when 1 then
  187. round(( select count(*) from t_sc where cid = sc.cid and score>= 70)/
  188. ( select count(*) from t_sc where cid = sc.cid)* 100, 2)|| '%' end chinese_jgl
  189. from t_sc sc
  190. group by sc.cid
  191. --21、查询不同老师所教不同课程平均分从高到低显示
  192. select sc.cid,
  193. round( avg(sc.score), 2) avg_score,
  194. c.name,
  195. t.tname
  196. from t_sc sc, t_course c, t_teacher t
  197. where sc.cid=c.cid and t.tid=c.teacherid
  198. group by sc.cid, c.name, t.tname
  199. order by avg_score desc;
  200. --22、查询如下语文成绩第 2 名到第 5 名的学生成绩单:
  201. --[学生ID],[学生姓名],语文成绩, 排名
  202. select * from (
  203. select t.*, rownum tn from (
  204. select s.stuid, s.name,
  205. min( case c.name when 'Chinese' then sc.score end) chinese_score
  206. from t_student s, t_sc sc, t_course c
  207. where s.stuid = sc.stuid
  208. and sc.cid = c.cid
  209. group by s.stuid, s.name
  210. order by chinese_score desc) t where rownum <= 5) where tn> 1
  211. --23、统计列印各科成绩,各分数段人数:课程ID,课程名称,[100-85],[85-70],[70-60],[ <60]
  212. select sc.cid, c.name,
  213. ( select count(*) from t_sc where cid=sc.cid and score>= 85 and score<= 100) a,
  214. ( select count(*) from t_sc where cid=sc.cid and score>= 70 and score< 85) b,
  215. ( select count(*) from t_sc where cid=sc.cid and score>= 60 and score< 70) c,
  216. ( select count(*) from t_sc where cid=sc.cid and score< 60) d
  217. from t_sc sc, t_course c
  218. where sc.cid = c.cid
  219. group by sc.cid, c.name;
  220. /*
  221. SELECT SC.Cid as 课程ID, c.name as 课程名称
  222. ,SUM(CASE WHEN sc.score BETWEEN 85 AND 100 THEN 1 ELSE 0 END) AS a
  223. ,SUM(CASE WHEN sc.score BETWEEN 70 AND 85 THEN 1 ELSE 0 END) AS b
  224. ,SUM(CASE WHEN sc.score BETWEEN 60 AND 70 THEN 1 ELSE 0 END) AS c
  225. ,SUM(CASE WHEN sc.score < 60 THEN 1 ELSE 0 END) AS d
  226. FROM t_SC sc,t_Course c
  227. where SC.Cid=C.Cid
  228. GROUP BY SC.Cid,c.name;
  229. */
  230. --24、查询学生平均成绩及其名次
  231. select t.*, rownum rn from (
  232. select s.stuid, s.name, avg(sc.score) avg_score from t_student s, t_sc sc where s.stuid=sc.stuid
  233. group by s.stuid, s.name
  234. order by avg_score desc) t;
  235. select t.*, row_number() over( order by avg_score desc) pm from (
  236. select s.stuid, s.name, avg(sc.score) avg_score
  237. from t_student s, t_sc sc where s.stuid=sc.stuid
  238. group by s.stuid, s.name
  239. ) t;
  240. --25、查询各科成绩前三名的记录:(不考虑成绩并列情况)
  241. select * from (
  242. select sc.cid, c.name, sc.score, row_number() over( partition by sc.cid,c.name order by sc.score desc) pm
  243. from t_sc sc, t_course c where sc.cid=c.cid ) where pm <= 3;
  244. /*
  245. SELECT t1.S# as 学生ID,t1.C# as 课程ID,Score as 分数
  246. FROM SC t1
  247. WHERE score IN (SELECT TOP 3 score
  248. FROM SC
  249. WHERE t1.C#= C#
  250. ORDER BY score DESC
  251. )
  252. ORDER BY t1.C#;
  253. */
  254. --26、查询每门课程被选修的学生数
  255. select cid, count(*) rs, ( select name from t_course where cid=t_sc.cid) cname from t_sc group by cid;
  256. --27、查询出只选修了3门课程的全部学生的学号和姓名
  257. select sc.stuid, s.name from t_sc sc, t_student s where sc.stuid = s.stuid group by sc.stuid,s.name having count(*) = 3;
  258. --28、查询男生、女生人数
  259. select case when sex= '1' then 'Man' else 'Woman' end sex, count(*) rs from t_student group by sex;
  260. --29、查询姓“张”的学生名单
  261. select * from t_student where name like 'w%';
  262. --30、查询同名同性学生名单,并统计同名人数
  263. select name, count(*) rs from t_student group by name having count(*)> 1;
  264. --31、1981年出生的学生名单(注:Student表中Sage列的类型是datetime)
  265. select to_char( sysdate, 'yyyy') year from dual;
  266. --32、查询每门课程的平均成绩,结果按平均成绩升序排列,平均成绩相同时,按课程号降序排列
  267. select cid, round( avg(score), 2) avg_score from t_sc group by cid order by avg_score, cid desc;
  268. --33、查询平均成绩大于85的所有学生的学号、姓名和平均成绩
  269. select s.stuid, s.name, round( avg(sc.score), 2) avg_score from t_student s, t_sc sc where s.stuid=sc.stuid group by s.stuid, s.name having avg(sc.score)> 80;
  270. --34、查询课程名称为“数据库”,且分数低于60的学生姓名和分数
  271. select s.stuid, s.name stuname, sc.score from t_student s, t_sc sc, t_course c where s.stuid=sc.stuid and sc.cid=c.cid and c.name= 'Computer' and sc.score< 70;
  272. --35、查询所有学生的选课情况
  273. select s.stuid, s.name stuname, c.cid, c.name coursename from t_student s, t_sc sc, t_course c where s.stuid=sc.stuid and sc.cid=c.cid;
  274. --36、查询任何一门课程成绩在70分以上的姓名、课程名称和分数
  275. select s.stuid, s.name stuname, c.cid, c.name coursename, sc.score from t_student s, t_sc sc, t_course c where s.stuid=sc.stuid and sc.cid=c.cid and sc.score> 70;
  276. --37、查询不及格的课程,并按课程号从大到小排列
  277. select c.cid, c.name from t_sc sc, t_course c where sc.cid=c.cid group by c.cid,c.name,sc.score having sc.score< 70 order by c.cid desc;
  278. --38、查询课程编号为003且课程成绩在80分以上的学生的学号和姓名;
  279. select s.stuid, s.name, sc.score from t_student s,t_sc sc where s.stuid=sc.stuid and sc.cid= 3 and sc.score> 80;
  280. --39、求选了课程的学生人数
  281. select count(*) rs from ( select stuid from t_sc group by stuid);
  282. --40、查询选修“叶平”老师所授课程的学生中,成绩最高的学生姓名及其成绩
  283. select s.stuid, s.name stuname, t.max_score from t_student s, t_sc sc,
  284. ( select c.cid, max(sc.score) max_score from t_sc sc, t_course c, t_teacher t where sc.cid=c.cid and c.teacherid=t.tid
  285. and t.tname= 'Mr.mao' group by c.cid) t
  286. where s.stuid=sc.stuid and sc.cid=t.cid and sc.score=t.max_score;
  287. /*
  288. select S.name stuname, sc.score
  289. from t_Student s, t_SC sc, t_Course C, t_Teacher t
  290. where S.Stuid = SC.Stuid
  291. and SC.Cid = C.Cid
  292. and C.Teacherid = T.tid
  293. and T.Tname = 'Mr.mao'
  294. and SC.score = (select max(score) from t_SC where Cid = C.Cid);*/
  295. --41、查询各个课程及相应的选修人数
  296. select cid, count(*) rs from t_sc group by cid order by cid;
  297. --42、查询不同课程成绩相同的学生的学号、课程号、学生成绩
  298. select * from t_sc sc where exists ( select * from t_sc where stuid!=sc.stuid and cid!=sc.cid and score=sc.score);
  299. select distinct A.Stuid,B.score from t_SC A ,t_SC B where A.Score=B.Score and A.Cid <>B.Cid ;
  300. --43、查询每门功成绩最好的前两名
  301. select * from ( select stuid, cid ,score, row_number() over( partition by cid order by score desc) pm from t_sc) where pm< 3;
  302. /*
  303. SELECT t1.S# as 学生ID, t1.C# as 课程ID, Score as 分数
  304. FROM SC t1
  305. WHERE score IN
  306. (SELECT TOP 2 score FROM SC WHERE t1.C# = C# ORDER BY score DESC)
  307. ORDER BY t1.C#;
  308. */
  309. --44、统计每门课程的学生选修人数(超过10人的课程才统计)。要求输出课程号和选修人数,
  310. --查询结果按人数降序排列,查询结果按人数降序排列,若人数相同,按课程号升序排列
  311. select cid, count(*) rs from t_sc group by cid having count(*) > 5 order by rs desc, cid;
  312. --45、检索至少选修两门课程的学生学号
  313. select stuid , count(*) from t_sc group by stuid having count(*) > 2;
  314. --46、查询全部学生都选修的课程的课程号和课程名
  315. select c.cid, c.name from t_course c where exists (
  316. select cid, count(*) rs from t_sc where cid=c.cid group by cid having count(*) = ( select count(*) from t_student)
  317. );
  318. --47、查询没学过“叶平”老师讲授的任一门课程的学生姓名
  319. select * from t_student where stuid not in(
  320. select s.stuid from t_student s, t_sc sc where s.stuid=sc.stuid and sc.cid in ( select cid from t_course c, t_teacher t where c.teacherid=t.tid and t.tname= 'Mr.zhu'));
  321. select * from t_Student s where s.stuid not in ( select sc.stuid from t_Course c,t_Teacher t,t_SC sc where c.teacherid=T.Tid and SC.Cid=c.Cid and t.Tname= 'Mr.zhu');
  322. --48、查询1门以上不及格课程的同学的学号及其平均成绩
  323. select stuid, avg(score) avg_score from t_sc group by stuid having stuid in(
  324. select stuid from t_sc where score< 70 group by stuid having count(*) > 1);
  325. select S#, avg( isnull(score, 0)) from SC where S# in ( select S# from SC where score < 60 group by S# having count(*)> 1) group by S#;
  326. --49、检索“5”课程分数小于60,按分数降序排列的同学学号
  327. select stuid from t_sc where cid= 5 and score< 70 order by score desc;
  328. --50、删除“002”同学的“001”课程的成绩
  329. delete from t_sc where stuid= 2 and cid= 1;


文章标签:  sql 数据库 oracle

题目来源于http://blog.csdn.net/lifetragedy/article/details/9935699,由于原文题目中的数据在我自己建的DB里没有数据,所以解答中的条件可能是符合我DB的条件,而不是符合题目的条件。但解答应该符合题目的意思要求。但是文中的解答肯能会有错误,但都是经过我DB中实际跑过的。欢迎指出错误。

select * from t_student 学生表


select * from t_course 课程表

select * from t_sc 学生成绩表



select * from t_teacher 老师表



  1. --http://blog.csdn.net/lifetragedy/article/details/9935699
  2. --create table t_student(stuid number(10,2) primary key,name varchar2(100), age number(3,0), sex char(1));
  3. --create table t_course(cid number(10,2) primary key, name varchar2(100), teacherid number(10, 2));
  4. --alter table t_course
  5. --add constraint tcourse_fk foreign key (teacherid) references t_teacher(tid);
  6. --create table t_sc(stuid number(10, 2), cid number(10,2), score number(10,2));
  7. --alter table t_sc
  8. --add constraint t_sc_pk primary key (stuid, cid);
  9. --alter table t_sc
  10. --add constraint t_sc_stuid_fk foreign key (stuid) references t_student(stuid);
  11. --alter table t_sc
  12. --add constraint t_sc_cid_fk foreign key (cid) references t_course(cid);
  13. --create table t_teacher(tid number(10,2) primary key, tname varchar2(100));
  14. /*
  15. select * from t_student for update;
  16. select * from t_course for update;
  17. select * from t_sc for update;
  18. select * from t_teacher for update;
  19. */
  20. --查询“1”课程比“2”课程成绩高的所有学生的学号
  21. select a.stuid
  22. from ( select * from t_sc s where s.cid = 1) a,
  23. ( select * from t_sc s where s.cid = 2) b
  24. where a.stuid = b.stuid
  25. and a.score > b.score;
  26. /*
  27. select a.S#
  28. from (select s#, score from SC where C# = '001') a,
  29. (select s#, score from SC where C# = '002')
  30. where a.score > b.score
  31. and a.s# = b.s#;
  32. */
  33. --2、查询平均成绩大于80分的同学的学号和平均成绩;
  34. select stuid, avg(score) from t_sc group by stuid having avg(score)> 80;
  35. --3、查询所有同学的学号、姓名、选课数、总成绩;
  36. select s.stuid, s.name, c.xks, c.zcj
  37. from t_student s left outer join
  38. ( select stuid, count(cid) xks, sum(score) zcj
  39. from t_sc
  40. group by stuid) c
  41. on s.stuid = c.stuid;
  42. select s.stuid, s.name, count(c.cid), sum(c.score)
  43. from t_student s
  44. left Outer join t_sc c
  45. on s.stuid = c.stuid
  46. group by s.stuid, s.name;
  47. --4、查询姓“李”的老师的个数;
  48. select count(tid) from t_teacher where tname like '李%';
  49. --5、查询没学过“叶平”老师课的同学的学号、姓名;
  50. select s.stuid,s.name from t_student s, t_course c, t_teacher t, t_sc sc where s.stuid=sc.stuid and c.cid=sc.cid and c.teacherid=t.tid and t.tname!= '叶平';
  51. select Student.S#, Student.Sname
  52. from Student
  53. where S# not in
  54. ( select distinct (SC.S#) from SC,
  55. Course,
  56. Teacher where SC.C# = Course.C# and Teacher.T# = Course.T# andTeacher.Tname = '叶平');
  57. --6、查询学过“001”并且也学过编号“002”课程的同学的学号、姓名;
  58. select s.stuid,s.name from t_student s, t_sc c where c.cid= 1 and s.stuid=c.stuid and exists ( select * from t_sc sc where sc.stuid=s.stuid and sc.cid= 2);
  59. -- select Student.S#,Student.Sname fromStudent,SC where Student.S#=SC.S# andSC.C#='001'and exists( Select * from SC as SC_2 where SC_2.S#=SC.S# and SC_2.C#='002');
  60. --7、查询学过“叶平”老师所教的所有课的同学的学号、姓名;
  61. select s.stuid,s.name from t_student s, t_sc sc where s.stuid = sc.stuid and sc.cid in(
  62. select c.cid from t_teacher t, t_course c where t.tid=c.teacherid and t.tname= 'Mr.kay');
  63. select S#, Sname
  64. from Student
  65. where S# in
  66. ( select S#
  67. from SC, Course, Teacher
  68. where SC.C# = Course.C# andTeacher.T# = Course.T#
  69. and Teacher.Tname = '叶平'
  70. group by S#
  71. having count(SC.C#) = ( select count(C#) from Course,
  72. Teacher where Teacher.T# = Course.T# and Tname = '叶平'));
  73. --8、查询课程编号“002”的成绩比课程编号“001”课程低的所有同学的学号、姓名;
  74. Select S#, Sname
  75. from ( select Student.S#,
  76. Student.Sname,
  77. score,
  78. ( select score
  79. from SC SC_2
  80. where SC_2.S# = Student.S# and SC_2.C# = '002') score2
  81. from Student, SC
  82. where Student.S# = SC.S# andC# = '001') S_2
  83. where score2 < score;
  84. --9、查询所有课程成绩小于60分的同学的学号、姓名;
  85. select s.stuid, s.name from t_student s where s.stuid not in ( select sc.stuid from t_student s, t_sc sc where sc.score>= 60 and s.stuid=sc.stuid);
  86. select s.stuid, s.name from t_student s where not exists ( select * from t_sc sc where sc.stuid=s.stuid and sc.score>= 60);
  87. --10、查询没有学全所有课的同学的学号、姓名;
  88. select s.stuid, s.name from t_student s,t_sc sc where s.stuid=sc.stuid group by s.stuid,s.name having count(sc.cid) != ( select count(cid) from t_course);
  89. select Student.S#, Student.Sname
  90. from Student, SC whereStudent.S# = SC.S#
  91. group by Student.S#, Student.Sname
  92. having count(C#) < ( select count(C#) from Course);
  93. --11、查询至少有一门课与学号为“1001”的同学所学相同的同学的学号和姓名;
  94. select distinct s.stuid,s.name from t_student s, t_sc sc where s.stuid=sc.stuid and s.stuid!= 1 and sc.cid in ( select cid from t_sc where stuid= 1)
  95. --select s.stuid,s.name from t_Student s, t_SC sc where s.stuid=SC.Stuid and sc.cid in (select Cid from t_SC where stuid='1');
  96. --13、把“t_SC”表中“叶平”老师教的课的成绩都更改为此课程的平均成绩;
  97. update t_sc sc1 set sc1.score =
  98. ( select avg(score) from t_sc group by cid having cid in ( select cid from t_teacher t, t_course c where t.tid=c.teacherid and t.tname= 'Mr.mao') and cid=sc1.cid);
  99. /*
  100. update t_sc sc_1
  101. set sc_1.score =
  102. (select avg(sc_2.score) from t_sc sc_2 where sc_2.cid = sc_1.cid)
  103. from t_course,
  104. t_teacher
  105. where t_course.Cid = t_sc.cid and t_course.tid = t_teacher.tid
  106. and t_teacher.tname = 'Mr.mao');
  107. */
  108. select * from t_sc sc, t_course c, t_teacher t where sc.cid=c.cid and c.teacherid=t.tid and t.tname= 'Mr.mao'
  109. --14、查询和“1002”号的同学学习的课程完全相同的其他同学学号和姓名;
  110. select * from t_sc where stuid in(
  111. select stuid
  112. from t_sc
  113. group by stuid
  114. having count(*) = ( select count(*) from t_sc where stuid = 6)
  115. )
  116. and cid in ( select cid from t_sc where stuid= 6);
  117. --15、删除学习“叶平”老师课的SC表记录
  118. delete t_sc where cid in(
  119. select cid from t_course c, t_teacher t where c.teacherid=t.tid and t.tname= 'Mr.mao');
  120. --16、向SC表中插入一些记录,这些记录要求符合以下条件:没有上过编号“5”课程的同学学号、2号课的平均成绩;
  121. insert into t_sc
  122. select stuid, '5' cid, ( select avg(score) from t_sc sc where sc.cid= 2) score
  123. from t_student s where not exists
  124. ( select * from t_sc sc where sc.cid= 5 and sc.stuid=s.stuid);
  125. --17、按平均成绩从高到低显示所有学生的“数据库”、“企业管理”、“英语”三门的课程成绩,按
  126. --如下形式显示: 学生ID,,数据库,企业管理,英语,有效课程数,有效平均分
  127. select t.stuid,
  128. max( case t.cname when 'Chinese' then t.score end) Chinese,
  129. max( case t.cname when 'Math' then t.score end) Math,
  130. max( case t.cname when 'English' then t.score end) English,
  131. round( avg(t.score), 2) avgscore,
  132. count(t.cid) kcs
  133. from ( select s.stuid, s.name stuname, sc.cid, sc.score, c.name cname
  134. from t_student s, t_sc sc, t_course c
  135. where s.stuid = sc.stuid
  136. and sc.cid = c.cid
  137. and c.name in ( 'Chinese', 'Math', 'English')) t
  138. group by t.stuid
  139. order by avgscore desc;
  140. /*
  141. select sc.stuid,
  142. (select score from t_sc where cid=1 and stuid=sc.stuid) Chinese,
  143. (select score from t_sc where cid=2 and stuid=sc.stuid) Math,
  144. (select score from t_sc where cid=3 and stuid=sc.stuid) English,
  145. round(avg(sc.score),2) avgscore,
  146. count(c.cid) kcs
  147. from t_sc sc, t_course c where sc.cid=c.cid and c.name in ('Chinese', 'Math', 'English')
  148. group by sc.stuid
  149. order by avgscore desc;
  150. */
  151. --18、查询各科成绩最高和最低的分:以如下形式显示:课程ID,最高分,最低分
  152. select cid, max(score) maxscore, min(score) minscore from t_sc group by cid order by cid;
  153. /*
  154. SELECT L.Cid As 课程ID, L.score AS 最高分, R.score AS 最低分
  155. FROM t_SC L, t_SC R
  156. WHERE L.Cid = R.Cid
  157. and L.score = (SELECT MAX(IL.score)
  158. FROM t_SC IL, t_Student IM
  159. WHERE L.Cid = IL.Cid
  160. and IM.Stuid = IL.Stuid
  161. GROUP BY IL.Cid)
  162. AND R.Score =
  163. (SELECT MIN(IR.score) FROM t_SC IR WHERE R.Cid = IR.Cid GROUP BY IR.Cid);
  164. */
  165. --19、按各科平均成绩从低到高和及格率的百分数从高到低顺序
  166. select sc.cid, ( select name from t_course where cid=sc.cid) cname,
  167. round( avg(sc.score), 2) avgscore,
  168. round(( select count(*) from t_sc where cid = sc.cid and score>= 70)/
  169. ( select count(*) from t_sc where cid = sc.cid)* 100, 2)|| '%' jgl
  170. from t_sc sc
  171. group by sc.cid
  172. order by avgscore, jgl desc;
  173. /*
  174. SELECT t.C# AS 课程号,max(course.Cname)AS 课程名,isnull(AVG(score),0) AS平均成绩
  175. ,100 * SUM(CASE WHEN isnull(score,0)>=60 THEN 1 ELSE 0 END)/COUNT(*) AS 及格百分数
  176. FROM SC T,Course
  177. where t.C#=course.C#
  178. GROUP BY t.C#
  179. ORDER BY 100* SUM(CASE WHEN isnull(score,0)>=60 THEN 1 ELSE 0 END)/COUNT(*) DESC
  180. */
  181. --20、查询如下课程平均成绩和及格率的百分数(用"1行"显示):
  182. --企业管理(001),马克思(002),OO&UML (003),数据库(004)
  183. select * from t_sc sc group by sc.cid
  184. select sc.cid, ( select name from t_course where cid=sc.cid) cname,
  185. case sc.cid when 1 then round( avg(sc.score), 2) end chinese_avgscore,
  186. case sc.cid when 1 then
  187. round(( select count(*) from t_sc where cid = sc.cid and score>= 70)/
  188. ( select count(*) from t_sc where cid = sc.cid)* 100, 2)|| '%' end chinese_jgl
  189. from t_sc sc
  190. group by sc.cid
  191. --21、查询不同老师所教不同课程平均分从高到低显示
  192. select sc.cid,
  193. round( avg(sc.score), 2) avg_score,
  194. c.name,
  195. t.tname
  196. from t_sc sc, t_course c, t_teacher t
  197. where sc.cid=c.cid and t.tid=c.teacherid
  198. group by sc.cid, c.name, t.tname
  199. order by avg_score desc;
  200. --22、查询如下语文成绩第 2 名到第 5 名的学生成绩单:
  201. --[学生ID],[学生姓名],语文成绩, 排名
  202. select * from (
  203. select t.*, rownum tn from (
  204. select s.stuid, s.name,
  205. min( case c.name when 'Chinese' then sc.score end) chinese_score
  206. from t_student s, t_sc sc, t_course c
  207. where s.stuid = sc.stuid
  208. and sc.cid = c.cid
  209. group by s.stuid, s.name
  210. order by chinese_score desc) t where rownum <= 5) where tn> 1
  211. --23、统计列印各科成绩,各分数段人数:课程ID,课程名称,[100-85],[85-70],[70-60],[ <60]
  212. select sc.cid, c.name,
  213. ( select count(*) from t_sc where cid=sc.cid and score>= 85 and score<= 100) a,
  214. ( select count(*) from t_sc where cid=sc.cid and score>= 70 and score< 85) b,
  215. ( select count(*) from t_sc where cid=sc.cid and score>= 60 and score< 70) c,
  216. ( select count(*) from t_sc where cid=sc.cid and score< 60) d
  217. from t_sc sc, t_course c
  218. where sc.cid = c.cid
  219. group by sc.cid, c.name;
  220. /*
  221. SELECT SC.Cid as 课程ID, c.name as 课程名称
  222. ,SUM(CASE WHEN sc.score BETWEEN 85 AND 100 THEN 1 ELSE 0 END) AS a
  223. ,SUM(CASE WHEN sc.score BETWEEN 70 AND 85 THEN 1 ELSE 0 END) AS b
  224. ,SUM(CASE WHEN sc.score BETWEEN 60 AND 70 THEN 1 ELSE 0 END) AS c
  225. ,SUM(CASE WHEN sc.score < 60 THEN 1 ELSE 0 END) AS d
  226. FROM t_SC sc,t_Course c
  227. where SC.Cid=C.Cid
  228. GROUP BY SC.Cid,c.name;
  229. */
  230. --24、查询学生平均成绩及其名次
  231. select t.*, rownum rn from (
  232. select s.stuid, s.name, avg(sc.score) avg_score from t_student s, t_sc sc where s.stuid=sc.stuid
  233. group by s.stuid, s.name
  234. order by avg_score desc) t;
  235. select t.*, row_number() over( order by avg_score desc) pm from (
  236. select s.stuid, s.name, avg(sc.score) avg_score
  237. from t_student s, t_sc sc where s.stuid=sc.stuid
  238. group by s.stuid, s.name
  239. ) t;
  240. --25、查询各科成绩前三名的记录:(不考虑成绩并列情况)
  241. select * from (
  242. select sc.cid, c.name, sc.score, row_number() over( partition by sc.cid,c.name order by sc.score desc) pm
  243. from t_sc sc, t_course c where sc.cid=c.cid ) where pm <= 3;
  244. /*
  245. SELECT t1.S# as 学生ID,t1.C# as 课程ID,Score as 分数
  246. FROM SC t1
  247. WHERE score IN (SELECT TOP 3 score
  248. FROM SC
  249. WHERE t1.C#= C#
  250. ORDER BY score DESC
  251. )
  252. ORDER BY t1.C#;
  253. */
  254. --26、查询每门课程被选修的学生数
  255. select cid, count(*) rs, ( select name from t_course where cid=t_sc.cid) cname from t_sc group by cid;
  256. --27、查询出只选修了3门课程的全部学生的学号和姓名
  257. select sc.stuid, s.name from t_sc sc, t_student s where sc.stuid = s.stuid group by sc.stuid,s.name having count(*) = 3;
  258. --28、查询男生、女生人数
  259. select case when sex= '1' then 'Man' else 'Woman' end sex, count(*) rs from t_student group by sex;
  260. --29、查询姓“张”的学生名单
  261. select * from t_student where name like 'w%';
  262. --30、查询同名同性学生名单,并统计同名人数
  263. select name, count(*) rs from t_student group by name having count(*)> 1;
  264. --31、1981年出生的学生名单(注:Student表中Sage列的类型是datetime)
  265. select to_char( sysdate, 'yyyy') year from dual;
  266. --32、查询每门课程的平均成绩,结果按平均成绩升序排列,平均成绩相同时,按课程号降序排列
  267. select cid, round( avg(score), 2) avg_score from t_sc group by cid order by avg_score, cid desc;
  268. --33、查询平均成绩大于85的所有学生的学号、姓名和平均成绩
  269. select s.stuid, s.name, round( avg(sc.score), 2) avg_score from t_student s, t_sc sc where s.stuid=sc.stuid group by s.stuid, s.name having avg(sc.score)> 80;
  270. --34、查询课程名称为“数据库”,且分数低于60的学生姓名和分数
  271. select s.stuid, s.name stuname, sc.score from t_student s, t_sc sc, t_course c where s.stuid=sc.stuid and sc.cid=c.cid and c.name= 'Computer' and sc.score< 70;
  272. --35、查询所有学生的选课情况
  273. select s.stuid, s.name stuname, c.cid, c.name coursename from t_student s, t_sc sc, t_course c where s.stuid=sc.stuid and sc.cid=c.cid;
  274. --36、查询任何一门课程成绩在70分以上的姓名、课程名称和分数
  275. select s.stuid, s.name stuname, c.cid, c.name coursename, sc.score from t_student s, t_sc sc, t_course c where s.stuid=sc.stuid and sc.cid=c.cid and sc.score> 70;
  276. --37、查询不及格的课程,并按课程号从大到小排列
  277. select c.cid, c.name from t_sc sc, t_course c where sc.cid=c.cid group by c.cid,c.name,sc.score having sc.score< 70 order by c.cid desc;
  278. --38、查询课程编号为003且课程成绩在80分以上的学生的学号和姓名;
  279. select s.stuid, s.name, sc.score from t_student s,t_sc sc where s.stuid=sc.stuid and sc.cid= 3 and sc.score> 80;
  280. --39、求选了课程的学生人数
  281. select count(*) rs from ( select stuid from t_sc group by stuid);
  282. --40、查询选修“叶平”老师所授课程的学生中,成绩最高的学生姓名及其成绩
  283. select s.stuid, s.name stuname, t.max_score from t_student s, t_sc sc,
  284. ( select c.cid, max(sc.score) max_score from t_sc sc, t_course c, t_teacher t where sc.cid=c.cid and c.teacherid=t.tid
  285. and t.tname= 'Mr.mao' group by c.cid) t
  286. where s.stuid=sc.stuid and sc.cid=t.cid and sc.score=t.max_score;
  287. /*
  288. select S.name stuname, sc.score
  289. from t_Student s, t_SC sc, t_Course C, t_Teacher t
  290. where S.Stuid = SC.Stuid
  291. and SC.Cid = C.Cid
  292. and C.Teacherid = T.tid
  293. and T.Tname = 'Mr.mao'
  294. and SC.score = (select max(score) from t_SC where Cid = C.Cid);*/
  295. --41、查询各个课程及相应的选修人数
  296. select cid, count(*) rs from t_sc group by cid order by cid;
  297. --42、查询不同课程成绩相同的学生的学号、课程号、学生成绩
  298. select * from t_sc sc where exists ( select * from t_sc where stuid!=sc.stuid and cid!=sc.cid and score=sc.score);
  299. select distinct A.Stuid,B.score from t_SC A ,t_SC B where A.Score=B.Score and A.Cid <>B.Cid ;
  300. --43、查询每门功成绩最好的前两名
  301. select * from ( select stuid, cid ,score, row_number() over( partition by cid order by score desc) pm from t_sc) where pm< 3;
  302. /*
  303. SELECT t1.S# as 学生ID, t1.C# as 课程ID, Score as 分数
  304. FROM SC t1
  305. WHERE score IN
  306. (SELECT TOP 2 score FROM SC WHERE t1.C# = C# ORDER BY score DESC)
  307. ORDER BY t1.C#;
  308. */
  309. --44、统计每门课程的学生选修人数(超过10人的课程才统计)。要求输出课程号和选修人数,
  310. --查询结果按人数降序排列,查询结果按人数降序排列,若人数相同,按课程号升序排列
  311. select cid, count(*) rs from t_sc group by cid having count(*) > 5 order by rs desc, cid;
  312. --45、检索至少选修两门课程的学生学号
  313. select stuid , count(*) from t_sc group by stuid having count(*) > 2;
  314. --46、查询全部学生都选修的课程的课程号和课程名
  315. select c.cid, c.name from t_course c where exists (
  316. select cid, count(*) rs from t_sc where cid=c.cid group by cid having count(*) = ( select count(*) from t_student)
  317. );
  318. --47、查询没学过“叶平”老师讲授的任一门课程的学生姓名
  319. select * from t_student where stuid not in(
  320. select s.stuid from t_student s, t_sc sc where s.stuid=sc.stuid and sc.cid in ( select cid from t_course c, t_teacher t where c.teacherid=t.tid and t.tname= 'Mr.zhu'));
  321. select * from t_Student s where s.stuid not in ( select sc.stuid from t_Course c,t_Teacher t,t_SC sc where c.teacherid=T.Tid and SC.Cid=c.Cid and t.Tname= 'Mr.zhu');
  322. --48、查询1门以上不及格课程的同学的学号及其平均成绩
  323. select stuid, avg(score) avg_score from t_sc group by stuid having stuid in(
  324. select stuid from t_sc where score< 70 group by stuid having count(*) > 1);
  325. select S#, avg( isnull(score, 0)) from SC where S# in ( select S# from SC where score < 60 group by S# having count(*)> 1) group by S#;
  326. --49、检索“5”课程分数小于60,按分数降序排列的同学学号
  327. select stuid from t_sc where cid= 5 and score< 70 order by score desc;
  328. --50、删除“002”同学的“001”课程的成绩
  329. delete from t_sc where stuid= 2 and cid= 1;


猜你喜欢

转载自blog.csdn.net/qq_28817739/article/details/80910856