当前位置:   article > 正文

MySql经典面试题(含表)_max(case c.c when '03' then b.score else null end)

max(case c.c when '03' then b.score else null end)

创建数据库表

1.学生表

Student(SID,Sname,Sage,Ssex) --SID 学生编号,Sname 学生姓名,Sage 出生年月,Ssex 学生性别

create table Student(SID varchar(10),Sname nvarchar(10),Sage datetime,Ssex nvarchar(10));

2.课程表

Course(CID,Cname,TID) --CID --课程编号,Cname 课程名称,TID 教师编号

create table Course(CID varchar(10),Cname nvarchar(10),TID varchar(10));

3.教师表

Teacher(TID,Tname) --TID 教师编号,Tname 教师姓名

create table Teacher(TID varchar(10),Tname nvarchar(10));

4.成绩表

SC(SID,CID,score) --SID 学生编号,CID 课程编号,score 分数

create table SC(SID varchar(10),CID varchar(10),score decimal(18,1));

添加数据

1.学生表

  1. insert into Student values('01' , '张三' , '1990-01-01' , '男');
  2. insert into Student values('02' , '李四' , '1990-12-21' , '男');
  3. insert into Student values('03' , '王五' , '1990-05-20' , '男');
  4. insert into Student values('04' , '赵六' , '1990-08-06' , '男');
  5. insert into Student values('05' , '田七' , '1991-12-01' , '女');
  6. insert into Student values('06' , '孙八' , '1992-03-01' , '女');
  7. insert into Student values('07' , '汾九' , '1989-07-01' , '女');
  8. insert into Student values('08' , '郑十' , '1990-01-20' , '女');

2.课程表

  1. insert into Course values('01' , '语文' , '02');
  2. insert into Course values('02' , '数学' , '01');
  3. insert into Course values('03' , '英语' , '03');

3.教师表

  1. insert into Teacher values('01' , '李零');
  2. insert into Teacher values('02' , '刘一');
  3. insert into Teacher values('03' , '唐二');

4.成绩表

  1. insert into SC values('01' , '01' , 80);
  2. insert into SC values('01' , '02' , 90);
  3. insert into SC values('01' , '03' , 99);
  4. insert into SC values('02' , '01' , 70);
  5. insert into SC values('02' , '02' , 60);
  6. insert into SC values('02' , '03' , 80);
  7. insert into SC values('03' , '01' , 80);
  8. insert into SC values('03' , '02' , 80);
  9. insert into SC values('03' , '03' , 80);
  10. insert into SC values('04' , '01' , 50);
  11. insert into SC values('04' , '02' , 30);
  12. insert into SC values('04' , '03' , 20);
  13. insert into SC values('05' , '01' , 76);
  14. insert into SC values('05' , '02' , 87);
  15. insert into SC values('06' , '01' , 31);
  16. insert into SC values('06' , '03' , 34);
  17. insert into SC values('07' , '02' , 89);
  18. insert into SC values('07' , '03' , 98);

