当前位置:   article > 正文

程序员必修课--sql思维举重训练

sql逻辑思维训练

写sql是程序员基本功课,找工作面试一般必有sql题,实际工作中对sql的需求更是千变万化,所以掌握好sql对于一个程序员来说是件非常重要的事情.本文通过一个简单易懂的关系(学生/课程/成绩关系)尽量构造出各种各样的需求来提高我们写sql的功底,其中有些sql还是有一些难度的,大多数sql我都给出了思考思路,希望本人N天的劳动成果对您有所帮助.

限于本人水平有限,有些地方的sql写的并不完美,希望大家不吝赐教.最后提一点小小的要求,希望大家对本文积极进行评论,大家的评论是我改进和提高的动力,我希望在和大家的交流中得到提高.

表关系:



建表和初始化sql(本例子使用oralce数据库):

  1. --创建表
  2. create table T_STUDENT(sno NUMBER not null, sname VARCHAR2(30), sdree VARCHAR2(50), sage NUMBER, ssex CHAR(2));
  3. alter table T_STUDENT add primary key (SNO);
  4. create table T_SCORE(sno NUMBER, cno NUMBER, grade NUMBER(4,1), tno NUMBER, id NUMBER not null);
  5. alter table T_SCORE add primary key (ID);
  6. create table T_COURSE(cno NUMBER not null, cname VARCHAR2(30));
  7. alter table T_COURSE add primary key (CNO);
  8. --初始化学生表
  9. insert into T_STUDENT (SNO, SNAME, SDREE, SAGE, SSEX) values (1, '李坤', '天融信', 26, '男');
  10. insert into T_STUDENT (SNO, SNAME, SDREE, SAGE, SSEX) values (2, '曹贵生', '中银', 26, '男');
  11. insert into T_STUDENT (SNO, SNAME, SDREE, SAGE, SSEX) values (3, '柳波', '买卖宝', 27, '男');
  12. insert into T_STUDENT (SNO, SNAME, SDREE, SAGE, SSEX) values (4, '纪争光', 'IBM', 23, '男');
  13. insert into T_STUDENT (SNO, SNAME, SDREE, SAGE, SSEX) values (5, '李学宇', '微软', 25, '女');
  14. insert into T_STUDENT (SNO, SNAME, SDREE, SAGE, SSEX) values (6, '李雪琪', '文思创新', 25, '女');
  15. insert into T_STUDENT (SNO, SNAME, SDREE, SAGE, SSEX) values (7, '陈绪', '中海油', 26, '男');
  16. insert into T_STUDENT (SNO, SNAME, SDREE, SAGE, SSEX) values (8, '韩正阳', '中海油', 24, '男');
  17. insert into T_STUDENT (SNO, SNAME, SDREE, SAGE, SSEX) values (9, '陈伟东', '中核', 24, '男');
  18. insert into T_STUDENT (SNO, SNAME, SDREE, SAGE, SSEX) values (10, '刘兵', '优酷', 24, '男');
  19. insert into T_STUDENT (SNO, SNAME, SDREE, SAGE, SSEX) values (11, '丁成云', '联想', 25, '女');
  20. insert into T_STUDENT (SNO, SNAME, SDREE, SAGE, SSEX) values (12, '王鹏', '中兴', 25, '男');
  21. commit;
  22. --初始化课程表
  23. insert into T_COURSE (CNO, CNAME) values (1, 'JAVA程序设计');
  24. insert into T_COURSE (CNO, CNAME) values (2, 'ORACLE开发');
  25. insert into T_COURSE (CNO, CNAME) values (3, 'C++程序设计');
  26. insert into T_COURSE (CNO, CNAME) values (4, 'C#程序设计');
  27. insert into T_COURSE (CNO, CNAME) values (5, 'Windows实战');
  28. insert into T_COURSE (CNO, CNAME) values (6, 'Center OS教程');
  29. insert into T_COURSE (CNO, CNAME) values (7, 'Jsp/Servlet开发');
  30. insert into T_COURSE (CNO, CNAME) values (8, 'J2EE从入门到精通');
  31. insert into T_COURSE (CNO, CNAME) values (9, 'EJB及设计模式');
  32. insert into T_COURSE (CNO, CNAME) values (10, 'Javascript/jQuery实战');
  33. insert into T_COURSE (CNO, CNAME) values (11, 'Flash设计');
  34. insert into T_COURSE (CNO, CNAME) values (12, 'HTML/CSS/JAVASCRIPT实战');
  35. insert into T_COURSE (CNO, CNAME) values (13, '精通ASP.NET');
  36. insert into T_COURSE (CNO, CNAME) values (14, 'JBoss入门');
  37. insert into T_COURSE (CNO, CNAME) values (15, 'Spring开发');
  38. commit;
  39. --初始化成绩表
  40. insert into T_SCORE (SNO, CNO, GRADE, TNO, ID) values (1, 2, 90.0, 2, 1);
  41. insert into T_SCORE (SNO, CNO, GRADE, TNO, ID) values (1, 3, 80.0, 3, 2);
  42. insert into T_SCORE (SNO, CNO, GRADE, TNO, ID) values (1, 4, 90.0, 4, 3);
  43. insert into T_SCORE (SNO, CNO, GRADE, TNO, ID) values (11, 2, 70.0, 2, 4);
  44. insert into T_SCORE (SNO, CNO, GRADE, TNO, ID) values (2, 11, 66.0, 11, 5);
  45. insert into T_SCORE (SNO, CNO, GRADE, TNO, ID) values (2, 15, 77.0, 15, 6);
  46. insert into T_SCORE (SNO, CNO, GRADE, TNO, ID) values (2, 8, 87.0, 8, 7);
  47. insert into T_SCORE (SNO, CNO, GRADE, TNO, ID) values (2, 6, 96.0, 6, 8);
  48. insert into T_SCORE (SNO, CNO, GRADE, TNO, ID) values (3, 2, 89.0, 2, 9);
  49. insert into T_SCORE (SNO, CNO, GRADE, TNO, ID) values (3, 1, 91.0, 1, 10);
  50. insert into T_SCORE (SNO, CNO, GRADE, TNO, ID) values (4, 2, 83.0, 2, 11);
  51. insert into T_SCORE (SNO, CNO, GRADE, TNO, ID) values (5, 4, 73.0, 4, 12);
  52. insert into T_SCORE (SNO, CNO, GRADE, TNO, ID) values (5, 1, 60.0, 1, 13);
  53. insert into T_SCORE (SNO, CNO, GRADE, TNO, ID) values (5, 8, 82.0, 8, 14);
  54. insert into T_SCORE (SNO, CNO, GRADE, TNO, ID) values (6, 8, 90.5, 10, 15);
  55. insert into T_SCORE (SNO, CNO, GRADE, TNO, ID) values (8, 2, 58.0, 2, 16);
  56. insert into T_SCORE (SNO, CNO, GRADE, TNO, ID) values (11, 3, 80.0, 3, 17);
  57. insert into T_SCORE (SNO, CNO, GRADE, TNO, ID) values (9, 11, 65.0, 11, 18);
  58. insert into T_SCORE (SNO, CNO, GRADE, TNO, ID) values (9, 12, 67.0, 12, 19);
  59. insert into T_SCORE (SNO, CNO, GRADE, TNO, ID) values (9, 15, 95.0, 15, 20);
  60. insert into T_SCORE (SNO, CNO, GRADE, TNO, ID) values (9, 13, 59.0, 13, 21);
  61. insert into T_SCORE (SNO, CNO, GRADE, TNO, ID) values (10, 4, 98.0, 4, 22);
  62. insert into T_SCORE (SNO, CNO, GRADE, TNO, ID) values (10, 6, 97.0, 6, 23);
  63. insert into T_SCORE (SNO, CNO, GRADE, TNO, ID) values (10, 7, 96.0, 7, 24);
  64. insert into T_SCORE (SNO, CNO, GRADE, TNO, ID) values (11, 7, 95.0, 7, 25);
  65. insert into T_SCORE (SNO, CNO, GRADE, TNO, ID) values (12, 8, 69.0, 8, 26);
  66. insert into T_SCORE (SNO, CNO, GRADE, TNO, ID) values (12, 9, 85.0, 9, 27);
  67. insert into T_SCORE (SNO, CNO, GRADE, TNO, ID) values (10, 14, 100.0, 14, 28);
  68. insert into T_SCORE (SNO, CNO, GRADE, TNO, ID) values (6, 9, 100.0, 9, 29);
  69. insert into T_SCORE (SNO, CNO, GRADE, TNO, ID) values (11, 1, 59.0, 1, 30);
  70. insert into T_SCORE (SNO, CNO, GRADE, TNO, ID) values (11, 4, 90.0, 4, 31);
  71. insert into T_SCORE (SNO, CNO, GRADE, TNO, ID) values (11, 5, 91.0, 5, 32);
  72. insert into T_SCORE (SNO, CNO, GRADE, TNO, ID) values (11, 6, 58.0, 6, 33);
  73. insert into T_SCORE (SNO, CNO, GRADE, TNO, ID) values (11, 8, 93.0, 8, 34);
  74. insert into T_SCORE (SNO, CNO, GRADE, TNO, ID) values (11, 9, 57.0, 9, 35);
  75. insert into T_SCORE (SNO, CNO, GRADE, TNO, ID) values (11, 10, 95.0, 10, 36);
  76. insert into T_SCORE (SNO, CNO, GRADE, TNO, ID) values (11, 11, 96.0, 11, 37);
  77. insert into T_SCORE (SNO, CNO, GRADE, TNO, ID) values (11, 12, 97.0, 12, 38);
  78. insert into T_SCORE (SNO, CNO, GRADE, TNO, ID) values (11, 13, 98.0, 13, 39);
  79. insert into T_SCORE (SNO, CNO, GRADE, TNO, ID) values (11, 14, 99.0, 14, 40);
  80. insert into T_SCORE (SNO, CNO, GRADE, TNO, ID) values (11, 15, 89.0, 15, 41);
  81. commit;
