MySql 语句练习50题

表名和字段

–1.学生表

Student(s_id,s_name,s_birth,s_sex) –学生编号,学生姓名, 出生年月,学生性别

–2.课程表

Course(c_id,c_name,t_id) – –课程编号, 课程名称, 教师编号

–3.教师表

Teacher(t_id,t_name) –教师编号,教师姓名

–4.成绩表

Score(s_id,c_id,s_score) –学生编号,课程编号,分数

测试数据

  1. --建表
  2. --学生表
  3. CREATE TABLE `Student`(
  4. `s_id` VARCHAR(20),
  5. `s_name` VARCHAR(20) NOT NULL DEFAULT '',
  6. `s_birth` VARCHAR(20) NOT NULL DEFAULT '',
  7. `s_sex` VARCHAR(10) NOT NULL DEFAULT '',
  8. PRIMARY KEY(`s_id`)
  9. );
  10. --课程表
  11. CREATE TABLE `Course`(
  12. `c_id` VARCHAR(20),
  13. `c_name` VARCHAR(20) NOT NULL DEFAULT '',
  14. `t_id` VARCHAR(20) NOT NULL,
  15. PRIMARY KEY(`c_id`)
  16. );
  17. --教师表
  18. CREATE TABLE `Teacher`(
  19. `t_id` VARCHAR(20),
  20. `t_name` VARCHAR(20) NOT NULL DEFAULT '',
  21. PRIMARY KEY(`t_id`)
  22. );
  23. --成绩表
  24. CREATE TABLE `Score`(
  25. `s_id` VARCHAR(20),
  26. `c_id` VARCHAR(20),
  27. `s_score` INT(3),
  28. PRIMARY KEY(`s_id`,`c_id`)
  29. );
  30. --插入学生表测试数据
  31. insert into Student values('01' , '赵雷' , '1990-01-01' , '男');
  32. insert into Student values('02' , '钱电' , '1990-12-21' , '男');
  33. insert into Student values('03' , '孙风' , '1990-05-20' , '男');
  34. insert into Student values('04' , '李云' , '1990-08-06' , '男');
  35. insert into Student values('05' , '周梅' , '1991-12-01' , '女');
  36. insert into Student values('06' , '吴兰' , '1992-03-01' , '女');
  37. insert into Student values('07' , '郑竹' , '1989-07-01' , '女');
  38. insert into Student values('08' , '王菊' , '1990-01-20' , '女');
  39. --课程表测试数据
  40. insert into Course values('01' , '语文' , '02');
  41. insert into Course values('02' , '数学' , '01');
  42. insert into Course values('03' , '英语' , '03');
  43. --教师表测试数据
  44. insert into Teacher values('01' , '张三');
  45. insert into Teacher values('02' , '李四');
  46. insert into Teacher values('03' , '王五');
  47. --成绩表测试数据
  48. insert into Score values('01' , '01' , 80);
  49. insert into Score values('01' , '02' , 90);
  50. insert into Score values('01' , '03' , 99);
  51. insert into Score values('02' , '01' , 70);
  52. insert into Score values('02' , '02' , 60);
  53. insert into Score values('02' , '03' , 80);
  54. insert into Score values('03' , '01' , 80);
  55. insert into Score values('03' , '02' , 80);
  56. insert into Score values('03' , '03' , 80);
  57. insert into Score values('04' , '01' , 50);
  58. insert into Score values('04' , '02' , 30);
  59. insert into Score values('04' , '03' , 20);
  60. insert into Score values('05' , '01' , 76);
  61. insert into Score values('05' , '02' , 87);
  62. insert into Score values('06' , '01' , 31);
  63. insert into Score values('06' , '03' , 34);
  64. insert into Score values('07' , '02' , 89);
  65. insert into Score values('07' , '03' , 98);

表数据如下

student 学生表:

s_id s_name s_birth s_sex
01 赵雷 1990-01-01
02 钱电 1990-12-21
03 孙凤 1990-05-20
04 李云 1990-08-06
05 周梅 1991-12-12
06 吴兰 2017-12-13
07 郑竹 1989-07-01
08 王菊 1990-01-20
09 赵雷 1990-01-21
10 赵雷 1990-01-22

