1. -- 1.查询" 01 "课程比" 02 "课程成绩高的学生的信息及课程分数
    2. -- 1.1分别查询"01","02"课程的学生id和分数
    3. SELECT SId,score FROM SC WHERE CId = '01';
    4. SELECT SId,score FROM SC WHERE CId = '02';
    5. -- 1.2再查询"01""02"课程成绩高的学生id和分数
    6. SELECT tmp1.SId,tmp1.score AS '语文',tmp2.score AS '数学' FROM (SELECT SId,score FROM SC WHERE CId = '01') AS tmp1,
    7. (SELECT SId,score FROM SC WHERE CId = '02') AS tmp2 WHERE tmp1.score > tmp2.score AND tmp1.SId = tmp2.SId;
    8. -- 1.3关联学生表查询学生的信息
    9. SELECT *
    10. FROM student AS stu RIGHT JOIN (SELECT tmp1.SId,tmp1.score AS '语文',tmp2.score AS '数学' FROM (SELECT SId,score FROM SC WHERE CId = '01') AS tmp1,
    11. (SELECT SId,score FROM SC WHERE CId = '02') AS tmp2 WHERE tmp1.score > tmp2.score AND tmp1.SId = tmp2.SId)AS tmp ON stu.SId = tmp.SId;
    12. -- 2.查询平均成绩大于等于 60 分的同学的学生编号和学生姓名和平均成绩
    13. -- 2.1在成绩表查询每位学生的平均成绩
    14. SELECT avg(score),SId FROM SC GROUP BY SId;
    15. -- 2.2查询出平均成绩大于等于60分的学生id和平均成绩
    16. SELECT avg(score) AS `avg`,SId FROM SC GROUP BY SId HAVING AVG(score) >= 60;
    17. -- 2.32.2作为临时表关联学生表查询出学生姓名,学生编号和平均成绩
    18. SELECT stu.*,tmp.avg
    19. FROM Student AS stu
    20. RIGHT JOIN (SELECT avg(score) AS `avg`,SId FROM SC GROUP BY SId HAVING AVG(score) >= 60) AS tmp
    21. ON stu.SId = tmp.SId;
    22. -- 3.查询在 SC 表存在成绩的学生信息
    23. select DISTINCT student.*
    24. from student,sc
    25. where student.SId=sc.SId AND sc.score >= 0;
    26. -- 4.查询所有同学的学生编号、学生姓名、选课总数、所有课程的成绩总和
    27. -- 4.1在成绩表查询每位同学的成绩总和和选课总数
    28. SELECT sc.SId,COUNT(CId) AS cou,SUM(score) AS sum FROM SC GROUP BY sc.SId;
    29. -- 4.24.1作为临时表关联查询学生表使用外连接
    30. SELECT *
    31. FROM Student AS stu
    32. LEFT JOIN (SELECT sc.SId,COUNT(CId) AS cou,SUM(score) AS sum FROM SC GROUP BY sc.SId) AS tmp
    33. ON stu.SId = tmp.SId;
    34. -- 4.3补充查询字段
    35. SELECT stu.SId,stu.Sname,stu.Sage,stu.Ssex,tmp.cou,tmp.sum
    36. FROM Student AS stu
    37. LEFT JOIN (SELECT sc.SId,COUNT(CId) AS cou,SUM(score) AS sum FROM SC GROUP BY sc.SId) AS tmp
    38. ON stu.SId = tmp.SId;
    39. -- 5.查有成绩的学生信息
    40. SELECT * FROM sc WHERE EXISTS(score);
    41. -- 6.查询「李」姓老师的数量
    42. SELECT COUNT(*) FROM teacher WHERE Tname LIKE '李%';
    43. -- 7.查询学过「张三」老师授课的同学的信息
    44. -- 7.1确定查询的表(老师表,成绩表,学生表);
    45. SELECT * FROM teacher AS te,sc,student AS stu WHERE sc.SId = stu.SId;
    46. -- 7.27.1加入查询条件张三老师,并按照学生id进行分组
    47. SELECT * FROM teacher AS te,sc,student AS stu WHERE sc.SId = stu.SId AND te.Tname = '张三' GROUP BY sc.SId;
    48. -- 7.3确定最终查询的字段学生信息
    49. SELECT stu.SId,stu.Sname,stu.Sage,stu.Ssex FROM teacher AS te,sc,student AS stu WHERE sc.SId = stu.SId AND te.Tname = '张三' GROUP BY sc.SId;
    50. -- 8.查询没有学全所有课程的同学的信息
    51. -- 8.1使用聚合函数在课程表中查询一共有几门课
    52. SELECT COUNT(CId) FROM course;
    53. -- 8.2在成绩表根据课程查询每位学生学习课程的数量并筛选出学完课程的学生ID
    54. SELECT sc.SId FROM sc GROUP BY sc.SId HAVING COUNT(sc.CId) = (SELECT COUNT(CId) FROM course);
    55. -- 8.38.2查询出了已学完课程的学生ID,根据学生ID在学生表查询出还未学完课程的学生信息(NOT IN)
    56. SELECT
    57. stu.*
    58. FROM
    59. student AS stu
    60. WHERE
    61. stu.SId NOT IN (
    62. SELECT
    63. sc.SId
    64. FROM
    65. sc
    66. GROUP BY
    67. sc.SId
    68. HAVING
    69. COUNT(sc.CId) = (SELECT COUNT(CId) FROM course)
    70. )
    71. -- 9.查询至少有一门课与学号为" 01 "的同学所学相同的同学的信息
    72. -- 9.1在成绩表查询学号01的同学所学习的课程
    73. SELECT CId FROM sc WHERE SId = '01'
    74. -- 9.2在成绩表继续查询至少有同学学习一门9.1查询的课程的每位学生ID
    75. SELECT SId FROM sc WHERE CId IN (SELECT CId FROM sc WHERE SId = '01')
    76. -- 9.3根据9.2查询出的学生ID,在学生表查询查询学生信息
    77. SELECT * FROM student
    78. WHERE SId IN (SELECT SId FROM sc WHERE CId IN (SELECT CId FROM sc WHERE SId = '01'))
    79. -- 10.查询和" 01 "号的同学学习的课程完全相同的其他同学的信息
    80. -- 10.1在成绩表查询学号01的同学所学习的课程
    81. SELECT CId FROM sc WHERE SId = '01'
    82. -- 10.2在成绩表查询和'01'同学课程相同且课程数等于3的学生ID
    83. SELECT SId FROM sc WHERE CId IN (SELECT CId FROM sc WHERE SId = '01')GROUP BY sc.SId HAVING COUNT(sc.CId) > 2
    84. -- 10.3在学生表根据10.2的学生ID查询学生信息
    85. SELECT * FROM student
    86. WHERE SId IN (SELECT SId FROM sc WHERE CId IN (SELECT CId FROM sc WHERE SId = '01') GROUP BY sc.SId HAVING COUNT(sc.CId) > 2)
    87. -- 11.查询没学过"张三"老师讲授的任一门课程的学生姓名
    88. -- 11.1在教师表查询'张三'老师的id
    89. SELECT TId FROM teacher WHERE Tname = '张三'
    90. -- 11.2根据张三老师的id在成绩表查询所教授的课程id
    91. SELECT CId FROM course WHERE TId = (SELECT TId FROM teacher WHERE Tname = '张三')
    92. -- 11.3在成绩表根据课程id查询学习过这门课程的学生id
    93. SELECT SId FROM sc WHERE CId = (SELECT CId FROM course WHERE TId = (SELECT TId FROM teacher WHERE Tname = '张三'))
    94. -- 11.4在学生表查询在11.3以外的学生id
    95. SELECT * FROM student WHERE SId NOT IN(SELECT SId FROM sc WHERE CId = (SELECT CId FROM course WHERE TId = (SELECT TId FROM teacher WHERE Tname = '张三')))
    96. - 12.查询两门及其以上不及格课程的同学的学号,姓名及其平均成绩
    97. -- 12.1在成绩表查询成绩不及格的学生ID
    98. SELECT SId,CId FROM sc WHERE score < 60
    99. -- 12.212.1基础上根据学生进行分组并查询其平均成绩
    100. SELECT SId,AVG(score) FROM sc WHERE score < 60 GROUP BY SId
    101. -- 12.3根据12.2查询的学生ID关联学生表查询学生姓名学号和平均成绩
    102. SELECT stu.SId,stu.Sname,tmp.avg as '平均成绩'
    103. FROM student AS stu LEFT JOIN (SELECT SId,AVG(score) AS avg FROM sc WHERE score < 60 GROUP BY SId HAVING COUNT(*) >= 2) AS tmp
    104. ON stu.SId = tmp.SId
    105. -- 13.检索" 01 "课程分数小于 60,按分数降序排列的学生信息
    106. -- 13.1在成绩查询课程为'01'的且分数小于60的学生ID
    107. SELECT SId,score FROM sc WHERE CId = '01' AND score < 60
    108. -- 13.213.1的基础上把分数按照分数降序排序
    109. SELECT SId FROM sc WHERE CId = '01' AND score < 60 ORDER BY score DESC
    110. -- 13.3根据13.2查询出的学生ID,在学生表查询出学生信息
    111. SELECT * FROM student
    112. WHERE SId IN (SELECT SId FROM sc WHERE CId = '01' AND score < 60 ORDER BY score DESC)
    113. -- 14.按平均成绩从高到低显示所有学生的所有课程的成绩以及平均成绩
    114. -- 14.1在成绩表查询出每位学生的平均成绩并按照平均成绩降序排序
    115. SELECT SId,AVG(score) FROM sc GROUP BY SId ORDER BY AVG(score) DESC
    116. -- 14.214.1的基础上关联成绩表查询所有学生ID和课程成绩和平均成绩
    117. SELECT s.SId,s.CId,s.score,tmp.avg as '平均成绩'
    118. from sc AS s LEFT JOIN (SELECT SId,AVG(score) AS avg FROM sc GROUP BY SId ) AS tmp
    119. ON s.SId = tmp.SId ORDER BY tmp.avg DESC
    120. -- 15.查询各科成绩最高分、最低分和平均分:以如下形式显示:课程 ID,课程 name,最高分,最低分,平均分,及格率,中等率,优良率,优秀率
    121. -- 及格为>=60,中等为:70-80,优良为:80-90,优秀为:>=90
    122. -- 要求输出课程号和选修人数,查询结果按人数降序排列,若人数相同,按课程号升序排列
    123. SELECT sc.CId AS '课程ID',MAX(sc.score)AS '最高分',MIN(sc.score) AS '最低分',AVG(sc.score) AS '平均分',COUNT(*) AS '选修人数',
    124. SUM(CASE WHEN sc.score > 60 THEN 1 ELSE 0 END)/COUNT(*) AS '及格率',
    125. SUM(CASE WHEN 70 < sc.score < 80 THEN 1 ELSE 0 END)/COUNT(*) AS '中等',
    126. SUM(CASE WHEN sc.score >= 90 THEN 1 ELSE 0 END)/COUNT(*) AS '优秀'
    127. FROM sc GROUP BY CId ORDER BY COUNT(*) DESC,sc.CId ASC
    128. -- 16.查询学生的总成绩,并进行排名,总分重复时不保留名次空缺
    129. -- 16.1在成绩表查询每位学生的总成绩,并按照总成绩降序排序
    130. SET @i = 0;
    131. SELECT tmp.SId,total,@i:= @i + 1 AS rank FROM(
    132. SELECT sc.SId,SUM(sc.score) AS total
    133. FROM sc GROUP BY SId ORDER BY SUM(sc.score) DESC)tmp