在思维举重前,先来一个热身运动吧:
  1. --(一)查询选修课程名称为'JAVA程序设计'的学员学号和姓名
  2. --方法一:
  3. --1.查询'JAVA程序设计'这门课程的课程号
  4. --2.在成绩表中查询课程号为步骤1的课程号的成绩信息..
  5. --3.在学生表中查询学号为步骤2结果的学生信息
  6. select st.sno, st.sname
  7. from student st
  8. where st.sno in
  9. (select sno
  10. from score sc
  11. where sc.cno =
  12. (select co.cno from course co where co.cname = 'JAVA程序设计'));
  13. --方法二:
  14. --1.查询'JAVA程序设计'这门课程的课程号
  15. --2.在成绩表和学生表关联结果中查询课程号等于步骤1的课程号的成绩信息.
  16. --sql86
  17. select st.sno, st.sname
  18. from student st, score sc
  19. where st.sno = sc.sno
  20. and sc.cno =
  21. (select co.cno from course co where co.cname = 'JAVA程序设计');
  22. --sql92
  23. select st.sno, st.sname
  24. from student st
  25. join score sc
  26. on st.sno = sc.sno
  27. where sc.cno =
  28. (select co.cno from course co where co.cname = 'JAVA程序设计');
  29. --方法三:
  30. --1.在成绩表和课程表关联结果中查询选修了'JAVA程序设计'这门课的学生的学号
  31. --2.在学生表中查询步骤1中的学号的学生的详细信息.
  32. --sql86
  33. select st.sno, st.sname
  34. from student st
  35. where st.sno in (select sc.sno
  36. from score sc, course co
  37. where sc.cno = co.cno
  38. and co.cname = 'JAVA程序设计');
  39. --sql92
  40. select st.sno, st.sname
  41. from student st
  42. where st.sno in (select sc.sno
  43. from score sc
  44. join course co
  45. on sc.cno = co.cno
  46. where co.cname = 'JAVA程序设计');
  47. --方法四:
  48. --在成绩表和课程表和学生表三表关联的结果中过滤得到选修了 'JAVA程序设计'的学生基本信息
  49. --sql86
  50. select st.sno, st.sname
  51. from student st, score sc, course co
  52. where st.sno = sc.sno
  53. and co.cno = sc.cno
  54. and co.cname = 'JAVA程序设计';
  55. --sql92(1) 用where过滤
  56. select st.sno, st.sname
  57. from student st
  58. join score sc
  59. on st.sno = sc.sno
  60. join course co
  61. on co.cno = sc.cno
  62. where co.cname = 'JAVA程序设计';
  63. --sql92(2) 在关联条件中过滤
  64. select st.sno, st.sname
  65. from student st
  66. join score sc
  67. on st.sno = sc.sno
  68. join course co
  69. on co.cno = sc.cno
  70. and co.cname = 'JAVA程序设计';
  71. 注: 1.对于sql86 和sql92的区别见这篇文章: http://blog.csdn.net/lk_blog/article/details/7580300
  72. 2.如果您在看的过程中觉得不熟悉的地方很多,建议您先看此文: http://blog.csdn.net/lk_blog/article/details/7585501

