2. 查询"01"课程比"02"课程成绩低的学生的信息及课程分数
SELECT st.*, s.s_score AS 数学, s2.s_score AS 语文 FROM student st LEFT JOIN score s ON s.s_id = st.s_id AND s.c_id = '01' LEFT JOIN score s2 ON s2.s_id = st.s_id AND s2.c_id = '02' WHERE s.s_score < s2.s_score
3. 查询平均成绩大于等于60分的同学的学生编号和学生姓名和平均成绩
SELECT st.s_id AS '学生编号', st.s_name AS '学生姓名', AVG( s.s_score ) AS avgScore FROM student st LEFT JOIN score s ON st.s_id = s.s_id GROUP BY st.s_id HAVING avgScore >= 60
4. 查询平均成绩小于60分的同学的学生编号和学生姓名和平均成绩
- (包括有成绩的和无成绩的)
SELECT st.s_id AS '学生编号', st.s_name AS '学生姓名',( CASE WHEN ROUND( AVG( sc.s_score ), 2 ) IS NULL THEN 0 ELSE ROUND( AVG( sc.s_score ), 2 ) END ) FROM student st LEFT JOIN score sc ON st.s_id = sc.s_id GROUP BY st.s_id HAVING AVG( sc.s_score )< 60 OR AVG( sc.s_score ) IS NULL
5. 查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩
SELECT st.s_id AS '学生编号', st.s_name AS '学生姓名', COUNT( sc.c_id ) AS '选课总数', sum( CASE WHEN sc.s_score IS NULL THEN 0 ELSE sc.s_score END ) AS '总成绩' FROM student st LEFT JOIN score sc ON st.s_id = sc.s_id GROUP BY st.s_id
6. 查询"流"姓老师的数量
SELECT COUNT(t_id) FROM teacher WHERE t_name LIKE '流%'
7. 查询学过"流浪法师"老师授课的同学的信息
SELECT st.* FROM student st LEFT JOIN score sc ON sc.s_id = st.s_id LEFT JOIN course cs ON cs.c_id = sc.c_id LEFT JOIN teacher tc ON tc.t_id = cs.t_id WHERE tc.t_name = '流浪法师'
8. 查询没学过"张三"老师授课的同学的信息
-- 查询流浪法师教的课 SELECT cs.* FROM course cs LEFT JOIN teacher tc ON tc.t_id = cs.t_id WHERE tc.t_name = '流浪法师' -- 查询有流浪法师课程成绩的学生id SELECT sc.s_id FROM score sc WHERE sc.c_id IN ( SELECT cs.c_id FROM course cs LEFT JOIN teacher tc ON tc.t_id = cs.t_id WHERE tc.t_name = '流浪法师') -- 取反,查询没有学过流浪法师课程的同学信息 SELECT st.* FROM student st WHERE st.s_id NOT IN ( SELECT sc.s_id FROM score sc WHERE sc.c_id IN ( SELECT cs.c_id FROM course cs LEFT JOIN teacher tc ON tc.t_id = cs.t_id WHERE tc.t_name = '流浪法师' ) )
9. 查询学过编号为"01"并且也学过编号为"02"的课程的同学的信息
- 方法 1
-- 查询学过编号为01课程的同学id SELECT st.s_id FROM student st INNER JOIN score sc ON sc.s_id = st.s_id INNER JOIN course cs ON cs.c_id = sc.c_id AND cs.c_id = '01'; -- 查询学过编号为02课程的同学id SELECT st2.s_id FROM student st2 INNER JOIN score sc2 ON sc2.s_id = st2.s_id INNER JOIN course cs2 ON cs2.c_id = sc2.c_id AND cs2.c_id = '02'; -- 查询学过编号为"01"并且也学过编号为"02"的课程的同学的信息 SELECT st.* FROM student st INNER JOIN score sc ON sc.s_id = st.s_id INNER JOIN course cs ON cs.c_id = sc.c_id AND sc.c_id = '01' WHERE st.s_id IN ( SELECT st2.s_id FROM student st2 INNER JOIN score sc2 ON sc2.s_id = st2.s_id INNER JOIN course cs2 ON cs2.c_id = sc2.c_id AND cs2.c_id = '02' );
- 方法 2
SELECT a.* FROM student a, score b, score c WHERE a.s_id = b.s_id AND a.s_id = c.s_id AND b.c_id = '01' AND c.c_id = '02';
10. 查询学过编号为"01"但是没有学过编号为"02"的课程的同学的信息
SELECT st.s_id FROM student st INNER JOIN score sc ON sc.s_id = st.s_id INNER JOIN course cs ON cs.c_id = sc.c_id AND cs.c_id = '01' WHERE st.s_id NOT IN ( SELECT st.s_id FROM student st INNER JOIN score sc ON sc.s_id = st.s_id INNER JOIN course cs ON cs.c_id = sc.c_id AND cs.c_id = '02' );
11. 查询没有学全所有课程的同学的信息
- 方法 1
SELECT * FROM student WHERE s_id NOT IN ( SELECT st.s_id FROM student st INNER JOIN score sc ON sc.s_id = st.s_id AND sc.c_id = '01' WHERE st.s_id IN ( SELECT st.s_id FROM student st INNER JOIN score sc ON sc.s_id = st.s_id AND sc.c_id = '02' WHERE st.s_id ) AND st.s_id IN ( SELECT st.s_id FROM student st INNER JOIN score sc ON sc.s_id = st.s_id AND sc.c_id = '03' WHERE st.s_id ) );
- 方法 2
SELECT a.* FROM student a LEFT JOIN score b ON a.s_id = b.s_id GROUP BY a.s_id HAVING COUNT( b.c_id ) != '3';
12. 查询至少有一门课与学号为"01"的同学所学相同的同学的信息
SELECT DISTINCT st.* FROM student st LEFT JOIN score sc ON sc.s_id = st.s_id WHERE sc.c_id IN ( SELECT sc2.c_id FROM student st2 LEFT JOIN score sc2 ON sc2.s_id = st2.s_id WHERE st2.s_id = '01' );
13. 查询和"01"号的同学学习的课程完全相同的其他同学的信息
SELECT st.* FROM student st LEFT JOIN score sc ON sc.s_id = st.s_id GROUP BY st.s_id HAVING GROUP_CONCAT( sc.c_id )=( SELECT GROUP_CONCAT( sc2.c_id ) FROM student st2 LEFT JOIN score sc2 ON sc2.s_id = st2.s_id WHERE st2.s_id = '01' );
14. 查询没学过"邪恶小法师"老师讲授的任一门课程的学生姓名
SELECT * FROM student WHERE s_id NOT IN ( SELECT sc.s_id FROM score sc INNER JOIN course cs ON cs.c_id = sc.c_id INNER JOIN teacher t ON t.t_id = cs.t_id AND t.t_name = '邪恶小法师');
15. 查询两门及其以上不及格课程的同学的学号,姓名及其平均成绩
SELECT st.s_id AS '学号', st.s_name AS '姓名', AVG( sc.s_score ) AS '平均成绩' FROM student st LEFT JOIN score sc ON sc.s_id = st.s_id WHERE sc.s_id IN ( SELECT sc.s_id FROM score sc WHERE sc.s_score < 60 OR sc.s_score IS NULL GROUP BY sc.s_id HAVING COUNT( 1 )>= 2 ) GROUP BY st.s_id
16. 检索"01"课程分数小于60,按分数降序排列的学生信息
SELECT st.* FROM student st INNER JOIN score sc ON sc.s_id = st.s_id AND sc.c_id = '01' AND sc.s_score < '60' ORDER BY sc.s_score DESC; SELECT st.* FROM student st LEFT JOIN score sc ON sc.s_id = st.s_id WHERE sc.c_id = '01' AND sc.s_score < '60' ORDER BY sc.s_score DESC;
17. 按平均成绩从高到低显示所有学生的所有课程的成绩以及平均成绩
- 方法 1
SELECT st.*, AVG( sc4.s_score ) AS '平均分', sc.s_score AS '语文', sc2.s_score AS '数学', sc3.s_score AS '英语' FROM student st LEFT JOIN score sc ON sc.s_id = st.s_id AND sc.c_id = '01' LEFT JOIN score sc2 ON sc2.s_id = st.s_id AND sc2.c_id = '02' LEFT JOIN score sc3 ON sc3.s_id = st.s_id AND sc3.c_id = '03' LEFT JOIN score sc4 ON sc4.s_id = st.s_id GROUP BY st.s_id ORDER BY AVG( sc4.s_score ) DESC;
- 方法 2
SELECT st.*, ( CASE WHEN AVG( sc4.s_score ) IS NULL THEN 0 ELSE AVG( sc4.s_score ) END ) AS '平均分', ( CASE WHEN sc.s_score IS NULL THEN 0 ELSE sc.s_score END ) AS '语文', ( CASE WHEN sc2.s_score IS NULL THEN 0 ELSE sc2.s_score END ) AS '数学', ( CASE WHEN sc3.s_score IS NULL THEN 0 ELSE sc3.s_score END ) AS '英语' FROM student st LEFT JOIN score sc ON sc.s_id = st.s_id AND sc.c_id = '01' LEFT JOIN score sc2 ON sc2.s_id = st.s_id AND sc2.c_id = '02' LEFT JOIN score sc3 ON sc3.s_id = st.s_id AND sc3.c_id = '03' LEFT JOIN score sc4 ON sc4.s_id = st.s_id GROUP BY st.s_id ORDER BY AVG( sc4.s_score ) DESC;
18. 查询各科成绩最高分、最低分和平均分:
- 以如下形式显示:课程ID,课程name,最高分,最低分,平均分,及格率,中等率,优良率,优秀率
- 及格为>=60,中等为:70-80,优良为:80-90,优秀为:>=90
SELECT cs.c_id, cs.c_name, MAX( sc1.s_score ) AS '最高分', MIN( sc2.s_score ) AS '最低分', AVG( sc3.s_score ) AS '平均分', (( SELECT COUNT( s_id ) FROM score WHERE s_score >= 60 AND c_id = cs.c_id )/( SELECT COUNT( s_id ) FROM score WHERE c_id = cs.c_id )) AS '及格率', (( SELECT COUNT( s_id ) FROM score WHERE s_score >= 70 AND s_score < 80 AND c_id = cs.c_id )/( SELECT COUNT( s_id ) FROM score WHERE c_id = cs.c_id )) AS '中等率', (( SELECT COUNT( s_id ) FROM score WHERE s_score >= 80 AND s_score < 90 AND c_id = cs.c_id )/( SELECT COUNT( s_id ) FROM score WHERE c_id = cs.c_id )) AS '优良率', (( SELECT COUNT( s_id ) FROM score WHERE s_score >= 90 AND c_id = cs.c_id )/( SELECT COUNT( s_id ) FROM score WHERE c_id = cs.c_id )) AS '优秀率' FROM course cs LEFT JOIN score sc1 ON sc1.c_id = cs.c_id LEFT JOIN score sc2 ON sc2.c_id = cs.c_id LEFT JOIN score sc3 ON sc3.c_id = cs.c_id GROUP BY cs.c_id;
19. 按各科成绩进行排序,并显示排名(实现不完全)
- mysql没有rank函数
- 加@score是为了防止用union all 后打乱了顺序
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 LEFT JOIN score sc ON sc.c_id = c.c_id WHERE c.c_id = "01" ORDER BY sc.s_score DESC ) c1, ( SELECT @i := 0 ) a UNION ALL 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 LEFT JOIN score sc ON sc.c_id = c.c_id WHERE c.c_id = "02" ORDER BY sc.s_score DESC ) c2, ( SELECT @ii := 0 ) aa UNION ALL 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 LEFT JOIN score sc ON sc.c_id = c.c_id WHERE c.c_id = "03" ORDER BY sc.s_score DESC ) c3; SET @iii = 0;
20. 查询学生的总成绩并进行排名
SELECT st.s_id, st.s_name, ( CASE WHEN sum( sc.s_score ) IS NULL THEN 0 ELSE SUM( sc.s_score ) END ) FROM student st LEFT JOIN score sc ON st.s_id = sc.s_id GROUP BY st.s_id ORDER BY SUM( sc.s_score ) DESC
21. 查询不同老师所教不同课程平均分从高到低显示
SELECT t.t_id, t.t_name, AVG( sc.s_score ) FROM teacher t LEFT JOIN course c ON c.t_id = t.t_id LEFT JOIN score sc ON sc.c_id = c.c_id GROUP BY t.t_id ORDER BY AVG( sc.s_score ) DESC
22. 查询所有课程的成绩第2名到第3名的学生信息及该课程成绩
SELECT a.* FROM ( SELECT st.s_id, st.s_name, c.c_id, c.c_name, sc.s_score FROM student st LEFT JOIN score sc ON sc.s_id = st.s_id INNER JOIN course c ON sc.c_id = c.c_id AND c.c_id = '01' ORDER BY sc.s_score DESC LIMIT 1, 2 ) a UNION ALL SELECT b.* FROM ( SELECT st.s_id, st.s_name, c.c_id, c.c_name, sc.s_score FROM student st LEFT JOIN score sc ON sc.s_id = st.s_id INNER JOIN course c ON c.c_id = sc.c_id AND c.c_id = '02' ORDER BY sc.s_score DESC LIMIT 1, 2 ) b UNION ALL SELECT c.* FROM ( SELECT st.s_id, st.s_name, c.c_id, c.c_name, sc.s_score FROM student st LEFT JOIN score sc ON sc.s_id = st.s_id INNER JOIN course c ON c.c_id = sc.c_id AND c.c_id = '03' ORDER BY sc.s_score DESC LIMIT 1, 2 ) c;
23. 统计各科成绩各分数段人数:课程编号,课程名称,[100-85],[85-70],[70-60],[0-60]及所占百分比
SELECT c.c_id, c.c_name, ( 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 ) AS '100-85', (( 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 )) AS '85-70', (( 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 )) AS '70-60', (( 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 )) AS '85-70' FROM course c ORDER BY c.c_id
24. 查询学生平均成绩及其名次
SET @i = 0; SELECT a.*, @i := @i + 1 FROM ( 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 ) AS agvScore FROM student st LEFT JOIN score sc ON sc.s_id = st.s_id GROUP BY st.s_id ORDER BY agvScore DESC ) a
25. 查询各科成绩前三名的记录
SELECT a.* FROM ( SELECT st.s_id, st.s_name, c.c_id, c.c_name, sc.s_score FROM student st LEFT JOIN score sc ON sc.s_id = st.s_id INNER JOIN course c ON c.c_id = sc.c_id AND c.c_id = '01' ORDER BY sc.s_score DESC LIMIT 0, 3 ) a UNION ALL SELECT b.* FROM ( SELECT st.s_id, st.s_name, c.c_id, c.c_name, sc.s_score FROM student st LEFT JOIN score sc ON sc.s_id = st.s_id INNER JOIN course c ON c.c_id = sc.c_id AND c.c_id = '02' ORDER BY sc.s_score DESC LIMIT 0, 3 ) b UNION ALL SELECT c.* FROM ( SELECT st.s_id, st.s_name, c.c_id, c.c_name, sc.s_score FROM student st LEFT JOIN score sc ON sc.s_id = st.s_id INNER JOIN course c ON c.c_id = sc.c_id AND c.c_id = '03' ORDER BY sc.s_score DESC LIMIT 0, 3 ) c
26. 查询每门课程被选修的学生数
SELECT c.c_id, c.c_name, COUNT( 1 ) FROM course c LEFT JOIN score sc ON sc.c_id = c.c_id INNER JOIN student st ON st.s_id = c.c_id GROUP BY c.c_id