score 分数表:

s_id c_id s_score
01 01 80
01 02 90
01 03 99
02 01 70
02 02 60
02 03 80
03 01 80
03 02 80
03 03 80
04 01 50
04 02 30
04 03 20
05 01 76
05 03 87
06 01 31
06 03 34
07 03 89
07 01 98

course 课程表

c_id c_name t_id
01 语文 02
02 数学 01
03 英语 03

teacher 老师表:

t_id t_name
01 张三
02 李四
03 王五
  1. -- 准备条件,去掉 sql_mode ONLY_FULL_GROUP_BY 否则此种情况下会报错:
  2. -- Expression #1 of select list is not in group by clause and contains nonaggregated column 'userinfo.
  3. -- 原因:
  4. -- MySQL 5.7.5up实现了对功能依赖的检测。如果启用了only_full_group_by SQL模式(在默认情况下是这样),
  5. -- 那么MySQL就会拒绝选择列表、条件或顺序列表引用的查询,这些查询将引用组中未命名的非聚合列,而不是在功能上依赖于它们。
  6. -- (在5.7.5之前,MySQL没有检测到功能依赖项,only_full_group_by在默认情况下是不启用的。关于前5.7.5行为的描述,请参阅MySQL 5.6参考手册。)
  7. -- 执行以下个命令,可以查看 sql_mode 的内容。
  8. SHOW SESSION VARIABLES;
  9. SHOW GLOBAL VARIABLES;
  10. select @@sql_mode;
  11. -- 更改
  12. set global sql_mode='STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION';
  13. set session sql_mode='STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION';