经典例题

  1. #1、查询"01"课程比"02"课程成绩高的学生的信息及课程分数
  2. ###1.1、查询同时存在"01"课程和"02"课程的情况
  3. SELECT a.*,b.score 01的成绩,c.score 02的成绩 from student a
  4. JOIN sc b on a.SID=b.SID and b.CID='01'
  5. JOIN sc c on a.SID=b.SID and c.CID='02'
  6. #1.2、查询同时存在"01"课程和"02"课程的情况和存在"01"课程但可能不存在"02"课程的情况(不存在时显示为null)(以下存在相同内容时不再解释)
  7. SELECT a.*,b.score 01的分数,c.score 02的分数 from student a
  8. LEFT JOIN sc b on a.SID=b.SID and b.CID='01'
  9. LEFT JOIN sc c on a.SID=c.SID and c.CID='02'
  10. where IFNULL(b.score,0)
  11. #2、查询"01"课程比"02"课程成绩低的学生的信息及课程分数
  12. #2.1、查询同时存在"01"课程和"02"课程的情况
  13. SELECT a.*,b.score 01分数,c.score 02分数 from student a,sc b,sc c
  14. where a.SID=b.SID and a.SID=c.SID and b.CID='01' and c.CID='02' and b.score<c.score
  15. #2.2、查询同时存在"01"课程和"02"课程的情况和不存在"01"课程但存在"02"课程的情况
  16. SELECT a.*,b.score 01的分数,c.score 02的分数 from student a
  17. LEFT JOIN sc b on a.SID=b.SID and b.CID='01'
  18. LEFT JOIN sc c on a.SID=b.SID and c.CID='02'
  19. where IFNULL(b.score,0)
  20. #3、查询平均成绩大于等于60分的同学的学生编号和学生姓名和平均成绩
  21. SELECT a.sid,a.Sname,cast(avg(b.score) as DECIMAL(18,2)) avg_score
  22. from student a,sc b
  23. where a.SID=b.SID
  24. GROUP BY a.SID,a.Sname
  25. HAVING cast(avg(b.score) as DECIMAL(18,2)) >60
  26. ORDER BY a.SID
  27. #4、查询平均成绩小于60分的同学的学生编号和学生姓名和平均成绩
  28. #4.1、查询在sc表存在成绩的学生信息的SQL语句。
  29. select a.SID , a.Sname , cast(avg(b.score) as decimal(18,2)) avg_score
  30. from Student a , sc b
  31. where a.SID = b.SID
  32. group by a.SID , a.Sname
  33. having cast(avg(b.score) as decimal(18,2)) < 60
  34. order by a.SID
  35. #4.2、查询在sc表中不存在成绩的学生信息的SQL语句。
  36. SELECT a.SID,a.Sname,IFNULL(CAST(AVG(b.score) AS DECIMAL(18,2)),0) avg_score from student a
  37. LEFT JOIN sc b on a.SID=b.SID
  38. GROUP BY a.SID,a.Sname
  39. HAVING IFNULL(CAST(AVG(b.score) AS DECIMAL(18,2)),0)<60
  40. ORDER BY a.SID
  41. #5、查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩
  42. #5.1、查询所有有成绩的SQL。
  43. SELECT a.SID 学生编号,a.Sname 学生姓名,count(b.SID) 选课总数,SUM(b.score) 所有课程的总成绩 from student a,sc b
  44. where a.SID=b.SID
  45. GROUP BY a.SID,a.Sname
  46. ORDER BY a.SID
  47. #5.2、查询所有(包括有成绩和无成绩)的SQL。
  48. SELECT a.SID 学生编号,a.Sname 学生姓名,count(b.SID) 选课总数,SUM(b.score) 所有课程的总成绩
  49. from student a
  50. LEFT JOIN sc b
  51. on a.SID=b.SID
  52. GROUP BY a.SID,a.Sname
  53. ORDER BY a.SID
  54. #6、查询"李"姓老师的数量
  55. #方法1
  56. SELECT count(Tname) from teacher where Tname LIKE '李%'
  57. ###方法2
  58. select count(Tname) 李姓老师的数量 from Teacher where left(Tname,1) = '李'
  59. #7、查询学过"李零"老师授课的同学的信息
  60. SELECT a.* from student a,teacher t ,course c,sc s
  61. where a.SID=s.SID and s.CID=c.CID and c.TID=t.TID and t.Tname='李零'
  62. #8、查询没学过"李零"老师授课的同学的信息
  63. sql1:
  64. select m.* from Student m where SID not in (select distinct SC.SID from SC , Course , Teacher where SC.CID = Course.CID and Course.TID = Teacher.TID and Teacher.Tname = '李零') order by m.SID
  65. sql2:
  66. SELECT * from student s where s.SID not in (SELECT s.SID FROM teacher t,course c,sc s where t.TID=c.TID and s.CID=c.CID and Tname ='李零')
  67. #9、查询学过编号为"01"并且也学过编号为"02"的课程的同学的信息
  68. #方法1
  69. select Student.* from Student , SC where Student.SID = SC.SID and SC.CID = '01' and exists (Select 1 from SC SC_2 where SC_2.SID = SC.SID and SC_2.CID = '02') order by Student.SID
  70. #方法2
  71. select Student.* from Student , SC where Student.SID = SC.SID and SC.CID = '02' and exists (Select 1 from SC SC_2 where SC_2.SID = SC.SID and SC_2.CID = '01') order by Student.SID
  72. #方法3
  73. select m.* from Student m where SID in
  74. (
  75. select SID from
  76. (
  77. select distinct SID from SC where CID = '01'
  78. union all
  79. select distinct SID from SC where CID = '02'
  80. ) t group by SID having count(1) = 2
  81. )
  82. order by m.SID
  83. #10、查询学过编号为"01"但是没有学过编号为"02"的课程的同学的信息
  84. #方法1
  85. select Student.* from Student , SC where Student.SID = SC.SID and SC.CID = '01' and not exists (Select 1 from SC SC_2 where SC_2.SID = SC.SID and SC_2.CID = '02') order by Student.SID
  86. #方法2
  87. select Student.* from Student , SC where Student.SID = SC.SID and SC.CID = '01' and Student.SID not in (Select SC_2.SID from SC SC_2 where SC_2.SID = SC.SID and SC_2.CID = '02') order by Student.SID
  88. #11、查询没有学全所有课程的同学的信息
  89. #11.1
  90. select Student.*
  91. from Student , SC
  92. where Student.SID = SC.SID
  93. group by Student.SID , Student.Sname , Student.Sage , Student.Ssex having count(CID) < (select count(CID) from Course)
  94. #11.2
  95. select Student.*
  96. from Student left join SC
  97. on Student.SID = SC.SID
  98. group by Student.SID , Student.Sname , Student.Sage , Student.Ssex having count(CID) < (select count(CID) from Course)
  99. #12、查询至少有一门课与学号为"01"的同学所学相同的同学的信息
  100. select distinct Student.* from Student , SC where Student.SID = SC.SID and SC.CID in (select CID from SC where SID = '01') and Student.SID <> '01'
  101. #13、查询和"01"号的同学学习的课程完全相同的其他同学的信息
  102. select Student.* from Student where SID in
  103. (select distinct SC.SID from SC where SID <> '01' and SC.CID in (select distinct CID from SC where SID = '01')
  104. group by SC.SID having count(1) = (select count(1) from SC where SID='01'))
  105. #14、查询没学过"李零"老师讲授的任一门课程的学生姓名
  106. select student.* from student where student.SID not in
  107. (select distinct sc.SID from sc , course , teacher where sc.CID = course.CID and course.TID = teacher.TID and teacher.tname = '李零')
  108. order by student.SID
  109. #15、查询两门及其以上不及格课程的同学的学号,姓名及其平均成绩
  110. select student.SID , student.sname , cast(avg(score) as decimal(18,2)) avg_score from student , sc
  111. where student.SID = SC.SID and student.SID in (select SID from SC where score < 60 group by SID having count(1) >= 2)
  112. group by student.SID , student.sname
  113. #16、检索"01"课程分数小于60,按分数降序排列的学生信息
  114. select student.* , sc.CID , sc.score from student , sc
  115. where student.SID = SC.SID and sc.score < 60 and sc.CID = '01'
  116. order by sc.score desc
  117. #17、按平均成绩从高到低显示所有学生的所有课程的成绩以及平均成绩#17.1 SQL 2000 静态
  118. select a.SID 学生编号 , a.Sname 学生姓名 ,
  119. max(case c.Cname when '语文' then b.score else null end) 语文 ,
  120. max(case c.Cname when '数学' then b.score else null end) 数学 ,
  121. max(case c.Cname when '英语' then b.score else null end) 英语 ,
  122. cast(avg(b.score) as decimal(18,2)) 平均分
  123. from Student a
  124. left join SC b on a.SID = b.SID
  125. left join Course c on b.CID = c.CID
  126. group by a.SID , a.Sname
  127. order by 平均分 desc
  128. #17.2 SQL 2000 动态
  129. declare @sql nvarchar(4000)
  130. set @sql = 'select a.SID ' + '学生编号' + ' , a.Sname ' + '学生姓名'
  131. select @sql = @sql + ',max(case c.Cname when '''+Cname+''' then b.score else null end) '+Cname+' '
  132. from (select distinct Cname from Course) as t
  133. set @sql = @sql + ' , cast(avg(b.score) as decimal(18,2)) ' + '平均分' + ' from Student a left join SC b on a.SID = b.SID left join Course c on b.CID = c.CID
  134. group by a.SID , a.Sname order by ' + '平均分' + ' desc'
  135. exec(@sql)
  136. #18、查询各科成绩最高分、最低分和平均分:以如下形式显示:课程ID,课程name,最高分,最低分,平均分,及格率,中等率,优良率,优秀率
  137. #及格为>=60,中等为:70-80,优良为:80-90,优秀为:>=90
  138. #方法1
  139. select m.CID 课程编号 , m.Cname 课程名称 ,
  140. max(n.score) 最高分 ,
  141. min(n.score) 最低分 ,
  142. cast(avg(n.score) as decimal(18,2)) 平均分 ,
  143. cast((select count(1) from SC where CID = m.CID and score >= 60)*100.0 / (select count(1) from SC where CID = m.CID) as decimal(18,2)) 及格率 ,
  144. cast((select count(1) from SC where CID = m.CID and score >= 70 and score < 80 )*100.0 / (select count(1) from SC where CID = m.CID) as decimal(18,2)) 中等率 ,
  145. cast((select count(1) from SC where CID = m.CID and score >= 80 and score < 90 )*100.0 / (select count(1) from SC where CID = m.CID) as decimal(18,2)) 优良率 ,
  146. cast((select count(1) from SC where CID = m.CID and score >= 90)*100.0 / (select count(1) from SC where CID = m.CID) as decimal(18,2)) 优秀率
  147. from Course m , SC n
  148. where m.CID = n.CID
  149. group by m.CID , m.Cname
  150. order by m.CID
  151. #方法2
  152. select m.CID 课程编号 , m.Cname 课程名称 ,
  153. (select max(score) from SC where CID = m.CID) 最高分 ,
  154. (select min(score) from SC where CID = m.CID) 最低分 ,
  155. (select cast(avg(score) as decimal(18,2)) from SC where CID = m.CID) 平均分 ,
  156. cast((select count(1) from SC where CID = m.CID and score >= 60)*100.0 / (select count(1) from SC where CID = m.CID) as decimal(18,2)) 及格率,
  157. cast((select count(1) from SC where CID = m.CID and score >= 70 and score < 80 )*100.0 / (select count(1) from SC where CID = m.CID) as decimal(18,2)) 中等率 ,
  158. cast((select count(1) from SC where CID = m.CID and score >= 80 and score < 90 )*100.0 / (select count(1) from SC where CID = m.CID) as decimal(18,2)) 优良率 ,
  159. cast((select count(1) from SC where CID = m.CID and score >= 90)*100.0 / (select count(1) from SC where CID = m.CID) as decimal(18,2)) 优秀率
  160. from Course m
  161. order by m.CID
  162. #19、按各科成绩进行排序,并显示排名#19.1 sql 2000用子查询完成
  163. #Score重复时保留名次空缺
  164. select t.* , px = (select count(1) from SC where CID = t.CID and score > t.score) + 1 from sc t order by t.cid , px
  165. #Score重复时合并名次
  166. select t.* , px = (select count(distinct score) from SC where CID = t.CID and score >= t.score) from sc t order by t.cid , px
  167. #19.2 sql 2005用rank,DENSE_RANK完成
  168. #Score重复时保留名次空缺(rank完成)
  169. select t.* , px = rank() over(partition by cid order by score desc) from sc t order by t.CID , px
  170. #Score重复时合并名次(DENSE_RANK完成)
  171. select t.* , px = DENSE_RANK() over(partition by cid order by score desc) from sc t order by t.CID , px
  172. #20、查询学生的总成绩并进行排名#20.1 查询学生的总成绩
  173. select m.SID 学生编号 ,
  174. m.Sname 学生姓名 ,
  175. isnull(sum(score),0) 总成绩
  176. from Student m left join SC n on m.SID = n.SID
  177. group by m.SID , m.Sname
  178. order by 总成绩 desc
  179. #20.2 查询学生的总成绩并进行排名,sql 2000用子查询完成,分总分重复时保留名次空缺和不保留名次空缺两种。
  180. select t1.* , px = (select count(1) from
  181. (
  182. select m.SID 学生编号 ,
  183. m.Sname 学生姓名 ,
  184. isnull(sum(score),0) 总成绩
  185. from Student m left join SC n on m.SID = n.SID
  186. group by m.SID , m.Sname
  187. ) t2 where 总成绩 > t1.总成绩) + 1 from
  188. (
  189. select m.SID 学生编号 ,
  190. m.Sname 学生姓名 ,
  191. isnull(sum(score),0) 总成绩
  192. from Student m left join SC n on m.SID = n.SID
  193. group by m.SID , m.Sname
  194. ) t1
  195. order by px
  196. select t1.* , px = (select count(distinct 总成绩) from
  197. (
  198. select m.SID 学生编号 ,
  199. m.Sname 学生姓名 ,
  200. isnull(sum(score),0) 总成绩
  201. from Student m left join SC n on m.SID = n.SID
  202. group by m.SID , m.Sname
  203. ) t2 where 总成绩 >= t1.总成绩) from
  204. (
  205. select m.SID 学生编号 ,
  206. m.Sname 学生姓名 ,
  207. isnull(sum(score),0) 总成绩
  208. from Student m left join SC n on m.SID = n.SID
  209. group by m.SID , m.Sname
  210. ) t1
  211. order by px
  212. #20.3 查询学生的总成绩并进行排名,sql 2005用rank,DENSE_RANK完成,分总分重复时保留名次空缺和不保留名次空缺两种。
  213. select t.* , px = rank() over(order by 总成绩 desc) from
  214. (
  215. select m.SID 学生编号 ,
  216. m.Sname 学生姓名 ,
  217. isnull(sum(score),0) 总成绩
  218. from Student m left join SC n on m.SID = n.SID
  219. group by m.SID , m.Sname
  220. ) t
  221. order by px
  222. select t.* , px = DENSE_RANK() over(order by 总成绩 desc) from
  223. (
  224. select m.SID 学生编号 ,
  225. m.Sname 学生姓名 ,
  226. isnull(sum(score),0) 总成绩
  227. from Student m left join SC n on m.SID = n.SID
  228. group by m.SID , m.Sname
  229. ) t
  230. order by px
  231. #21、查询不同老师所教不同课程平均分从高到低显示
  232. select m.TID , m.Tname , cast(avg(o.score) as decimal(18,2)) avg_score
  233. from Teacher m , Course n , SC o
  234. where m.TID = n.TID and n.CID = o.CID
  235. group by m.TID , m.Tname
  236. order by avg_score desc
  237. #22、查询所有课程的成绩第2名到第3名的学生信息及该课程成绩#22.1 sql 2000用子查询完成
  238. #Score重复时保留名次空缺
  239. select * from (select t.* , px = (select count(1) from SC where CID = t.CID and score > t.score) + 1 from sc t) m where px between 2 and 3 order by m.cid , m.px
  240. #Score重复时合并名次
  241. select * from (select t.* , px = (select count(distinct score) from SC where CID = t.CID and score >= t.score) from sc t) m where px between 2 and 3 order by m.cid , m.px
  242. #22.2 sql 2005用rank,DENSE_RANK完成
  243. #Score重复时保留名次空缺(rank完成)
  244. select * from (select t.* , px = rank() over(partition by cid order by score desc) from sc t) m where px between 2 and 3 order by m.CID , m.px
  245. #Score重复时合并名次(DENSE_RANK完成)
  246. select * from (select t.* , px = DENSE_RANK() over(partition by cid order by score desc) from sc t) m where px between 2 and 3 order by m.CID , m.px
  247. #23、统计各科成绩各分数段人数:课程编号,课程名称, 100-85 , 85-70 , 70-60 , 0-60 及所占百分比 #23.1 统计各科成绩各分数段人数:课程编号,课程名称, 100-85 , 85-70 , 70-60 , 0-60
  248. #横向显示
  249. select Course.CID 课程编号 , Cname as 课程名称 ,
  250. sum(case when score >= 85 then 1 else 0 end) 85-100 ,
  251. sum(case when score >= 70 and score < 85 then 1 else 0 end) 70-85 ,
  252. sum(case when score >= 60 and score < 70 then 1 else 0 end) 60-70 ,
  253. sum(case when score < 60 then 1 else 0 end) 0-60
  254. from sc , Course
  255. where SC.CID = Course.CID
  256. group by Course.CID , Course.Cname
  257. order by Course.CID
  258. #纵向显示1(显示存在的分数段)
  259. select m.CID 课程编号 , m.Cname 课程名称 , 分数段 = (
  260. case when n.score >= 85 then '85-100'
  261. when n.score >= 70 and n.score < 85 then '70-85'
  262. when n.score >= 60 and n.score < 70 then '60-70'
  263. else '0-60'
  264. end) ,
  265. count(1) 数量
  266. from Course m , sc n
  267. where m.CID = n.CID
  268. group by m.CID , m.Cname , (
  269. case when n.score >= 85 then '85-100'
  270. when n.score >= 70 and n.score < 85 then '70-85'
  271. when n.score >= 60 and n.score < 70 then '60-70'
  272. else '0-60'
  273. end)
  274. order by m.CID , m.Cname , 分数段
  275. #纵向显示2(显示存在的分数段,不存在的分数段用0显示)
  276. select m.CID 课程编号 , m.Cname 课程名称 , 分数段 = (
  277. case when n.score >= 85 then '85-100'
  278. when n.score >= 70 and n.score < 85 then '70-85'
  279. when n.score >= 60 and n.score < 70 then '60-70'
  280. else '0-60'
  281. end) ,
  282. count(1) 数量
  283. from Course m , sc n
  284. where m.CID = n.CID
  285. group by all m.CID , m.Cname , (
  286. case when n.score >= 85 then '85-100'
  287. when n.score >= 70 and n.score < 85 then '70-85'
  288. when n.score >= 60 and n.score < 70 then '60-70'
  289. else '0-60'
  290. end)
  291. order by m.CID , m.Cname , 分数段
  292. #23.2 统计各科成绩各分数段人数:课程编号,课程名称, 100-85 , 85-70 , 70-60 , <60 及所占百分比
  293. #横向显示
  294. select m.CID 课程编号, m.Cname 课程名称,
  295. (select count(1) from SC where CID = m.CID and score < 60) 0-60 ,
  296. cast((select count(1) from SC where CID = m.CID and score < 60)*100.0 / (select count(1) from SC where CID = m.CID) as decimal(18,2)) 百分比 ,
  297. (select count(1) from SC where CID = m.CID and score >= 60 and score < 70) 60-70 ,
  298. cast((select count(1) from SC where CID = m.CID and score >= 60 and score < 70)*100.0 / (select count(1) from SC where CID = m.CID) as decimal(18,2)) 百分比 ,
  299. (select count(1) from SC where CID = m.CID and score >= 70 and score < 85) 70-85 ,
  300. cast((select count(1) from SC where CID = m.CID and score >= 70 and score < 85)*100.0 / (select count(1) from SC where CID = m.CID) as decimal(18,2)) 百分比 ,
  301. (select count(1) from SC where CID = m.CID and score >= 85) 85-100 ,
  302. cast((select count(1) from SC where CID = m.CID and score >= 85)*100.0 / (select count(1) from SC where CID = m.CID) as decimal(18,2)) 百分比
  303. from Course m
  304. order by m.CID
  305. #纵向显示1(显示存在的分数段)
  306. select m.CID 课程编号 , m.Cname 课程名称 , 分数段 = (
  307. case when n.score >= 85 then '85-100'
  308. when n.score >= 70 and n.score < 85 then '70-85'
  309. when n.score >= 60 and n.score < 70 then '60-70'
  310. else '0-60'
  311. end) ,
  312. count(1) 数量 ,
  313. cast(count(1) * 100.0 / (select count(1) from sc where CID = m.CID) as decimal(18,2)) 百分比
  314. from Course m , sc n
  315. where m.CID = n.CID
  316. group by m.CID , m.Cname , (
  317. case when n.score >= 85 then '85-100'
  318. when n.score >= 70 and n.score < 85 then '70-85'
  319. when n.score >= 60 and n.score < 70 then '60-70'
  320. else '0-60'
  321. end)
  322. order by m.CID , m.Cname , 分数段
  323. #纵向显示2(显示存在的分数段,不存在的分数段用0显示)
  324. select m.CID 课程编号 , m.Cname 课程名称 , 分数段 = (
  325. case when n.score >= 85 then '85-100'
  326. when n.score >= 70 and n.score < 85 then '70-85'
  327. when n.score >= 60 and n.score < 70 then '60-70'
  328. else '0-60'
  329. end) ,
  330. count(1) 数量 ,
  331. cast(count(1) * 100.0 / (select count(1) from sc where CID = m.CID) as decimal(18,2)) 百分比
  332. from Course m , sc n
  333. where m.CID = n.CID
  334. group by all m.CID , m.Cname , (
  335. case when n.score >= 85 then '85-100'
  336. when n.score >= 70 and n.score < 85 then '70-85'
  337. when n.score >= 60 and n.score < 70 then '60-70'
  338. else '0-60'
  339. end)
  340. order by m.CID , m.Cname , 分数段
  341. #24、查询学生平均成绩及其名次#24.1 查询学生的平均成绩并进行排名,sql 2000用子查询完成,分平均成绩重复时保留名次空缺和不保留名次空缺两种。
  342. select t1.* , px = (select count(1) from
  343. (
  344. select m.SID 学生编号 ,
  345. m.Sname 学生姓名 ,
  346. isnull(cast(avg(score) as decimal(18,2)),0) 平均成绩
  347. from Student m left join SC n on m.SID = n.SID
  348. group by m.SID , m.Sname
  349. ) t2 where 平均成绩 > t1.平均成绩) + 1 from
  350. (
  351. select m.SID 学生编号 ,
  352. m.Sname 学生姓名 ,
  353. isnull(cast(avg(score) as decimal(18,2)),0) 平均成绩
  354. from Student m left join SC n on m.SID = n.SID
  355. group by m.SID , m.Sname
  356. ) t1
  357. order by px
  358. select t1.* , px = (select count(distinct 平均成绩) from
  359. (
  360. select m.SID 学生编号 ,
  361. m.Sname 学生姓名 ,
  362. isnull(cast(avg(score) as decimal(18,2)),0) 平均成绩
  363. from Student m left join SC n on m.SID = n.SID
  364. group by m.SID , m.Sname
  365. ) t2 where 平均成绩 >= t1.平均成绩) from
  366. (
  367. select m.SID 学生编号 ,
  368. m.Sname 学生姓名 ,
  369. isnull(cast(avg(score) as decimal(18,2)),0) 平均成绩
  370. from Student m left join SC n on m.SID = n.SID
  371. group by m.SID , m.Sname
  372. ) t1
  373. order by px
  374. #24.2 查询学生的平均成绩并进行排名,sql 2005用rank,DENSE_RANK完成,分平均成绩重复时保留名次空缺和不保留名次空缺两种。
  375. select t.* , px = rank() over(order by 平均成绩 desc) from
  376. (
  377. select m.SID 学生编号 ,
  378. m.Sname 学生姓名 ,
  379. isnull(cast(avg(score) as decimal(18,2)),0) 平均成绩
  380. from Student m left join SC n on m.SID = n.SID
  381. group by m.SID , m.Sname
  382. ) t
  383. order by px
  384. select t.* , px = DENSE_RANK() over(order by 平均成绩 desc) from
  385. (
  386. select m.SID 学生编号 ,
  387. m.Sname 学生姓名 ,
  388. isnull(cast(avg(score) as decimal(18,2)),0) 平均成绩
  389. from Student m left join SC n on m.SID = n.SID
  390. group by m.SID , m.Sname
  391. ) t
  392. order by px
  393. #25、查询各科成绩前三名的记录#25.1 分数重复时保留名次空缺
  394. select m.* , n.CID , n.score from Student m, SC n where m.SID = n.SID and n.score in
  395. (select top 3 score from sc where CID = n.CID order by score desc) order by n.CID , n.score desc
  396. #25.2 分数重复时不保留名次空缺,合并名次
  397. #sql 2000用子查询实现
  398. select * from (select t.* , px = (select count(distinct score) from SC where CID = t.CID and score >= t.score) from sc t) m where px between 1 and 3 order by m.Cid , m.px
  399. #sql 2005用DENSE_RANK实现
  400. select * from (select t.* , px = DENSE_RANK() over(partition by Cid order by score desc) from sc t) m where px between 1 and 3 order by m.CID , m.px
  401. #26、查询每门课程被选修的学生数
  402. select Cid , count(SID) 学生数 from sc group by CID
  403. #27、查询出只有两门课程的全部学生的学号和姓名
  404. select Student.SID , Student.Sname
  405. from Student , SC
  406. where Student.SID = SC.SID
  407. group by Student.SID , Student.Sname
  408. having count(SC.CID) = 2
  409. order by Student.SID
  410. #28、查询男生、女生人数
  411. select count(Ssex) as 男生人数 from Student where Ssex = N'男'
  412. select count(Ssex) as 女生人数 from Student where Ssex = N'女'
  413. select sum(case when Ssex = N'男' then 1 else 0 end) 男生人数 ,sum(case when Ssex = N'女' then 1 else 0 end) 女生人数 from student
  414. select case when Ssex = N'男' then N'男生人数' else N'女生人数' end 男女情况 , count(1) 人数 from student group by case when Ssex = N'男' then N'男生人数' else N'女生人数' end
  415. #29、查询名字中含有"风"字的学生信息
  416. select * from student where sname like N'%风%'
  417. select * from student where charindex(N'风' , sname) > 0
  418. #30、查询同名同性学生名单,并统计同名人数
  419. select Sname 学生姓名 , count(*) 人数 from Student group by Sname having count(*) > 1
  420. #31、查询1990年出生的学生名单(注:Student表中Sage列的类型是datetime)
  421. select * from Student where year(sage) = 1990
  422. select * from Student where datediff(yy,sage,'1990-01-01') = 0
  423. select * from Student where datepart(yy,sage) = 1990
  424. select * from Student where convert(varchar(4),sage,120) = '1990'
  425. #32、查询每门课程的平均成绩,结果按平均成绩降序排列,平均成绩相同时,按课程编号升序排列
  426. select m.CID , m.Cname , cast(avg(n.score) as decimal(18,2)) avg_score
  427. from Course m, SC n
  428. where m.CID = n.CID
  429. group by m.CID , m.Cname
  430. order by avg_score desc, m.CID asc
  431. #33、查询平均成绩大于等于85的所有学生的学号、姓名和平均成绩
  432. select a.SID , a.Sname , cast(avg(b.score) as decimal(18,2)) avg_score
  433. from Student a , sc b
  434. where a.SID = b.SID
  435. group by a.SID , a.Sname
  436. having cast(avg(b.score) as decimal(18,2)) >= 85
  437. order by a.SID
  438. #34、查询课程名称为"数学",且分数低于60的学生姓名和分数
  439. select sname , score
  440. from Student , SC , Course
  441. where SC.SID = Student.SID and SC.CID = Course.CID and Course.Cname = N'数学' and score < 60
  442. #35、查询所有学生的课程及分数情况
  443. select Student.* , Course.Cname , SC.CID , SC.score
  444. from Student, SC , Course
  445. where Student.SID = SC.SID and SC.CID = Course.CID
  446. order by Student.SID , SC.CID
  447. #36、查询任何一门课程成绩在70分以上的姓名、课程名称和分数
  448. select Student.* , Course.Cname , SC.CID , SC.score
  449. from Student, SC , Course
  450. where Student.SID = SC.SID and SC.CID = Course.CID and SC.score >= 70
  451. order by Student.SID , SC.CID
  452. #37、查询不及格的课程
  453. select Student.* , Course.Cname , SC.CID , SC.score
  454. from Student, SC , Course
  455. where Student.SID = SC.SID and SC.CID = Course.CID and SC.score < 60
  456. order by Student.SID , SC.CID
  457. #38、查询课程编号为01且课程成绩在80分以上的学生的学号和姓名
  458. select Student.* , Course.Cname , SC.CID , SC.score
  459. from Student, SC , Course
  460. where Student.SID = SC.SID and SC.CID = Course.CID and SC.CID = '01' and SC.score >= 80
  461. order by Student.SID , SC.CID
  462. #39、求每门课程的学生人数
  463. select Course.CID , Course.Cname , count(*) 学生人数
  464. from Course , SC
  465. where Course.CID = SC.CID
  466. group by Course.CID , Course.Cname
  467. order by Course.CID , Course.Cname
  468. #40、查询选修"李零"老师所授课程的学生中,成绩最高的学生信息及其成绩#40.1 当最高分只有一个时
  469. select top 1 Student.* , Course.Cname , SC.CID , SC.score
  470. from Student, SC , Course , Teacher
  471. where Student.SID = SC.SID and SC.CID = Course.CID and Course.TID = Teacher.TID and Teacher.Tname = N'李零'
  472. order by SC.score desc
  473. #40.2 当最高分出现多个时
  474. select Student.* , Course.Cname , SC.CID , SC.score
  475. from Student, SC , Course , Teacher
  476. where Student.SID = SC.SID and SC.CID = Course.CID and Course.TID = Teacher.TID and Teacher.Tname = N'李零' and
  477. SC.score = (select max(SC.score) from SC , Course , Teacher where SC.CID = Course.CID and Course.TID = Teacher.TID and Teacher.Tname = N'李零')
  478. #41、查询不同课程成绩相同的学生的学生编号、课程编号、学生成绩
  479. #方法1
  480. select m.* from SC m ,(select CID , score from SC group by CID , score having count(1) > 1) n
  481. where m.CID= n.CID and m.score = n.score order by m.CID , m.score , m.SID
  482. #方法2
  483. select m.* from SC m where exists (select 1 from (select CID , score from SC group by CID , score having count(1) > 1) n
  484. where m.CID= n.CID and m.score = n.score) order by m.CID , m.score , m.SID
  485. #42、查询每门功成绩最好的前两名
  486. select t.* from sc t where score in (select top 2 score from sc where CID = T.CID order by score desc) order by t.CID , t.score desc
  487. #43、统计每门课程的学生选修人数(超过5人的课程才统计)。要求输出课程号和选修人数,查询结果按人数降序排列,若人数相同,按课程号升序排列
  488. select Course.CID , Course.Cname , count(*) 学生人数
  489. from Course , SC
  490. where Course.CID = SC.CID
  491. group by Course.CID , Course.Cname
  492. having count(*) >= 5
  493. order by 学生人数 desc , Course.CID
  494. #44、检索至少选修两门课程的学生学号
  495. select student.SID , student.Sname
  496. from student , SC
  497. where student.SID = SC.SID
  498. group by student.SID , student.Sname
  499. having count(1) >= 2
  500. order by student.SID
  501. #45、查询选修了全部课程的学生信息
  502. #方法1 根据数量来完成
  503. select student.* from student where SID in
  504. (select SID from sc group by SID having count(1) = (select count(1) from course))
  505. #方法2 使用双重否定来完成
  506. select t.* from student t where t.SID not in
  507. (
  508. select distinct m.SID from
  509. (
  510. select SID , CID from student , course
  511. ) m where not exists (select 1 from sc n where n.SID = m.SID and n.CID = m.CID)
  512. )
  513. #方法3 使用双重否定来完成
  514. select t.* from student t where not exists(select 1 from
  515. (
  516. select distinct m.SID from
  517. (
  518. select SID , CID from student , course
  519. ) m where not exists (select 1 from sc n where n.SID = m.SID and n.CID = m.CID)
  520. ) k where k.SID = t.SID
  521. )
  522. #46、查询各学生的年龄#46.1 只按照年份来算
  523. select * , datediff(yy , sage , getdate()) 年龄 from student
  524. #46.2 按照出生日期来算,当前月日 < 出生年月的月日则,年龄减一
  525. select * , case when right(convert(varchar(10),getdate(),120),5) < right(convert(varchar(10),sage,120),5) then datediff(yy , sage , getdate()) - 1 else datediff(yy , sage , getdate()) end 年龄 from student
  526. #47、查询本周过生日的学生
  527. select * from student where datediff(week,datename(yy,getdate()) + right(convert(varchar(10),sage,120),6),getdate()) = 0
  528. #48、查询下周过生日的学生
  529. select * from student where datediff(week,datename(yy,getdate()) + right(convert(varchar(10),sage,120),6),getdate()) = -1
  530. #49、查询本月过生日的学生
  531. select * from student where datediff(mm,datename(yy,getdate()) + right(convert(varchar(10),sage,120),6),getdate()) = 0
  532. #50、查询下月过生日的学生
  533. select * from student where datediff(mm,datename(yy,getdate()) + right(convert(varchar(10),sage,120),6),getdate()) = -1

 

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

闽ICP备14008679号