目录
10、查询学过编号为"01"但是没有学过编号为"02"的课程的同学的信息:
11、查询没有学全所有课程的同学的信息:
12、查询至少有一门课与学号为"01"的同学所学相同的同学的信息:
13、查询和"01"号的同学学习的课程完全相同的其他同学的信息:
14、查询没学过"张三"老师讲授的任一门课程的学生姓名:
15、查询两门及其以上不及格课程的同学的学号,姓名及其平均成绩:
16、检索"01"课程分数小于60,按分数降序排列的学生信息:
17、按平均成绩从高到低显示所有学生的所有课程的成绩以及平均成绩:
18、查询各科成绩最高分、最低分和平均分:以如下形式显示:课程ID,课程name,最高分,最低分,平均分,及格率,中等率,优良率,优秀率:
19、按各科成绩进行排序,并显示排名(实现不完全):
SELECT student.s_id, s_name, s_birth, s_sex
FROM student, score sc1
WHERE student.s_id = sc1.s_id AND sc1.c_id = '01' AND NOT EXISTS
(SELECT *
FROM score sc2
WHERE student.s_id = sc2.s_id AND sc2.c_id = '02');
SELECT *
FROM student
WHERE EXISTS
(SELECT *
FROM course
WHERE NOT EXISTS
(SELECT *
FROM score
WHERE score.s_id = student.s_id AND score.c_id = course.c_id));
SELECT *
FROM student
WHERE EXISTS
(SELECT *
FROM score sc1
WHERE sc1.s_id = '01' AND EXISTS
(SELECT *
FROM score sc2
WHERE sc2.s_id = student.s_id AND sc1.c_id = sc2.c_id));
SELECT student.*
FROM (SELECT *
FROM score
WHERE s_id NOT IN
(SELECT s_id
FROM score
WHERE c_id NOT IN
(SELECT c_id
FROM score
WHERE s_id = '01'))
AND s_id != '01') AS sc LEFT JOIN student ON sc.s_id = student.s_id
GROUP BY sc.s_id
HAVING COUNT(c_id) = (SELECT COUNT(c_id)
FROM score
WHERE s_id = '01');
SELECT s_name
FROM student
WHERE s_id NOT IN
(SELECT score.s_id
FROM score
WHERE score.s_id = student.s_id AND score.c_id IN
(SELECT course.c_id
FROM course, teacher
WHERE course.t_id = teacher.t_id AND t_name = '张三'));
SELECT student.s_id, s_name, AVG(s_score) avg
FROM student, score
WHERE student.s_id = score.s_id
GROUP BY score.s_id
HAVING COUNT(s_score < 60 OR NULL) >= 2;
SELECT student.*, s_score
FROM student, score
WHERE student.s_id = score.s_id AND score.c_id = '01' AND score.s_score < 60
GROUP BY score.s_id
ORDER BY s_score DESC;
SELECT score.*, avg
FROM score, (SELECT s_id, AVG(sc1.s_score) avg
FROM score sc1
GROUP BY sc1.s_id) AS sc
WHERE score.s_id = sc.s_id
ORDER BY avg DESC;
--及格为 >= 60,中等为:70 ~ 80,优良为:80 ~ 90,优秀为:>=90
SELECT score.c_id '课程ID', c_name '课程名称', MAX(s_score) '最高分', MIN(s_score) '最低分', AVG(s_score) '平均分',
((SELECT COUNT(s_id)
FROM score
WHERE s_score >= 60 AND c_id = course.c_id) /(SELECT COUNT(s_id)
FROM score
WHERE c_id = course.c_id)) "及格率",
((SELECT COUNT(s_id)
FROM score
WHERE s_score >= 70 AND s_score < 80 AND c_id = course.c_id) / (SELECT COUNT(s_id)
FROM score
WHERE c_id = course.c_id)) "中等率",
((SELECT COUNT(s_id)
FROM score
WHERE s_score >= 80 AND s_score < 90 AND c_id = course.c_id) / (SELECT COUNT(s_id)
FROM score
WHERE c_id = course.c_id)) "优良率",
((SELECT COUNT(s_id)
FROM score
WHERE s_score >= 90 AND c_id = course.c_id ) / (SELECT COUNT(s_id)
FROM score
WHERE c_id = course.c_id)) "优秀率"
FROM course LEFT JOIN score ON score.c_id = course.c_id
GROUP BY course.c_id;
SELECT sc1.*, count(sc2.s_score) + 1 ranking
FROM score sc1 LEFT JOIN score sc2 ON sc1.c_id = sc2.c_id AND sc1.s_score < sc2.s_score
GROUP BY sc1.c_id, sc1.s_id
ORDER BY sc1.c_id, ranking