进入正文:

  1. --(一)查询不选修课程编号为'1'的学员姓名和所属单位
  2. --1.在成绩表中查询课程号为'1'的所有学生学号
  3. --2.在学生表中查询学号不在步骤1中的学生的基本信息.
  4. select st.sname, st.sdree
  5. from t_student st
  6. where st.sno not in (select sc.sno from t_score sc where sc.cno = '1');
  7. --(二)查询平均成绩大于85的所有学生的学号、姓名和平均成绩?
  8. select sc.sno, st.sname, avg(sc.grade)
  9. from t_score sc
  10. join t_student st
  11. on sc.sno = st.sno
  12. group by sc.sno, st.sname
  13. having avg(sc.grade) > 85;
  14. --(三)查询课程名称为"JAVA程序设计",且分数低于60的学生姓名和分数
  15. select st.sname, sc.grade
  16. from t_score sc
  17. join t_student st
  18. on sc.sno = st.sno
  19. join t_course co
  20. on sc.cno = co.cno
  21. where sc.grade < 60
  22. and co.cname = 'JAVA程序设计';
  23. select st.sname, sc.grade
  24. from t_score sc
  25. join t_student st
  26. on sc.sno = st.sno
  27. join t_course co
  28. on sc.cno = co.cno
  29. and sc.grade < 60
  30. and co.cname = 'JAVA程序设计' ;
  31. select st.sname, sc.grade
  32. from t_score sc
  33. join t_student st
  34. on sc.sno = st.sno
  35. where sc.cno = (select cno from t_course where cname = 'JAVA程序设计')
  36. and sc.grade < 60
  37. --(四)查询任何一门课程成绩全部都在70分以上的姓名、课程名称和分数?
  38. --1.查询出成绩小于70分的学生的学号.
  39. --2.将学生,成绩,课程三张表作关联.
  40. --3.在关联表中过滤出不在步骤1查询结果中的学生信息.
  41. select st.sname, co.cname, sc.grade
  42. from t_student st
  43. join t_score sc
  44. on st.sno = sc.sno
  45. join t_course co
  46. on sc.cno = co.cno
  47. where st.sno not in (select sc1.sno from t_score sc1 where sc1.grade < 70);
  48. --(五)查询出选了课的学生的人数.
  49. select count(distinct(sc.sno)) from t_score sc;
  50. --(六)查询每门课程被选课的学生数
  51. select sc.cno, count(distinct(sc.sno)) from t_score sc group by sc.cno;
  52. --(七)查询选了全部课程的学员姓名和所属单位
  53. --1.在课程表中查询出所有课程的数量
  54. --2.在成绩表中查询出学生选课数等于步骤1中总选课数的学生的学号,注意要用distinct,having中可以使用count,where中不能使用count.
  55. --3.在学生表中查出步骤2中学生的基本信息.
  56. select st.sname, st.sdree
  57. from t_student st
  58. where st.sno in
  59. (select sc.sno
  60. from t_score sc
  61. group by sc.sno
  62. having count(distinct sc.cno) = (select count(distinct sc1.cno)
  63. from t_course sc1));
  64. --(八) 查询没有学全所有课的同学的学号、姓名
  65. select st.sname, st.sdree
  66. from t_student st
  67. where st.sno in
  68. (select sc.sno
  69. from t_score sc
  70. group by sc.sno
  71. having count(distinct sc.cno) != (select count(*) from t_course));
  72. --(九)查询选修课程超过5门的学员学号和所属单位
  73. --1.在成绩表中查询出选课程超过5门的学生学号.
  74. --2.在学生表中查询步骤1中学号的学生的基本信息.
  75. select st.sname, st.sdree
  76. from t_student st
  77. where st.sno in (select sc.sno
  78. from t_score sc
  79. group by sc.sno
  80. having count(distinct sc.cno) > 5);
  81. --(十)查询出没有选课的学生基本信息
  82. --1.在成绩表中查询出所有选过课的学生的学号.
  83. --2.在学生表中查询出步骤1中学生的基本信息.
  84. select *
  85. from t_student st
  86. where st.sno not in (select sc.sno from t_score sc);
  87. --下面的两个sql等价,在成绩表中数据量很大时使用下面的sql
  88. select *
  89. from t_student st
  90. where st.sno not in (select distinct (sc.sno) from t_score sc);
  91. select *
  92. from t_student st
  93. where st.sno not in (select sc.sno from t_score sc group by sc.sno);
  94. --(十一) 列出有二门以上不及格课程的学生姓名及其平均成绩
  95. --方法一
  96. --1.在成绩表中查询出2门不及格学生的学号,结果记作t1
  97. --2.将学生表和t1和成绩表三表作关联得到关联表,在关联表中取学生基本信息和平均成绩.
  98. --sql92
  99. select st.sno, st.sname, avg(sc.grade)
  100. from t_student st
  101. join (select sc.sno
  102. from t_score sc
  103. where sc.grade < 60
  104. group by sc.sno
  105. having count(distinct sc.cno) > 2) t1
  106. on st.sno = t1.sno
  107. join t_score sc
  108. on sc.sno = t1.sno
  109. group by st.sno, st.sname;
  110. --sql86
  111. select st.sno, st.sname, avg(sc.grade)
  112. from t_student st,
  113. t_score sc,
  114. (select sc.sno
  115. from t_score sc
  116. where sc.grade < 60
  117. group by sc.sno
  118. having count(distinct sc.cno) > 2) t1
  119. where st.sno = t1.sno
  120. and sc.sno = t1.sno
  121. group by st.sno, st.sname;
  122. --方法二:
  123. --1.在成绩表中查询出2门不及格学生的学号
  124. --2.将学生表和成绩表通过学号作关联并根据步骤1中的结果作过滤,在关联结果中取出学生基本信息和平均成绩
  125. select st.sno, st.sname, avg(sc.grade)
  126. from t_student st
  127. join t_score sc
  128. on st.sno = sc.sno
  129. where st.sno in (select sc.sno
  130. from t_score sc
  131. where sc.grade < 60
  132. group by sc.sno
  133. having count(distinct sc.cno) > 2)
  134. group by st.sno, st.sname;
  135. --(十二) 查询平均成绩大于60分的同学的学号和平均成绩
  136. --学生表和课程表关联,在having子句中过滤平均成绩大于60分.
  137. select st.sno, avg(sc.grade)
  138. from t_student st, t_score sc
  139. where st.sno = sc.sno
  140. group by st.sno
  141. having avg(sc.grade) > 60;
  142. --1.学生表和课程表关联,将关联的结果记作t1
  143. --2.在t1中过滤平均成绩大于60的学生学号.
  144. select t1.sno, t1.avg_grade
  145. from (select st.sno, avg(sc.grade) avg_grade
  146. from t_student st, t_score sc
  147. where st.sno = sc.sno
  148. group by st.sno) t1
  149. where t1.avg_grade > 60;
  150. --(十三)查询出只选修了一门课程的全部学生的学号和姓名
  151. --方法一:
  152. --1.将学生表和成绩表作关联,在分组函数中使用having子句过滤出只选了一门课程的学生基本信息.
  153. select sc.sno, st.sname
  154. from t_score sc
  155. join t_student st
  156. on sc.sno = st.sno
  157. group by sc.sno, st.sname
  158. having count(distinct sc.cno) = 1;
  159. --方法二:
  160. --1.在成绩表中查找学号,分组函数的过滤条件判断只选择了一门课程的学生.
  161. --2.在学生表中查找学号在步骤1中的值的学生的基本信息
  162. select st.sno,st.sname
  163. from t_student st
  164. where st.sno in (select sc.sno
  165. from t_score sc
  166. group by sc.sno
  167. having count(distinct sc.cno) = 1);
  168. --(十四)查询至少有一门课与学号为"1"的同学所学相同的同学的学号和姓名
  169. select st.sno, st.sname
  170. from t_student st
  171. join t_score sc1
  172. on st.sno = sc1.sno
  173. where sc1.cno in (select sc.cno from t_score sc where sc.sno = '1')
  174. group by st.sno, st.sname;
  175. --(十五)列出既学过"1"号课程,又学过"2"号课程的所有学生姓名
  176. --1.将成绩表和课程表作关联,在关联条件中作过滤查询出既选过课程'1'又选过课程'2'的学生的学号,注意看 co.cno in ('1', '2')和having count(distinct sc.cno) = 2 的位置.
  177. --2.在学生表中根据步骤1的结果作过滤查询出学生的基本信息.
  178. --方法一:
  179. --sql86
  180. select st.sno, st.sname
  181. from t_student st,
  182. (select sc.sno
  183. from t_score sc, t_course co
  184. where sc.cno = co.cno
  185. and co.cno in ('1', '2')
  186. group by sc.sno
  187. having count(distinct sc.cno) = 2) t1
  188. where st.sno = t1.sno;
  189. --sql92
  190. select st.sno, st.sname
  191. from t_student st join
  192. (select sc.sno
  193. from t_score sc join t_course co
  194. on sc.cno = co.cno
  195. and co.cno in ('1', '2')
  196. group by sc.sno
  197. having count(distinct sc.cno) = 2) t1
  198. on st.sno = t1.sno;
  199. --方法二:
  200. --sql86
  201. select st.sno, st.sname
  202. from t_student st
  203. where st.sno in (select sc.sno
  204. from t_score sc, t_course co
  205. where sc.cno = co.cno
  206. and co.cno in ('1', '2')
  207. group by sc.sno
  208. having count(distinct sc.cno) = 2);
  209. --sql92
  210. select st.sno, st.sname
  211. from t_student st
  212. where st.sno in (select sc.sno
  213. from t_score sc
  214. join t_course co
  215. on sc.cno = co.cno
  216. and co.cno in ('1', '2')
  217. group by sc.sno
  218. having count(distinct sc.cno) = 2);
  219. --(十六)查询至少学过学号为"1"的同学所有门课的同学学号和姓名
  220. --1.查询出'1'号同学学习的全部课程.
  221. --2.查询出'1'号同学学习全部课程的数量.
  222. --3.将课程表和成绩表做关联,在关联表中查询出学生的学号,关联条件中加入过滤条件[课程号在步骤1查询结果范围内],过滤条件中加入数量等级步骤2中得到的数量.
  223. --4.在学生表中查询步骤3中的学号的学生的基本信息.
  224. select st.sno, st.sname
  225. from t_student st
  226. where st.sno in
  227. (select sc.sno
  228. from t_score sc
  229. join t_course co
  230. on sc.cno = co.cno
  231. and co.cno in (select sc.cno from t_score sc where sc.sno = '1')
  232. group by sc.sno
  233. having count(distinct sc.cno) = (select count(distinct sc.cno)
  234. from t_score sc
  235. where sc.sno = '1'))
  236. --(十七)查询和"6"号同学学习的课程完全相同的同学的学号和姓名
  237. --分析:要查询与6号同学完全相同的课程的学生信息,等价于学过6号同学的学过的所有课程并且选课数量与6同学选课数量相等.
  238. --方法一:
  239. --1.查询出'1'号同学学习的全部课程.
  240. --2.查询出'1'号同学学习全部课程的数量.
  241. --3.将课程表和成绩表做关联,在关联表中查询出学生的学号和选课数量,记作 t2,关联条件中加入过滤条件[课程号在步骤1查询结果范围内],过滤条件中加入数量等级步骤2中得到的数量.
  242. --4.在成绩表中查询出学号和每个学生选课数量.得到结果记作: t1
  243. --5.将步骤3中的t2和步骤4中的t1通过学生学号关联,添加过滤条件,t1中的选课数量等于t2中的选课数量.
  244. --6.在学生表中查询不步骤5的学生学号的基本信息.
  245. select st.sno, st.sname
  246. from t_student st
  247. where st.sno in
  248. (select t1.sno
  249. from (select sc_a.sno, count(distinct sc_a.cno) num_outer
  250. from t_score sc_a
  251. group by sc_a.sno) t1
  252. join (select sc.sno, count(distinct sc.cno) num_inner
  253. from t_score sc
  254. join t_course co
  255. on sc.cno = co.cno
  256. and co.cno in
  257. (select sc.cno from t_score sc where sc.sno = '6')
  258. group by sc.sno
  259. having count(distinct sc.cno) = (select count(distinct sc.cno)
  260. from t_score sc
  261. where sc.sno = '6')) t2
  262. on t1.sno = t2.sno
  263. where t1.num_outer = t2.num_inner);
  264. --(十八)列出"1"号课成绩比"2"号课成绩高的所有学生的学号及其"1"号课和"2"号课的成绩
  265. --1.将学生表和课程表作两次关联,一次关联用于取该学生课程'1'的成绩,另一次关联用于取该学生课程'2'的成绩.
  266. --sql86
  267. select st.sno, st.sname, sc_a.grade, sc_b.grade
  268. from t_student st, t_score sc_a, t_score sc_b
  269. where sc_a.cno = '1'
  270. and sc_b.cno = '2'
  271. and st.sno = sc_a.sno
  272. and st.sno = sc_b.sno
  273. and sc_a.grade > sc_b.grade
  274. --sql92
  275. select st.sno, st.sname, sc_a.grade, sc_b.grade
  276. from t_student st
  277. join t_score sc_a
  278. on st.sno = sc_a.sno
  279. join t_score sc_b
  280. on st.sno = sc_b.sno
  281. where sc_a.cno = '1'
  282. and sc_b.cno = '2'
  283. and sc_a.grade > sc_b.grade
  284. --(十九)查询所有同学的学号、姓名、选课数、总成绩
  285. select st.sno, st.sname, count(sc.cno), sum(sc.grade)
  286. from t_student st, t_score sc
  287. where st.sno = sc.sno
  288. group by st.sno, st.sname;
  289. --(二十)查询课程成绩小于60分的同学的学号,姓名,课程名,成绩
  290. --下面两条sql虽然结果相同,但意义不同,注意理解一下哦.
  291. --1.将学生表,课程表,成绩表作关联
  292. --2.对关联后的结果作过滤,过滤出成绩小于60的学生基本信息.
  293. select st.sno, st.sname, co.cname, sc.grade
  294. from t_score sc
  295. join t_student st
  296. on sc.sno = st.sno
  297. join t_course co
  298. on sc.cno = co.cno
  299. where sc.grade < 60
  300. --1.将学生表,课程表,成绩表作关联,在关联条件中过滤成绩小于60.
  301. select st.sno, st.sname, co.cname, sc.grade
  302. from t_score sc
  303. join t_student st
  304. on sc.sno = st.sno
  305. join t_course co
  306. on sc.cno = co.cno
  307. and sc.grade <60
  308. --(二十一)按平均成绩从到低显示所有学生的"JAVA程序设计"、"J2EE从入门到精通"、"EJB及设计模式"三门的课程成绩,
  309. --并按如下形式显示: 学生ID,姓名,JAVA程序设计,J2EE从入门到精通,EJB及设计模式,有效课程数,有效课程平均分
  310. --1.将成绩表和课程表关联得到结果记作: t1, 关联时的条件选择只统计以上三门课程.
  311. --2.按题目中的要求组织统计结果.
  312. select st.sno,
  313. st.sname,
  314. sum(decode(t1.cname, 'JAVA程序设计', t1.grade)) JAVA程序设计,
  315. sum(decode(t1.cname, 'J2EE从入门到精通', t1.grade)) J2EE从入门到精通,
  316. sum(decode(t1.cname, 'EJB及设计模式', t1.grade)) EJB及设计模式,
  317. count(distinct t1.grade) 有效课程数,
  318. avg(t1.grade) 有效课程平均分
  319. from t_student st
  320. join (select *
  321. from t_score sc
  322. join t_course co
  323. on sc.cno = co.cno
  324. and co.cname in
  325. ('JAVA程序设计', 'J2EE从入门到精通', 'EJB及设计模式')) t1
  326. on st.sno = t1.sno
  327. group by st.sno, st.sname
  328. --将decode可以换成case when 第一种形式
  329. select st.sno,
  330. st.sname,
  331. sum(case t1.cname
  332. when 'JAVA程序设计' then
  333. t1.grade
  334. end) JAVA程序设计,
  335. sum(case t1.cname
  336. when 'J2EE从入门到精通' then
  337. t1.grade
  338. end) J2EE从入门到精通,
  339. sum(case t1.cname
  340. when 'EJB及设计模式' then
  341. t1.grade
  342. end) EJB及设计模式,
  343. count(distinct t1.grade) 有效课程数,
  344. avg(t1.grade) 有效课程平均分
  345. from t_student st
  346. join (select *
  347. from t_score sc
  348. join t_course co
  349. on sc.cno = co.cno
  350. and co.cname in
  351. ('JAVA程序设计', 'J2EE从入门到精通', 'EJB及设计模式')) t1
  352. on st.sno = t1.sno
  353. group by st.sno, st.sname
  354. --将decode可以换成case when 第二种形式
  355. select st.sno,
  356. st.sname,
  357. sum(case
  358. when t1.cname = 'JAVA程序设计' then
  359. t1.grade
  360. end) JAVA程序设计,
  361. sum(case
  362. when t1.cname = 'J2EE从入门到精通' then
  363. t1.grade
  364. end) J2EE从入门到精通,
  365. sum(case
  366. when t1.cname = 'EJB及设计模式' then
  367. t1.grade
  368. end) EJB及设计模式,
  369. count(distinct t1.grade) 有效课程数,
  370. avg(t1.grade) 有效课程平均分
  371. from t_student st
  372. join (select *
  373. from t_score sc
  374. join t_course co
  375. on sc.cno = co.cno
  376. and co.cname in
  377. ('JAVA程序设计', 'J2EE从入门到精通', 'EJB及设计模式')) t1
  378. on st.sno = t1.sno
  379. group by st.sno, st.sname
  380. --(二十二)查询各科成绩最高和最低的分:以如下形式显示:课程ID,课程名,最高分,最低分
  381. select sc.cno, co.cname, max(grade), min(grade)
  382. from t_score sc
  383. join t_course co
  384. on sc.cno = co.cno
  385. group by sc.cno, co.cname
  386. --(二十三)按各科平均成绩从低到高和及格率的百分数从高到低顺序
  387. --1.在成绩表中查出课程号,平均成绩,课程人数,记作 : t1
  388. --2.在成绩表中查出课程号,及格的课程人数,记作 : t2
  389. --3.将步骤1中的成绩和步骤2中的课程2关联,查出所要的结果并排序.
  390. select t1.cno, t1.avg_num 平均成绩, (count_num1 / count_num) * 100 及格率
  391. from (select sc.cno, avg(grade) avg_num, count(distinct sc.sno) count_num
  392. from t_score sc
  393. group by sc.cno) t1
  394. join (select sc1.cno, count(distinct sc1.sno) count_num1
  395. from t_score sc1
  396. where sc1.grade > 60
  397. group by sc1.cno) t2
  398. on t1.cno = t2.cno
  399. order by t1.avg_num asc, 及格率 desc
  400. --(二十四)统计各科成绩,各分数段人数:课程ID,课程名称,[100-90]优,[90-80]良,[80-70]中,[70-60]一般,[<60]不及格
  401. --1.在成绩表中根据成绩值分段
  402. --2.将步骤1中的结果与课程表关联.
  403. select sc.cno,co.cname,
  404. sum(case
  405. when sc.grade > 90 then
  406. 1
  407. end) 优,
  408. sum(case
  409. when sc.grade > 80 and sc.grade < 90 then
  410. 1
  411. end) 良,
  412. sum(case
  413. when sc.grade > 70 and sc.grade < 80 then
  414. 1
  415. end) 中,
  416. sum(case
  417. when sc.grade > 60 and sc.grade < 70 then
  418. 1
  419. end) 一般,
  420. sum(case
  421. when sc.grade < 60 then
  422. 1
  423. end) 不及格
  424. from t_score sc join t_course co on sc.cno = co.cno
  425. group by sc.cno,co.cname
  426. --(二十五)查询学生平均成绩及其名次
  427. select st.sno, st.sname, avg(sc.grade) avg_num
  428. from t_score sc
  429. join t_student st
  430. on sc.sno = st.sno
  431. group by st.sno, st.sname
  432. order by avg_num desc
  433. --(二十六)查询课程号分别为1,2,3的课程,成绩前三名的学生基本信息:(不考虑成绩并列情况)
  434. --方法一:
  435. --1.分别查出1,2,3各自的前3名的学生的学号,并用union all将结果集关联.
  436. --2.在学生表中查询步骤1中查到的id的学生的基本信息.
  437. select *
  438. from t_student
  439. where sno in (select t1.sno
  440. from (select sc1.*
  441. from t_score sc1
  442. where sc1.cno = 1
  443. order by sc1.grade desc) t1
  444. where rownum < 4
  445. union all
  446. select t1.sno
  447. from (select sc1.*
  448. from t_score sc1
  449. where sc1.cno = 2
  450. order by sc1.grade desc) t1
  451. where rownum < 4
  452. union all
  453. select t1.sno
  454. from (select sc1.*
  455. from t_score sc1
  456. where sc1.cno = 3
  457. order by sc1.grade desc) t1
  458. where rownum < 4)
  459. --方法二:
  460. --rank() over(Partition .. order by ...) 是按照某个字段的值进行分组并编号
  461. select t1.cno, t1.sno, t1.grade, r
  462. from (select sc.sno,
  463. sc.cno,
  464. sc.grade,
  465. rank() over(partition by sc.cno order by grade desc) r
  466. from t_score sc) t1
  467. where r < 4
  468. and t1.cno in (1, 2, 3)
  469. order by t1.cno, t1.sno, r;
  470. --(二十七)查询各科成绩前三名的记录(不考虑成绩并列情况)
  471. --rank() over(Partition .. order by ...) 是按照某个字段的值进行分组并编号
  472. select t1.cno, t1.sno, t1.grade, r
  473. from (select sc.sno,
  474. sc.cno,
  475. sc.grade,
  476. rank() over(partition by sc.cno order by grade desc) r
  477. from t_score sc) t1
  478. where r < 4
  479. order by t1.cno, t1.sno, r;


声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家自动化/article/detail/523926
推荐阅读
相关标签
  

闽ICP备14008679号