# 1. 查询姓名中带“冰” 的学生的名单
select student.* from student where stu_name like '%冰%'
# 2. 查询姓‘王’ 的老师的个数
select count(*) from teacher where tea_name like '王%'
# 3. 检索课程编号为“04”且分数小于60的学生学号,结果按分数降序排列
SELECT stu_id FROM score WHERE course_id = 04 AND course < 60 ORDER BY course DESC
# 4. 查询数学成绩不及格的学生和其对应的成绩,按照学号升序排序
SELECT score.`stu_id`,score.`course` FROM score WHERE course_id = 02 AND course < 60 ORDER BY stu_id ASC
# 5. 查询课程编号为“02”的总成绩
SELECT SUM(course) sum_course FROM score WHERE course_id = 02
# 6. 查询参加考试的学生个数
SELECT COUNT(DISTINCT stu_id) student_total FROM score
# 7. 查询各科成绩最高和最低的分,以如下的形式显示:课程号,最高分,最低分
SELECT score.`course_id`,MAX(score.`course`) 最高分,MIN(score.`course`) 最低分 FROM score GROUP BY score.`course_id`
# 8. 查询每门课程有多少学生参加了考试
SELECT score.`course_id`,COUNT(*) FROM score GROUP BY score.`course_id`
# 9. 查询男生、女生人数
SELECT SUM(IF(sex = '男',1,0)) 男,SUM(IF(sex = '女',1,0)) 女 FROM student
# 10. 查询平均成绩大于60分学生的学号和平均成绩
SELECT stu_id,AVG(course) average_course FROM score GROUP BY stu_id HAVING average_course>60
# 11. 查询至少选修两门课程的学生学号
SELECT stu_id,COUNT(*) FROM score GROUP BY stu_id HAVING COUNT(*)>=2
# 12. 查询同姓(假设每个学生姓名的第一个字为姓)的学生名单并统计同姓人数
SELECT LEFT(stu_name,1),COUNT(*) sum_name FROM student GROUP BY LEFT(stu_name,1) HAVING sum_name>1
# 13. 查询每门课程的平均成绩,结果按平均成绩升序排序,平均成绩相同时,按课程号降序排列
SELECT course_id, AVG(course) average_course FROM score GROUP BY course_id ORDER BY average_course ASC,course_id DESC
# 14. 统计参加考试人数大于等于15的学科
SELECT course_id FROM score GROUP BY course_id HAVING COUNT(*)>=15
# 15. 查询学生的总成绩并进行排名
SELECT SUM(course) sum_course FROM score GROUP BY stu_id ORDER BY sum_course ASC
# 16. 查询平均成绩大于60分的学生的学号和平均成绩
SELECT stu_id,AVG(course) avg_course FROM score GROUP BY stu_id HAVING avg_course>60
# 17. 查询一共参加三门课程且其中一门为语文课程的学生的id
SELECT stu_id,COUNT(course_id),any_value(course_id),GROUP_CONCAT(course_id) FROM score GROUP BY stu_id HAVING COUNT(course_id) = 4 AND FIND_IN_SET('05',GROUP_CONCAT(course_id))
# 18. 查询所有课程成绩小于60分学生的学号、姓名
SELECT stu_id,stu_name FROM student WHERE stu_id IN (SELECT stu_id FROM score GROUP BY stu_id HAVING MAX(course) <60)
# 19 查询出只选修了三门课程的全部学生的学号
SELECT stu_id FROM score GROUP BY stu_id HAVING COUNT(*) = 3
# 20. 查询所有学生的学号、姓名、选课数、总成绩
SELECT stu_id,COUNT(*) total_course,SUM(course) sum_course FROM score GROUP BY stu_id