练习题和sql

  1. -- 1、查询"01"课程比"02"课程成绩高的学生的信息及课程分数
  2. select st.*,sc.s_score as '语文' ,sc2.s_score '数学'
  3. from student st
  4. left join score sc on sc.s_id=st.s_id and sc.c_id='01'
  5. left join score sc2 on sc2.s_id=st.s_id and sc2.c_id='02'
  6. where sc.s_score>sc2.s_score
  7. -- 2、查询"01"课程比"02"课程成绩低的学生的信息及课程分数
  8. select st.*,sc.s_score '语文',sc2.s_score '数学' from student st
  9. left join score sc on sc.s_id=st.s_id and sc.c_id='01'
  10. left join score sc2 on sc2.s_id=st.s_id and sc2.c_id='02'
  11. where sc.s_score<sc2.s_score
  12. -- 3、查询平均成绩大于等于60分的同学的学生编号和学生姓名和平均成绩
  13. select st.s_id,st.s_name,ROUND(AVG(sc.s_score),2) cjScore from student st
  14. left join score sc on sc.s_id=st.s_id
  15. group by st.s_id having AVG(sc.s_score)>=60
  16. -- 4、查询平均成绩小于60分的同学的学生编号和学生姓名和平均成绩
  17. -- (包括有成绩的和无成绩的)
  18. select st.s_id,st.s_name,(case when ROUND(AVG(sc.s_score),2) is null then 0 else ROUND(AVG(sc.s_score)) end ) cjScore from student st
  19. left join score sc on sc.s_id=st.s_id
  20. group by st.s_id having AVG(sc.s_score)<60 or AVG(sc.s_score) is NULL
  21. -- 5、查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩
  22. select st.s_id,st.s_name,count(c.c_id),( case when SUM(sc.s_score) is null or sum(sc.s_score)="" then 0 else SUM(sc.s_score) end) from student st
  23. left join score sc on sc.s_id =st.s_id
  24. left join course c on c.c_id=sc.c_id
  25. group by st.s_id
  26. -- 6、查询"李"姓老师的数量
  27. select t.t_name,count(t.t_id) from teacher t
  28. group by t.t_id having t.t_name like "李%";
  29. -- 7、查询学过"张三"老师授课的同学的信息
  30. select st.* from student st
  31. left join score sc on sc.s_id=st.s_id
  32. left join course c on c.c_id=sc.c_id
  33. left join teacher t on t.t_id=c.t_id
  34. where t.t_name="张三"
  35. -- 8、查询没学过"张三"老师授课的同学的信息
  36. -- 张三老师教的课
  37. select c.* from course c left join teacher t on t.t_id=c.t_id where t.t_name="张三"
  38. -- 有张三老师课成绩的st.s_id
  39. select sc.s_id from score sc where sc.c_id in (select c.c_id from course c left join teacher t on t.t_id=c.t_id where t.t_name="张三")
  40. -- 不在上面查到的st.s_id的学生信息,即没学过张三老师授课的同学信息
  41. select st.* from student st where st.s_id not in(
  42. select sc.s_id from score sc where sc.c_id in (select c.c_id from course c left join teacher t on t.t_id=c.t_id where t.t_name="张三")
  43. )
  44. -- 9、查询学过编号为"01"并且也学过编号为"02"的课程的同学的信息
  45. select st.* from student st
  46. inner join score sc on sc.s_id = st.s_id
  47. inner join course c on c.c_id=sc.c_id and c.c_id="01"
  48. where st.s_id in (
  49. select st2.s_id from student st2
  50. inner join score sc2 on sc2.s_id = st2.s_id
  51. inner join course c2 on c2.c_id=sc2.c_id and c2.c_id="02"
  52. )
  53. 网友提供的思路(厉害呦~):
  54. SELECT st.*
  55. FROM student st
  56. INNER JOIN score sc ON sc.`s_id`=st.`s_id`
  57. GROUP BY st.`s_id`
  58. HAVING SUM(IF(sc.`c_id`="01" OR sc.`c_id`="02" ,1,0))>1
  59. -- 10、查询学过编号为"01"但是没有学过编号为"02"的课程的同学的信息
  60. select st.* from student st
  61. inner join score sc on sc.s_id = st.s_id
  62. inner join course c on c.c_id=sc.c_id and c.c_id="01"
  63. where st.s_id not in (
  64. select st2.s_id from student st2
  65. inner join score sc2 on sc2.s_id = st2.s_id
  66. inner join course c2 on c2.c_id=sc2.c_id and c2.c_id="02"
  67. )
  68. -- 11、查询没有学全所有课程的同学的信息
  69. -- 太复杂,下次换一种思路,看有没有简单点方法
  70. -- 此处思路为查学全所有课程的学生id,再内联取反面
  71. select * from student where s_id not in (
  72. select st.s_id from student st
  73. inner join score sc on sc.s_id = st.s_id and sc.c_id="01"
  74. where st.s_id in (
  75. select st2.s_id from student st2
  76. inner join score sc2 on sc2.s_id = st2.s_id and sc2.c_id="02"
  77. ) and st.s_id in (
  78. select st2.s_id from student st2
  79. inner join score sc2 on sc2.s_id = st2.s_id and sc2.c_id="03"
  80. ))
  81. -- 来自一楼网友的思路,左连接,根据学生id分组过滤掉 数量小于 课程表中总课程数量的结果(show me his code),简洁不少。
  82. select st.* from Student st
  83. left join Score S
  84. on st.s_id = S.s_id
  85. group by st.s_id
  86. having count(c_id)<(select count(c_id) from Course)
  87. -- 12、查询至少有一门课与学号为"01"的同学所学相同的同学的信息
  88. select distinct st.* from student st
  89. left join score sc on sc.s_id=st.s_id
  90. where sc.c_id in (
  91. select sc2.c_id from student st2
  92. left join score sc2 on sc2.s_id=st2.s_id
  93. where st2.s_id ='01'
  94. )
  95. -- 13、查询和"01"号的同学学习的课程完全相同的其他同学的信息
  96. select st.* from student st
  97. left join score sc on sc.s_id=st.s_id
  98. group by st.s_id
  99. having group_concat(sc.c_id) =
  100. (
  101. select group_concat(sc2.c_id) from student st2
  102. left join score sc2 on sc2.s_id=st2.s_id
  103. where st2.s_id ='01'
  104. )
  105. -- 14、查询没学过"张三"老师讲授的任一门课程的学生姓名
  106. select st.s_name from student st
  107. where st.s_id not in (
  108. select sc.s_id from score sc
  109. inner join course c on c.c_id=sc.c_id
  110. inner join teacher t on t.t_id=c.t_id and t.t_name="张三"
  111. )
  112. -- 15、查询两门及其以上不及格课程的同学的学号,姓名及其平均成绩
  113. select st.s_id,st.s_name,avg(sc.s_score) from student st
  114. left join score sc on sc.s_id=st.s_id
  115. where sc.s_id in (
  116. select sc.s_id from score sc
  117. where sc.s_score<60 or sc.s_score is NULL
  118. group by sc.s_id having COUNT(sc.s_id)>=2
  119. )
  120. group by st.s_id
  121. -- 16、检索"01"课程分数小于60,按分数降序排列的学生信息
  122. select st.*,sc.s_score from student st
  123. inner join score sc on sc.s_id=st.s_id and sc.c_id="01" and sc.s_score<60
  124. order by sc.s_score desc
  125. -- 17、按平均成绩从高到低显示所有学生的所有课程的成绩以及平均成绩
  126. -- 可加round,case when then else end 使显示更完美
  127. select st.s_id,st.s_name,avg(sc4.s_score) "平均分",sc.s_score "语文",sc2.s_score "数学",sc3.s_score "英语" from student st
  128. left join score sc on sc.s_id=st.s_id and sc.c_id="01"
  129. left join score sc2 on sc2.s_id=st.s_id and sc2.c_id="02"
  130. left join score sc3 on sc3.s_id=st.s_id and sc3.c_id="03"
  131. left join score sc4 on sc4.s_id=st.s_id
  132. group by st.s_id
  133. order by SUM(sc4.s_score) desc
  134. -- 18.查询各科成绩最高分、最低分和平均分:以如下形式显示:课程ID,课程name,最高分,最低分,平均分,及格率,中等率,优良率,优秀率
  135. -- 及格为>=60,中等为:70-80,优良为:80-90,优秀为:>=90
  136. select c.c_id,c.c_name,max(sc.s_score) "最高分",MIN(sc2.s_score) "最低分",avg(sc3.s_score) "平均分"
  137. ,((select count(s_id) from score where s_score>=60 and c_id=c.c_id )/(select count(s_id) from score where c_id=c.c_id)) "及格率"
  138. ,((select count(s_id) from score where s_score>=70 and s_score<80 and c_id=c.c_id )/(select count(s_id) from score where c_id=c.c_id)) "中等率"
  139. ,((select count(s_id) from score where s_score>=80 and s_score<90 and c_id=c.c_id )/(select count(s_id) from score where c_id=c.c_id)) "优良率"
  140. ,((select count(s_id) from score where s_score>=90 and c_id=c.c_id )/(select count(s_id) from score where c_id=c.c_id)) "优秀率"
  141. from course c
  142. left join score sc on sc.c_id=c.c_id
  143. left join score sc2 on sc2.c_id=c.c_id
  144. left join score sc3 on sc3.c_id=c.c_id
  145. group by c.c_id
  146. -- 19、按各科成绩进行排序,并显示排名(实现不完全)
  147. -- mysql没有rank函数
  148. -- @score是为了防止用union all 后打乱了顺序
  149. select c1.s_id,c1.c_id,c1.c_name,@score:=c1.s_score,@i:=@i+1 from (select c.c_name,sc.* from course c
  150. left join score sc on sc.c_id=c.c_id
  151. where c.c_id="01" order by sc.s_score desc) c1 ,
  152. (select @i:=0) a
  153. union all
  154. select c2.s_id,c2.c_id,c2.c_name,c2.s_score,@ii:=@ii+1 from (select c.c_name,sc.* from course c
  155. left join score sc on sc.c_id=c.c_id
  156. where c.c_id="02" order by sc.s_score desc) c2 ,
  157. (select @ii:=0) aa
  158. union all
  159. select c3.s_id,c3.c_id,c3.c_name,c3.s_score,@iii:=@iii+1 from (select c.c_name,sc.* from course c
  160. left join score sc on sc.c_id=c.c_id
  161. where c.c_id="03" order by sc.s_score desc) c3;
  162. set @iii=0;
  163. -- 20、查询学生的总成绩并进行排名
  164. select st.s_id,st.s_name
  165. ,(case when sum(sc.s_score) is null then 0 else sum(sc.s_score) end)
  166. from student st
  167. left join score sc on sc.s_id=st.s_id
  168. group by st.s_id order by sum(sc.s_score) desc
  169. -- 21、查询不同老师所教不同课程平均分从高到低显示
  170. select t.t_id,t.t_name,c.c_name,avg(sc.s_score) from teacher t
  171. left join course c on c.t_id=t.t_id
  172. left join score sc on sc.c_id =c.c_id
  173. group by t.t_id
  174. order by avg(sc.s_score) desc
  175. -- 22、查询所有课程的成绩第2名到第3名的学生信息及该课程成绩
  176. select a.* from (
  177. select st.*,c.c_id,c.c_name,sc.s_score from student st
  178. left join score sc on sc.s_id=st.s_id
  179. inner join course c on c.c_id =sc.c_id and c.c_id="01"
  180. order by sc.s_score desc LIMIT 1,2 ) a
  181. union all
  182. select b.* from (
  183. select st.*,c.c_id,c.c_name,sc.s_score from student st
  184. left join score sc on sc.s_id=st.s_id
  185. inner join course c on c.c_id =sc.c_id and c.c_id="02"
  186. order by sc.s_score desc LIMIT 1,2) b
  187. union all
  188. select c.* from (
  189. select st.*,c.c_id,c.c_name,sc.s_score from student st
  190. left join score sc on sc.s_id=st.s_id
  191. inner join course c on c.c_id =sc.c_id and c.c_id="03"
  192. order by sc.s_score desc LIMIT 1,2) c
  193. -- 23、统计各科成绩各分数段人数:课程编号,课程名称,[100-85],[85-70],[70-60],[0-60]及所占百分比
  194. select c.c_id,c.c_name
  195. ,((select count(1) from score sc where sc.c_id=c.c_id and sc.s_score<=100 and sc.s_score>80)/(select count(1) from score sc where sc.c_id=c.c_id )) "100-85"
  196. ,((select count(1) from score sc where sc.c_id=c.c_id and sc.s_score<=85 and sc.s_score>70)/(select count(1) from score sc where sc.c_id=c.c_id )) "85-70"
  197. ,((select count(1) from score sc where sc.c_id=c.c_id and sc.s_score<=70 and sc.s_score>60)/(select count(1) from score sc where sc.c_id=c.c_id )) "70-60"
  198. ,((select count(1) from score sc where sc.c_id=c.c_id and sc.s_score<=60 and sc.s_score>=0)/(select count(1) from score sc where sc.c_id=c.c_id )) "60-0"
  199. from course c order by c.c_id
  200. -- 24、查询学生平均成绩及其名次
  201. set @i=0;
  202. select a.*,@i:=@i+1 from (
  203. select st.s_id,st.s_name,round((case when avg(sc.s_score) is null then 0 else avg(sc.s_score) end),2) "平均分" from student st
  204. left join score sc on sc.s_id=st.s_id
  205. group by st.s_id order by sc.s_score desc) a
  206. -- 25、查询各科成绩前三名的记录
  207. select a.* from (
  208. select st.s_id,st.s_name,c.c_id,c.c_name,sc.s_score from student st
  209. left join score sc on sc.s_id=st.s_id
  210. inner join course c on c.c_id=sc.c_id and c.c_id='01'
  211. order by sc.s_score desc LIMIT 0,3) a
  212. union all
  213. select b.* from (
  214. select st.s_id,st.s_name,c.c_id,c.c_name,sc.s_score from student st
  215. left join score sc on sc.s_id=st.s_id
  216. inner join course c on c.c_id=sc.c_id and c.c_id='02'
  217. order by sc.s_score desc LIMIT 0,3) b
  218. union all
  219. select c.* from (
  220. select st.s_id,st.s_name,c.c_id,c.c_name,sc.s_score from student st
  221. left join score sc on sc.s_id=st.s_id
  222. inner join course c on c.c_id=sc.c_id and c.c_id='03'
  223. order by sc.s_score desc LIMIT 0,3) c
  224. -- 26、查询每门课程被选修的学生数
  225. select c.c_id,c.c_name,count(1) from course c
  226. left join score sc on sc.c_id=c.c_id
  227. inner join student st on st.s_id=c.c_id
  228. group by st.s_id
  229. -- 27、查询出只有两门课程的全部学生的学号和姓名
  230. select st.s_id,st.s_name from student st
  231. left join score sc on sc.s_id=st.s_id
  232. inner join course c on c.c_id=sc.c_id
  233. group by st.s_id having count(1)=2
  234. -- 28、查询男生、女生人数
  235. select st.s_sex,count(1) from student st group by st.s_sex
  236. -- 29、查询名字中含有"风"字的学生信息
  237. select st.* from student st where st.s_name like "%风%";
  238. -- 30、查询同名同性学生名单,并统计同名人数
  239. select st.*,count(1) from student st group by st.s_name,st.s_sex having count(1)>1
  240. -- 31、查询1990年出生的学生名单
  241. select st.* from student st where st.s_birth like "1990%";
  242. -- 32、查询每门课程的平均成绩,结果按平均成绩降序排列,平均成绩相同时,按课程编号升序排列
  243. select c.c_id,c.c_name,avg(sc.s_score) from course c
  244. inner join score sc on sc.c_id=c.c_id
  245. group by c.c_id order by avg(sc.s_score) desc,c.c_id asc
  246. -- 33、查询平均成绩大于等于85的所有学生的学号、姓名和平均成绩
  247. select st.s_id,st.s_name,avg(sc.s_score) from student st
  248. left join score sc on sc.s_id=st.s_id
  249. group by st.s_id having avg(sc.s_score)>=85
  250. -- 34、查询课程名称为"数学",且分数低于60的学生姓名和分数
  251. select st.s_id,st.s_name,sc.s_score from student st
  252. inner join score sc on sc.s_id=st.s_id and sc.s_score<60
  253. inner join course c on c.c_id=sc.c_id and c.c_name ="数学"
  254. -- 35、查询所有学生的课程及分数情况;
  255. select st.s_id,st.s_name,c.c_name,sc.s_score from student st
  256. left join score sc on sc.s_id=st.s_id
  257. left join course c on c.c_id =sc.c_id
  258. order by st.s_id,c.c_name
  259. -- 36、查询任何一门课程成绩在70分以上的姓名、课程名称和分数
  260. select st2.s_id,st2.s_name,c2.c_name,sc2.s_score from student st2
  261. left join score sc2 on sc2.s_id=st2.s_id
  262. left join course c2 on c2.c_id=sc2.c_id
  263. where st2.s_id in(
  264. select st.s_id from student st
  265. left join score sc on sc.s_id=st.s_id
  266. group by st.s_id having min(sc.s_score)>=70)
  267. order by s_id
  268. -- 37、查询不及格的课程
  269. select st.s_id,c.c_name,st.s_name,sc.s_score from student st
  270. inner join score sc on sc.s_id=st.s_id and sc.s_score<60
  271. inner join course c on c.c_id=sc.c_id
  272. -- 38、查询课程编号为01且课程成绩在80分以上的学生的学号和姓名
  273. select st.s_id,st.s_name,sc.s_score from student st
  274. inner join score sc on sc.s_id=st.s_id and sc.c_id="01" and sc.s_score>=80
  275. -- 39、求每门课程的学生人数
  276. select c.c_id,c.c_name,count(1) from course c
  277. inner join score sc on sc.c_id=c.c_id
  278. group by c.c_id
  279. -- 40、查询选修"张三"老师所授课程的学生中,成绩最高的学生信息及其成绩
  280. select st.*,c.c_name,sc.s_score,t.t_name from student st
  281. inner join score sc on sc.s_id=st.s_id
  282. inner join course c on c.c_id=sc.c_id
  283. inner join teacher t on t.t_id=c.t_id and t.t_name="张三"
  284. order by sc.s_score desc
  285. limit 0,1
  286. -- 41、查询不同课程成绩相同的学生的学生编号、课程编号、学生成绩
  287. select st.s_id,st.s_name,sc.c_id,sc.s_score from student st
  288. left join score sc on sc.s_id=st.s_id
  289. left join course c on c.c_id=sc.c_id
  290. where (
  291. select count(1) from student st2
  292. left join score sc2 on sc2.s_id=st2.s_id
  293. left join course c2 on c2.c_id=sc2.c_id
  294. where sc.s_score=sc2.s_score and c.c_id!=c2.c_id
  295. )>1
  296. -- 42、查询每门功成绩最好的前两名
  297. select a.* from (select st.s_id,st.s_name,c.c_name,sc.s_score from student st
  298. left join score sc on sc.s_id=st.s_id
  299. inner join course c on c.c_id=sc.c_id and c.c_id="01"
  300. order by sc.s_score desc limit 0,2) a
  301. union all
  302. select b.* from (select st.s_id,st.s_name,c.c_name,sc.s_score from student st
  303. left join score sc on sc.s_id=st.s_id
  304. inner join course c on c.c_id=sc.c_id and c.c_id="02"
  305. order by sc.s_score desc limit 0,2) b
  306. union all
  307. select c.* from (select st.s_id,st.s_name,c.c_name,sc.s_score from student st
  308. left join score sc on sc.s_id=st.s_id
  309. inner join course c on c.c_id=sc.c_id and c.c_id="03"
  310. order by sc.s_score desc limit 0,2) c
  311. -- 借鉴(更准确,漂亮):
  312. select a.s_id,a.c_id,a.s_score from score a
  313. where (select COUNT(1) from score b where b.c_id=a.c_id and b.s_score>=a.s_score)<=2 order by a.c_id
  314. -- 43、统计每门课程的学生选修人数(超过5人的课程才统计)。要求输出课程号和选修人数,查询结果按人数降序排列,
  315. -- 若人数相同,按课程号升序排列
  316. select sc.c_id,count(1) from score sc
  317. left join course c on c.c_id=sc.c_id
  318. group by c.c_id having count(1)>5
  319. order by count(1) desc,sc.c_id asc
  320. -- 44、检索至少选修两门课程的学生学号
  321. select st.s_id from student st
  322. left join score sc on sc.s_id=st.s_id
  323. group by st.s_id having count(1)>=2
  324. -- 45、查询选修了全部课程的学生信息
  325. select st.* from student st
  326. left join score sc on sc.s_id=st.s_id
  327. group by st.s_id having count(1)=(select count(1) from course)
  328. -- 46、查询各学生的年龄
  329. select st.*,timestampdiff(year,st.s_birth,now()) from student st
  330. -- 47、查询本周过生日的学生
  331. -- 此处可能有问题,week函数取的为当前年的第几周,2017-12-12是第50周而2018-12-12是第49周,可以取月份,day,星期几(%w),
  332. -- 再判断本周是否会持续到下一个月进行判断,太麻烦,不会写
  333. select st.* from student st
  334. where week(now())=week(date_format(st.s_birth,'%Y%m%d'))
  335. -- 48、查询下周过生日的学生
  336. select st.* from student st
  337. where week(now())+1=week(date_format(st.s_birth,'%Y%m%d'))
  338. -- 49、查询本月过生日的学生
  339. select st.* from student st
  340. where month(now())=month(date_format(st.s_birth,'%Y%m%d'))
  341. -- 50、查询下月过生日的学生
  342. -- 注意:当 当前月为12时,用month(now())+113而不是1,可用timestampadd()函数或mod取模
  343. select st.* from student st
  344. where month(timestampadd(month,1,now()))=month(date_format(st.s_birth,'%Y%m%d'))
  345. --
  346. select st.* from student st where (month(now()) + 1) mod 12 = month(date_format(st.s_birth,'%Y%m%d'))

