50条常用SQL语句

[cpp]  view plain copy
  1. /*student(学号#,姓名,性别,年龄) 
  2. course(课程号#,课程名,教师号#) 
  3. score(学号#,课程号#,成绩) 
  4. teacher(教师号#,教师名)*/  
  5. --1.查询“001”课程比“002”课程成绩高的所有学生的学号  
  6. select a.stuNo from score a,score b  
  7. where a.cNo='c001' and b.cNo='c002' and a.stuNo=b.stuNo and a.score>b.score  
  8. --2.查询平均成绩大于60分的同学的学号和平均成绩  
  9. select stuNo,avg(score)from score   
  10. group by stuNo  
  11. having avg(score)>60  
  12. --3.查询所有同学的学号、姓名、选课数、总成绩  
  13. select a.stuNo,a.stuName,count(cNo),sum(score) from student a,score b  
  14. where a.stuNo=b.stuNo  
  15. group by a.stuNo,a.stuName  
  16. --4.查询姓“赵”的老师的个数  
  17. select count(tName),tName from teacher  
  18. where tName like '赵%'  
  19. group by tName   
  20. --5.查询没学过“某某”老师课的同学的学号、姓名  
  21. select stuNo,stuName from student  
  22. where stuNo not in   
  23. (select a.stuNo from student a,score b where a.stuNo=b.stuNo and cNo in   
  24. (select d.cNo from teacher c,course d where c.tNo=d.tNo and c.tName='钱市保'))  
  25. --6.查询学过“001”并且也学过编号“002”课程的同学的学号、姓名;  
  26. select a.stuNo,a.stuName from student a,score b,score c  
  27. where a.stuNo=b.stuNo and b.stuNo=c.stuNo and b.cNo='c001' and c.cNo='c002'   
  28. --7.查询学过“某某”老师所教的所有课的同学的学号、姓名  
  29. select stuNo,stuName from student  
  30. where stuNo in (select stuNo from score a,course b,teacher c  
  31. where a.cNo=b.cNo and b.tNo=c.tNo and c.tName='钱市保'  
  32. group by stuNo               
  33. having count(a.cNo)>=(select count(cNo) from course d,teacher e  
  34. where d.tNo=e.tNo and e.tName='钱市保'))  
  35. --老师所教课程为一门课  
  36. select stuNo,stuName from student  
  37. where stuNo in (select a.stuNo from student a,score b where a.stuNo=b.stuNo and b.cNo in   
  38. (select cNo from teacher c,course d where c.tNo=d.tNo and c.tName='钱市保'))  
  39. --8.查询课程编号“002”的成绩比课程编号“001”课程低的所有同学的学号、姓名  
  40. select stuNo,stuName from student  
  41. where stuNo in   
  42. (select a.stuNo from score a,score b  
  43. where a.cNo='c001' and b.cNo='c002' and a.stuNo=b.stuNo and a.score>b.score)  
  44. --9.查询所有课程成绩小于60分的同学的学号、姓名  
  45. select stuNo,stuName from student   
  46. where stuNo in (select stuNo from score   
  47. where score<60  
  48. group by stuNo   
  49. having count(cNo)=(select count(cNo) from course))  
  50. --10.查询没有学全所有课的同学的学号、姓名  
  51. select b.stuNo,a.stuName,count(b.cNo) from student a,score b  
  52. where a.stuNo=b.stuNo  
  53. group by b.stuNo,a.stuName  
  54. having count(b.cNo)<(select count(cNo) from course)  
  55. --11.查询至少有一门课与学号为“1”的同学所学相同的同学的学号和姓名  
  56. select distinct a.stuNo,stuName from student a,score b  
  57. where a.stuNo=b.stuNo and cNo in (select cNo from score   
  58. where stuNo='001')  
  59. --12.查询至少学过学号为“001”同学一门课的其他同学学号和姓名  
  60. &&& select distinct a.stuNo,stuName from student a,score b  
  61. where a.stuNo=b.stuNo and cNo all join (select cNo from score   
  62. where stuNo='001')  
  63. --13.把“SC”表中“某某”老师教的课的成绩都更改为此课程的平均成绩  
  64. update score set score=savg  
  65. from score d,(select avg(score) as savg,a.cNo from score a,course b,teacher c  
  66. where a.cNo=b.cNo and b.tNo=c.tNo and tName='钱市保'  
  67. group by a.cNo) e  
  68. where d.cNo=e.cNo   
  69. --老师所教课程为一门课  
  70. update score   
  71. set score=(select avg(score) from score  
  72. group by cNo  
  73. having cNo=(select a.cNo from course a,teacher b where a.tNo=b.tNo and b.tName='钱市保'))  
  74. where cNo=(select a.cNo from course a,teacher b where a.tNo=b.tNo and b.tName='钱市保')  
  75. select * from score  
  76. --14.查询和“001”号的同学学习的课程完全相同的其他同学学号和姓名  
  77. select stuNo from score   
  78. where cNo in (select cNo from score where stuNo='005')  
  79. group by stuNo  
  80. having count(cNo)=(select count(*) from score where stuNo='005')  
  81. --15.删除学习“某某”老师课的SC表记录  
  82. delete from score where cNo=(select cNo from course a,teacher b where a.tNo=b.tNo and b.tName='钱市保')  
  83. select * from score   
  84. --16.向SC表中插入一些记录,这些记录要求符合以下条件:没有上过编号“003”课程的同学学号、2号课的平均成绩  
  85. --17.按平均成绩从高到低显示所有学生的“C语言”、“sql”、“java”三门的课程成绩  
  86. --按如下形式显示: 学生ID,C语言,sql,JAVA,有效课程数,有效平均分  
  87. --18.查询各科成绩最高和最低的分:以如下形式显示:课程ID,最高分,最低分  
  88. select  cNo,max(score) as 最高分,min(score) as 最低分 from score  
  89. group by cNo  
  90. --19.按各科平均成绩从低到高和及格率的百分数从高到低顺序  
  91. select avg(c.score),count(a.score)/count(b.score) from score c,(select a.cNo,count(a.score) from score a  
  92. where a.score<60  
  93. group by a.cNo) d,(select b.cNo,count(b.score) from score b  
  94. group by b.cNo) e  
  95. where d.cNo=e.cNo  
  96. group by c.cNo  
  97. order by avg(c.score) desc  
  98. (select a.cNo,count(a.score) from score a  
  99. where a.score<60  
  100. group by a.cNo) d  
  101. (select b.cNo,count(b.score) from score b  
  102. group by b.cNo) e  
  103. --20.查询如下课程平均成绩和及格率的百分数(用"1行"显示): C语言(001),数据结构(002),JAVA(003),离散数学(004)   
  104. --21.查询不同老师所教不同课程平均分从高到低显示   
  105. select tNo,a.cNo,avg(score) from course a,score b  
  106. where a.cNo=b.cNo  
  107. group by tNo,a.cNo  
  108. order by avg(score) desc  
  109. --22.查询如下课程成绩第 3 名到第 6 名的学生成绩单:C语言(001),数据结构(002),JAVA(003),离散数学(004)   
  110. --   [学生ID],[学生姓名],C语言,数据结构,JAVA,离散数学,平均成绩  
  111. --23.统计列印各科成绩,各分数段人数:课程ID,课程名称,[100-85],[85-70],[70-60],[ <60]  
  112. select distinct e.cNo,count(a.stuNo) as '100-85',count(b.stuNo) as '85-70',count(c.stuNo) as '70-60',count(d.stuNo) as '<60' from score a,score b,score c,score d,score e  
  113. where a.cNo in (select cNo from course) and a.score between 85 and 100 and b.cNo in (select cNo from course) and b.score between 71 and 84 and c.cNo in (select cNo from course) and  c.score between 60 and 70 and d.cNo in (select cNo from course) and d.score<60  
  114. group by e.cNo,a.stuNo,b.stuNo,c.stuNo,d.stuNo  
  115. having a.stuNo<>b.stuNo and a.stuNo<>c.stuNo and a.stuNo<>d.stuNo and b.stuNo<>c.stuNo and b.stuNo<>d.stuNo and c.stuNo<>d.stuNo  
  116. select cNo,count(stuNo) from score  
  117. where score between 70 and 100 and cNo='c001'  
  118. group by cNo  
  119. --24.查询学生平均成绩及其名次  
  120. select stuNo,avg(score) from score   
  121. group by stuNo  
  122. order by avg(score) desc  
  123. --25.查询各科成绩前三名的记录:(不考虑成绩并列情况)   
  124. select a.stuNo,a.cNo,a.score  
  125. from score a  
  126. where a.score in (select top 3 score from score b  
  127. where a.cNo=b.cNo  
  128. order by score)  
  129. order by a.cNo  
  130. --26.查询每门课程被选修的学生数   
  131. select b.cNo ,count(stuNo) from score a right join course  b  
  132. on a.cNo=b.cNo  
  133. group by b.cNo  
  134. --27.查询出只选修了一门课程的全部学生的学号和姓名   
  135. select b.stuNo,a.stuName from student a,score b  
  136. where a.stuNo=b.stuNo   
  137. group by b.stuNo,a.stuName  
  138. having count(b.cNo)=1  
  139. --28.查询男生、女生人数  
  140. select stuSex,count(stuSex) from student  
  141. group by stuSex  
  142. --29.查询姓‘zhao’的学生名单  
  143. select * from student   
  144. where stuName like '赵%'  
  145.    
  146. --30.查询同名同性学生名单,并统计同名人数  
  147. select a.stuNo,a.stuName,count(a.stuNo) from student a,student b  
  148. where a.stuName=b.stuName and a.stuSex=b.stuSex and a.stuNo<>b.stuNo  
  149. group by a.stuNo,a.stuName  
  150.    
  151. --31.1981年出生的学生名单(注:Student表中Sage列的类型是datetime  
  152. --32.、查询每门课程的平均成绩,结果按平均成绩升序排列,平均成绩相同时,按课程号降序排列   
  153. select cNo,avg(score) from score  
  154. group by cNo  
  155. order by avg(score) asc,cNo  
  156. --33.查询平均成绩大于70的所有学生的学号、姓名和平均成绩  
  157. select b.stuNo,a.stuName,avg(score) from student a,score b  
  158. where a.stuNo=b.stuNo  
  159. group by b.stuNo,a.stuName  
  160. having avg(score)>70  
  161. --34.查询课程名称为“java”,且分数低于70的学生姓名和分数  
  162. select a.stuName,b.score from student a,score b  
  163. where a.stuNo=b.stuNo and score<70 and b.cNo=(select cNo from course where cName='java')  
  164. --35.查询所有学生的选课情况  
  165. select a.stuNo,c.cNo from student a,score b,course c  
  166. where a.stuNo=b.stuNo and b.cNo=c.cNo  
  167. order by a.stuNo  
  168. select a.stuNo,cNo from student a left join (select a.stuNo,c.cNo from student a,score b,course c  
  169. where a.stuNo=b.stuNo and b.cNo=c.cNo) d  
  170. on a.stuNo=d.stuNo  
  171. order by a.stuNo  
  172. --36.查询任何一门课程成绩在70分以上的姓名、课程名称和分数  
  173. select a.stuName,b.cNo,score from student a,score b  
  174. where score>70 and a.stuNo=b.stuNo  
  175. --37.查询不及格的课程,并按课程号从大到小排列  
  176. select cNo,score from score  
  177. where score<60  
  178. order by cNo  
  179. --38.查询课程编号为003且课程成绩在60分以上的学生的学号和姓名  
  180. select b.stuNo,a.stuName from student a,score b  
  181. where b.cNo='c003' and score>60 and a.stuNo=b.stuNo  
  182. --39.求选了课程的学生人数  
  183. select count(a.stuNo) from (select distinct stuNo from score) a  
  184. --40.查询选修“赵”老师所授课程的学生中,成绩最高的学生姓名及其成绩  
  185. select b.stuNo,a.stuName,max(score) from student a,score b  
  186. where a.stuNo=b.stuNo and b.cNo in (select a.cNo from course a,teacher b where a.tNo=b.tNo and b.tName='钱市保')  
  187. group by b.stuNo,a.stuName,b.cNo  
  188. having b.cNo in (select a.cNo from course a,teacher b where a.tNo=b.tNo and b.tName='钱市保')  
  189. --41.查询各个课程及相应的选修人数  
  190. select cNo,count(stuNo) from score  
  191. group by cNo  
  192. select b.cNo ,count(stuNo) from score a right join course  b  
  193. on a.cNo=b.cNo  
  194. group by b.cNo  
  195. --42.查询不同课程成绩相同的学生的学号、课程号、学生成绩  
  196. select a.stuNo,a.cNo,a.score from score a,score b   
  197. where a.stuNo=b.stuNo and a.score=b.score and a.cNo<>b.cNo   
  198. --43. 查询每门功成绩最好的前两名  
  199. select a.stuNo,a.cNo,a.score  
  200. from score a  
  201. where score in(select top 2 score from score b  
  202. where a.cNo=b.cNo  
  203. order by score desc)  
  204. order by a.cNo  
  205. --44.统计每门课程的学生选修人数(超过2人的课程才统计)。要求输出课程号和选修人数,查询结果按人数降序排列,若人数相同,按课程号升序排列  
  206. select cNo,count(stuNo) 课程数 from score  
  207. group by cNo  
  208. having count(stuNo)>2  
  209. order by count(stuNo) desc,cNo  
  210. --45.检索至少选修两门课程的学生学号  
  211. select stuNo from score  
  212. group by stuNo  
  213. having count(cNo)>=2  
  214. --46.查询全部学生都选修的课程的课程号和课程名  
  215. select a.cNo,b.cName from score a,course b  
  216. where a.cNo=b.cNo  
  217. group by a.cNo,b.cName  
  218. having count(a.stuNo)=(select count(stuNo) from student)  
  219. select a.cNo,b.cName from score a,course b  
  220. group by a.cNo,b.cName,b.cNo  
  221. having a.cNo=b.cNo and count(a.stuNo)=(select count(stuNo) from student)  
  222. --47.查询没学过“钱”老师讲授的任一门课程的学生姓名  
  223. select stuNo,stuName from student  
  224. where stuNo not in (select stuNo from score a,course b,teacher c  
  225. where a.cNo=b.cNo and b.tNo=c.tNo and c.tName='钱市保'  
  226. group by stuNo  
  227. having count(a.cNo)<=(select count(cNo) from course d,teacher e  
  228. where d.tNo=e.tNo and e.tName='钱市保'))  
  229. select stuNo,stuName from student  
  230. where stuNo not in  
  231. (select stuNo from score where cNo in  
  232. (select cNo from teacher c,course d where c.tNo=d.tNo and c.tName='钱市保'))  
  233. --48.查询两门以上不及格课程的同学的学号及其平均成绩  
  234. select stuNo,avg(score) from score  
  235. where score<60  
  236. group by stuNo  
  237. having count(cNo)>2  
  238. --49.检索“004”课程分数小于60,按分数降序排列的同学学号   
  239. select stuNo from score   
  240. where score<60 and cNo='c004'  
  241. order by score desc  
  242. --50.删除“2”同学的“001”课程的成绩  
  243. delete from score where stuNo='002' and cNo='c001'   
  • 3
    点赞
  • 29
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值