课后练习:
    1、查询”李”姓老师的数量
    2、查询1990年出生的学生名单
    3、查询男生、女生人数
    4、查询学生的总成绩、平均成绩并进行排名
    5、查询每门课程被选修的学生数
    6、查询学过编号为”01”并且也学过编号为”02”的课程的同学的信息
    7、查询平均成绩大于等于85的所有学生的学号、姓名和平均成绩
    8、检索至少选修两门课程的学生学号
    9、统计各科成绩各分数段[100-85],[85-70],[70-60],[0-60]的人数

    1. -- 1.学生表
    2. create table S(
    3. s_id varchar(2) comment '学生编号' primary key,
    4. s_name varchar(10) comment '学生姓名',
    5. s_birth date comment '出生年月',
    6. s_gend enum('男','女') comment '学生性别'
    7. );
    8. insert into S values('01','赵雷','1990-01-01','男')
    9. ,('02','钱电','1990-12-21','男')
    10. ,('03','孙风','1990-05-20','男')
    11. ,('04','李云','1990-08-06','男')
    12. ,('05','周梅','1991-12-01','女')
    13. ,('06','吴兰','1992-03-01','女')
    14. ,('07','郑竹','1989-07-01','女')
    15. ,('08','王菊','1990-01-20','女');
    16. -- 2.课程表
    17. create table C(
    18. c_id varchar(2) comment '课程编号' primary key,
    19. c_name varchar(10) comment '课程名称',
    20. t_id varchar(2) comment '教师编号'
    21. );
    22. insert into C values('01','语文','02')
    23. ,('02','数学','01')
    24. ,('03','英语','03');
    25. -- 3.教师表
    26. create table T(
    27. t_id varchar(2) comment '教师编号' primary key,
    28. t_name varchar(10) comment '教师姓名'
    29. );
    30. insert into T values('01','张三')
    31. ,('02','李四')
    32. ,('03','王五');
    33. -- 4.成绩表
    34. create table Score(
    35. s_id varchar(2) comment '学生编号',
    36. c_id varchar(2) comment '课程编号',
    37. s_score float comment '分数',
    38. primary key(s_id,c_id)
    39. );
    40. insert into Score values('01','01',80),('03','01',80),('05','01',76),
    41. ('01','02',90),('03','02',80),('05','02',87),
    42. ('01','03',99),('03','03',80),('06','01',31),
    43. ('02','01',70),('04','01',50),('06','03',34),
    44. ('02','02',60),('04','02',30),('07','02',89),
    45. ('02','03',80),('04','03',20),('07','03',98);
    46. -- 1、查询"李"姓老师的数量
    47. select count(*) from T where t_name like '李%';
    48. +----------+
    49. | count(*) |
    50. +----------+
    51. | 1 |
    52. +----------+
    53. -- 2、查询1990年出生的学生名单
    54. select s_id,s_name,s_birth from S where s_birth like '1990%';
    55. +------+--------+------------+
    56. | s_id | s_name | s_birth |
    57. +------+--------+------------+
    58. | 01 | 赵雷 | 1990-01-01 |
    59. | 02 | 钱电 | 1990-12-21 |
    60. | 03 | 孙风 | 1990-05-20 |
    61. | 04 | 李云 | 1990-08-06 |
    62. | 08 | 王菊 | 1990-01-20 |
    63. +------+--------+------------+
    64. -- 3、查询男生、女生人数
    65. select s_gend,count(*) from S group by s_gend;
    66. +--------+----------+
    67. | s_gend | count(*) |
    68. +--------+----------+
    69. | | 4 |
    70. | | 4 |
    71. +--------+----------+
    72. -- 4、查询学生的总成绩、平均成绩并进行排名
    73. select S.s_id,S.s_name,sum(s_score),round(avg(s_score),2)
    74. from S,Score where Score.s_id=S.s_id
    75. group by Score.s_id
    76. order by sum(s_score) desc;
    77. +------+--------+--------------+-----------------------+
    78. | s_id | s_name | sum(s_score) | round(avg(s_score),2) |
    79. +------+--------+--------------+-----------------------+
    80. | 01 | 赵雷 | 269 | 89.67 |
    81. | 03 | 孙风 | 240 | 80 |
    82. | 02 | 钱电 | 210 | 70 |
    83. | 07 | 郑竹 | 187 | 93.5 |
    84. | 05 | 周梅 | 163 | 81.5 |
    85. | 04 | 李云 | 100 | 33.33 |
    86. | 06 | 吴兰 | 65 | 32.5 |
    87. +------+--------+--------------+-----------------------+
    88. -- 5、查询每门课程被选修的学生数
    89. select C.c_id,C.c_name,count(*)
    90. from C,Score
    91. where Score.c_id=C.c_id
    92. group by Score.c_id;
    93. +------+--------+----------+
    94. | c_id | c_name | count(*) |
    95. +------+--------+----------+
    96. | 01 | 语文 | 6 |
    97. | 02 | 数学 | 6 |
    98. | 03 | 英语 | 6 |
    99. +------+--------+----------+
    100. -- 6、查询学过编号为"01"并且也学过编号为"02"的课程的同学的信息
    101. -- 思路,先筛选0102,然后分组后count=2
    102. select s.*
    103. from S inner join Score on Score.s_id=S.s_id
    104. where c_id='01' or c_id='02'
    105. group by Score.s_id
    106. having count(*) = 2;
    107. +------+--------+------------+--------+
    108. | s_id | s_name | s_birth | s_gend |
    109. +------+--------+------------+--------+
    110. | 01 | 赵雷 | 1990-01-01 | |
    111. | 02 | 钱电 | 1990-12-21 | |
    112. | 03 | 孙风 | 1990-05-20 | |
    113. | 04 | 李云 | 1990-08-06 | |
    114. | 05 | 周梅 | 1991-12-01 | |
    115. +------+--------+------------+--------+
    116. -- 7、查询平均成绩大于等于85的所有学生的学号、姓名和平均成绩
    117. select S.s_id,S.s_name,round(avg(Score.s_score),2)
    118. from S inner join Score on Score.s_id=S.s_id
    119. group by Score.s_id
    120. having round(avg(Score.s_score),2) >= 85;
    121. +------+--------+-----------------------------+
    122. | s_id | s_name | round(avg(Score.s_score),2) |
    123. +------+--------+-----------------------------+
    124. | 01 | 赵雷 | 89.67 |
    125. | 07 | 郑竹 | 93.5 |
    126. +------+--------+-----------------------------+
    127. -- 8、检索至少选修两门课程的学生学号
    128. select s_id
    129. from Score
    130. group by s_id
    131. having count(c_id) >= 2;
    132. +------+
    133. | s_id |
    134. +------+
    135. | 01 |
    136. | 02 |
    137. | 03 |
    138. | 04 |
    139. | 05 |
    140. | 06 |
    141. | 07 |
    142. +------+
    143. -- 9、统计各科成绩各分数段[100-85),[85-70),[70-60),[0-60]的人数
    144. -- 方式一:case when
    145. select C.c_name,
    146. case when s_score <= 100 and s_score > 85 then 1 else 0 end as '[100-85)',
    147. case when s_score <= 85 and s_score > 70 then 1 else 0 end as '[85-70)',
    148. case when s_score <= 70 and s_score > 60 then 1 else 0 end as '[70-60)',
    149. case when s_score <= 60 and s_score >= 0 then 1 else 0 end as '[0-60]'
    150. from C inner join Score on Score.c_id=C.c_id
    151. --方式二:if()
    152. select C.c_name,
    153. if(s_score <= 100 and s_score > 85,1,0) as '[100-85)',
    154. if(s_score <= 85 and s_score > 70,1,0)as '[85-70)',
    155. if(s_score <= 70 and s_score > 60,1,0)as '[70-60)',
    156. if(s_score <= 60 and s_score >= 0,1,0)as '[0-60]'
    157. from C inner join Score on Score.c_id=C.c_id
    158. +--------+----------+---------+---------+--------+
    159. | c_name | [100-85) | [85-70) | [70-60) | [0-60] |
    160. +--------+----------+---------+---------+--------+
    161. | 语文 | 0 | 1 | 0 | 0 |
    162. | 数学 | 1 | 0 | 0 | 0 |
    163. | 英语 | 1 | 0 | 0 | 0 |
    164. | 语文 | 0 | 0 | 1 | 0 |
    165. | 数学 | 0 | 0 | 0 | 1 |
    166. | 英语 | 0 | 1 | 0 | 0 |
    167. | 语文 | 0 | 1 | 0 | 0 |
    168. | 数学 | 0 | 1 | 0 | 0 |
    169. | 英语 | 0 | 1 | 0 | 0 |
    170. | 语文 | 0 | 0 | 0 | 1 |
    171. | 数学 | 0 | 0 | 0 | 1 |
    172. | 英语 | 0 | 0 | 0 | 1 |
    173. | 语文 | 0 | 1 | 0 | 0 |
    174. | 数学 | 1 | 0 | 0 | 0 |
    175. | 语文 | 0 | 0 | 0 | 1 |
    176. | 英语 | 0 | 0 | 0 | 1 |
    177. | 数学 | 1 | 0 | 0 | 0 |
    178. | 英语 | 1 | 0 | 0 | 0 |
    179. +--------+----------+---------+---------+--------+
    180. -- 以课程分组,通过聚合函数聚合一下,countsum都可以
    181. select C.c_name,
    182. sum(case when s_score <= 100 and s_score > 85 then 1 else 0 end) as '[100-85)',
    183. sum(case when s_score <= 85 and s_score > 70 then 1 else 0 end) as '[85-70)',
    184. sum(case when s_score <= 70 and s_score > 60 then 1 else 0 end) as '[70-60)',
    185. sum(case when s_score <= 60 and s_score >= 0 then 1 else 0 end) as '[0-60]'
    186. from C inner join Score on Score.c_id=C.c_id
    187. group by Score.c_id;
    188. +--------+----------+---------+---------+--------+
    189. | c_name | [100-85) | [85-70) | [70-60) | [0-60] |
    190. +--------+----------+---------+---------+--------+
    191. | 语文 | 0 | 3 | 1 | 2 |
    192. | 数学 | 3 | 1 | 0 | 2 |
    193. | 英语 | 2 | 2 | 0 | 2 |
    194. +--------+----------+---------+---------+--------+

    一个字段满足多个条件
    行转列