Mysql Sql 语句练习题 (50道)的更多相关文章

  1. sql语句练习50题(Mysql版-详加注释)

    表名和字段 1.学生表       Student(s_id,s_name,s_birth,s_sex) --学生编号,学生姓名, 出生年月,学生性别 2.课程表       Course(c_id, ...

  2. mysql sql语句大全(转载)

      1.说明:创建数据库 CREATE DATABASE database-name 2.说明:删除数据库 drop database dbname 3.说明:备份sql server --- 创建 ...

  3. MySQL客户端工具的使用与MySQL SQL语句

    MySQL客户端工具的使用 1.MySQL程序的组成 客户端 mysql:CLI交互式客户端程序 mycli:CLI交互式客户端程序;使用sql语句时会有提示信息 mysql_secure_insta ...

  4. MYSQL SQL语句技巧初探(一)

    MYSQL SQL语句技巧初探(一) 本文是我最近了解到的sql某些方法()组合实现一些功能的总结以后还会更新: rand与rand(n)实现提取随机行及order by原理的探讨. Bit_and, ...

  5. MySQL经典练习题及答案,常用SQL语句练习50题

    表名和字段 –1.学生表 Student(s_id,s_name,s_birth,s_sex) –学生编号,学生姓名, 出生年月,学生性别 –2.课程表 Course(c_id,c_name,t_id ...

  6. sql语句练习50题(Mysql版)

    表名和字段–1.学生表Student(s_id,s_name,s_birth,s_sex) –学生编号,学生姓名, 出生年月,学生性别–2.课程表Course(c_id,c_name,t_id) – ...

  7. -sql语句练习50题(Mysql学习练习版)

    –1.学生表 Student(s_id,s_name,s_birth,s_sex) –学生编号,学生姓名, 出生年月,学生性别 –2.课程表 Course(c_id,c_name,t_id) – –课 ...

  8. sql语句练习50题(Mysql版) 围观

    表名和字段 –.学生表 Student(s_id,s_name,s_birth,s_sex) –学生编号,学生姓名, 出生年月,学生性别 –.课程表 Course(c_id,c_name,t_id) ...

  9. SQL练习题-50道SQL练习题及答案与详细分析

    网上流传较广的50道SQL训练,奋斗了不知道多久终于写完了.前18道题的难度依次递增,从19题开始的后半部分算是循环练习和额外function的附加练习,难度恢复到普通状态.第9题非常难,我反正没有写 ...

随机推荐

  1. CodeForces - 686D 【树的重心】

    传送门:http://codeforces.com/problemset/problem/686/D 题意:给你n个节点,其中1为根, 第二行给你2~n的节点的父亲节点编号. 然后是q个询问,求询问的 ...

  2. Go get unrecognized import path "gopkg.in/yaml.v2"

    安装gin: go get -u github.com/gin-gonic/gin 出现错误: package gopkg.in/yaml.v2: unrecognized import path & ...

  3. 2020.2.21一些python总结

    #字符串前面加r可以防止字符串转义 \也可以防止字符串转义#三引号可以实现输入多行文本#range(start,end,step)#列表 append添加一个元素到末尾 extend 添加一个素组到末 ...

  4. springMVC源码阅读-通过画图理解一个请求生命周期(十二)

  5. 寒假day12

    今天写了一点论文,刷了一些算法题

  6. 出现UnboundLocalError: local variable 'a' referenced before assignment异常的情况与解决方法

    出现UnboundLocalError: local variable ‘a’ referenced before assignment异常的情况与解决方法字面意思:局部变量赋值前被引用原因:局部变量 ...

  7. 学生信息管理系统java测试报告

    package studentinformation; /**姓名 胡海靖 * 学号 20183609 * 班级 信1805-2 */ class ScoreInformation { private ...

  8. while read line do done < file

    zzx@zzx120:~/test1$ cat file.txt    1122zzx@zzx120:~/test1$ cat ./read.sh #!/bin/bashwhile read line ...

  9. 2. Rabbitmq php 安装 amqp 拓展

    记录一下 使用 rabbitmq 安装 amqp 拓展 环境 ubuntu   安装目录在 /user/local 下进行 1. 安装 wget apt-get update apt-get inst ...

  10. C#判断两个字符串是否相等的方法 ,还有char赋空值办法。

    string str1="Test"; string str2 = "Test"; if (str1==str2) //第一种判断方式 { //第二种判断方式 ...