准备SQL数据:
-- 建表
-- 学生表
CREATE TABLE `Student`(
`s_id` VARCHAR(20),
`s_name` VARCHAR(20) NOT NULL DEFAULT '',
`s_birth` VARCHAR(20) NOT NULL DEFAULT '',
`s_sex` VARCHAR(10) NOT NULL DEFAULT '',
PRIMARY KEY(`s_id`)
);
-- 课程表
CREATE TABLE `Course`(
`c_id` VARCHAR(20),
`c_name` VARCHAR(20) NOT NULL DEFAULT '',
`t_id` VARCHAR(20) NOT NULL,
PRIMARY KEY(`c_id`)
);
-- 教师表
CREATE TABLE `Teacher`(
`t_id` VARCHAR(20),
`t_name` VARCHAR(20) NOT NULL DEFAULT '',
PRIMARY KEY(`t_id`)
);
-- 成绩表
CREATE TABLE `Score`(
`s_id` VARCHAR(20),
`c_id` VARCHAR(20),
`s_score` INT(3),
PRIMARY KEY(`s_id`,`c_id`)
);
-- 插入学生表测试数据
insert into Student values('01' , '赵雷' , '1990-01-01' , '男');
insert into Student values('02' , '钱电' , '1990-12-21' , '男');
insert into Student values('03' , '孙风' , '1990-05-20' , '男');
insert into Student values('04' , '李云' , '1990-08-06' , '男');
insert into Student values('05' , '周梅' , '1991-12-01' , '女');
insert into Student values('06' , '吴兰' , '1992-03-01' , '女');
insert into Student values('07' , '郑竹' , '1989-07-01' , '女');
insert into Student values('08' , '王菊' , '1990-01-20' , '女');
-- 课程表测试数据
insert into Course values('01' , '语文' , '02');
insert into Course values('02' , '数学' , '01');
insert into Course values('03' , '英语' , '03');
-- 教师表测试数据
insert into Teacher values('01' , '张三');
insert into Teacher values('02' , '李四');
insert into Teacher values('03' , '王五');
-- 成绩表测试数据
insert into Score values('01' , '01' , 80);
insert into Score values('01' , '02' , 90);
insert into Score values('01' , '03' , 99);
insert into Score values('02' , '01' , 70);
insert into Score values('02' , '02' , 60);
insert into Score values('02' , '03' , 80);
insert into Score values('03' , '01' , 80);
insert into Score values('03' , '02' , 80);
insert into Score values('03' , '03' , 80);
insert into Score values('04' , '01' , 50);
insert into Score values('04' , '02' , 30);
insert into Score values('04' , '03' , 20);
insert into Score values('05' , '01' , 76);
insert into Score values('05' , '02' , 87);
insert into Score values('06' , '01' , 31);
insert into Score values('06' , '03' , 34);
insert into Score values('07' , '02' , 89);
insert into Score values('07' , '03' , 98);
50到练习题
-- 1、查询"01"课程比"02"课程成绩高的学生的信息及课程分数
-- 2、查询"01"课程比"02"课程成绩低的学生的信息及课程分数
-- 3、查询平均成绩大于等于60分的同学的学生编号和学生姓名和平均成绩
SELECT stu.s_id, stu.s_name, avg(sco.s_score)
FROM Student AS stu, Score AS sco WHERE stu.s_id = sco.s_id
GROUP BY sco.s_id HAVING avg(sco.s_score) >= 60;
-- 4、查询平均成绩小于60分的同学的学生编号和学生姓名和平均成绩(包括有成绩的和无成绩的)
SELECT b.s_id, b.s_name, ROUND(AVG(a.s_score),2) AS avg_score
FROM student b LEFT JOIN score a on b.s_id = a.s_id
GROUP BY b.s_id HAVING avg_score < 60
UNION
SELECT a.s_id, a.s_name, 0 as avg_score from student a where a.s_id not in (select distinct s_id from score);
-- 5、查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩(包括有成绩的和无成绩的), 并按照成绩从高到低排序
-- COUNT(sco.c_id), SUM(sco.s_score)
SELECT stu.s_id, stu.s_name, COUNT(sco.c_id) AS sum_course, SUM(sco.s_score) AS sum_score
FROM Student AS stu LEFT JOIN Score AS sco ON stu.s_id = sco.s_id
GROUP BY stu.s_id ORDER BY sum_score DESC;
-- 6、查询"李"姓老师的数量
SELECT COUNT(t_id) FROM teacher WHERE t_name LIKE '李%';
-- 7、查询学过"张三"老师授课的同学的信息
-- 实现思路,根据老师名字查询老师的id,SELECT t_id FROM teacher WHERE t_name = '张三'
-- 根据老师的id查询课程id集合 SELECT c_id FROM course WHERE t_id = (SELECT t_id FROM teacher WHERE t_name = '张三')
-- 学生与关联分数表,查询课程id在老师授课id集合中的学生信息
SELECT stu.*
FROM student AS stu JOIN score AS sco
WHERE stu.s_id = sco.s_id
AND sco.c_id IN (SELECT c_id FROM course WHERE t_id = (SELECT t_id FROM teacher WHERE t_name = '张三'));
-- 8、查询没学过"张三"老师授课的同学的信息
-- 根据第7题查询出了学过张三老师的课程的学生信息,则只需从学生信息表中排除学过张三考试的课程学生即可;
-- 错误答案:将第7题中的 sco.c_id IN 改为 sco.c_id NOT IN 以为就可用满足,但因为有些学生可能同时学习了张三老师和其他老师的课程,这样查询出的结果并不是没学过张三老师的课程
SELECT stu.*
FROM student AS stu
WHERE stu.s_id NOT IN (SELECT stu.s_id
FROM student AS stu JOIN score AS sco
WHERE stu.s_id = sco.s_id
AND sco.c_id IN (SELECT c_id FROM course WHERE t_id = (SELECT t_id FROM teacher WHERE t_name = '张三')));
-- 9、查询学过编号为"01"并且也学过编号为"02"的课程的同学的信息
SELECT stu.*
FROM student AS stu, score AS sco
WHERE stu.s_id = '01' AND stu.s_id = sco.s_id
AND sco.c_id = '02';
-- 10、查询学过编号为"01"但是没有学过编号为"02"的课程的同学的信息
SELECT stu.*
FROM student AS stu
WHERE stu.s_id IN (SELECT s_id FROM score WHERE c_id = '01')
AND stu.s_id IN (SELECT s_id FROM score WHERE c_id != '02');
-- 11、查询没有学全所有课程的同学的信息
-- 实现思路:1、将socre根据学生id进行分组,计算出总的课程数量,之后过滤掉课程总数不等于course表总数的数据,将查询结果作为临时表,与学生表进行关联查询,得出没有将全部课程学习完的学生信息
-- 2、对于没有成绩的学生,则认为没有学习过任何课程,也既认为没有学全所有课程,所以讲1步骤的数据合并上没有成绩的学生
SELECT stu.* , sco.sum_course AS sum_course
FROM student AS stu ,
(SELECT s_id, COUNT(c_id) AS sum_course FROM score GROUP BY s_id HAVING sum_course != (SELECT COUNT(*) FROM course)) AS sco
WHERE stu.s_id = sco.s_id
UNION
SELECT stu_1.* , 0 AS sum_course FROM student AS stu_1 WHERE stu_1.s_id NOT IN (SELECT s_id FROM score);
-- 12、查询至少有一门课与学号为"01"的同学所学相同的同学的信息
-- 实现思路:1、先查询出学号01学生所学的所有课程c_id集合;2、查询score表中课程c_id在第一步中的数据;3、关联学生表查询出学生信息
SELECT stu.*
FROM student AS stu WHERE stu.s_id IN (SELECT DISTINCT s_id FROM score WHERE c_id IN (SELECT c_id FROM score WHERE s_id = '01'));
-- 13、查询和"01"号的同学学习的课程完全相同的其他同学的信息
-- 实现思路:1、查询01号同学所学的课程;2、查询课程c_id在01号学生课程中,并且课程总数与01号学生课程总数相等(则认为课程完全相同)的学生s_id;3、学生信息表关联第二步查询结果,过滤掉学号为01的其他学生信息
SELECT stu.* FROM student AS stu
WHERE stu.s_id != '01'
AND stu.s_id IN (SELECT DISTINCT s_id
FROM score WHERE c_id IN (SELECT c_id FROM score WHERE s_id = '01')
GROUP BY s_id HAVING COUNT(c_id) = (SELECT COUNT(c_id) AS sum_course FROM score WHERE s_id = '01'));
-- 14、查询没学过"张三"老师讲授的任一门课程的学生姓名
-- 此题与第8题相同,实现思路还是先查询出学过张三老师课程的学生,在从学生信息中排除学过的学生信息,得出没有学过任一门课程的学生名称
SELECT stu.s_name
FROM student AS stu
WHERE stu.s_id NOT IN (SELECT DISTINCT s_id FROM score WHERE c_id IN (SELECT c_id FROM course WHERE t_id IN (SELECT t_id FROM teacher WHERE t_name = '张三')));
-- 15、查询两门及其以上不及格课程的同学的学号,姓名及其平均成绩
-- 查询有2门以上不及格的学生
SELECT s_id, ROUND(AVG(s_score), 2) AS avg_score FROM score WHERE s_score < 60 GROUP BY s_id having count(*)>=2;
SELECT stu.s_id, stu.s_name, AVG(sco.s_score)
FROM student AS stu LEFT JOIN score AS sco ON stu.s_id = sco.s_id
WHERE stu.s_id IN (SELECT s_id FROM score WHERE s_score < 60 GROUP BY s_id HAVING COUNT(*) >= 2)
GROUP BY stu.s_id;
-- 16、检索"01"课程分数小于60,按分数降序排列的学生信息
-- 先找出课程01且分数小于60的学生id和分数
SELECT s_id, s_score FROM score WHERE s_score < 60 AND c_id = '01';
SELECT stu.*
FROM student AS stu, score AS sco
WHERE stu.s_id = sco.s_id AND sco.c_id = '01' AND sco.s_score < 60 ORDER BY sco.s_score DESC;
-- 17、按平均成绩从高到低显示所有学生的所有课程的成绩以及平均成绩
-- 平均成绩
AVG(sco.s_score)
-- 所以课程的成绩
SUM(sco.s_score)
ORDER BY sco.s_score DESC
SELECT sco.s_id,
(SELECT s_score FROM score WHERE s_id=sco.s_id AND c_id = '01') AS 语文,
(SELECT s_score FROM score WHERE s_id=sco.s_id AND c_id = '02') AS 数学,
(SELECT s_score FROM score WHERE s_id=sco.s_id AND c_id = '03') AS 英语,
SUM(sco.s_score) AS 总成绩, AVG(sco.s_score) AS 平均成绩
FROM score AS sco
GROUP BY sco.s_id
ORDER BY 平均成绩 DESC;
-- 18.查询各科成绩最高分、最低分和平均分:以如下形式显示:课程ID,课程name,最高分,最低分,平均分,及格率,中等率,优良率,优秀率
-- 及格为>=60,中等为:70-80,优良为:80-90,优秀为:>=90
MAX(sco.s_score)
MIN(sco.s_score)
AVG(sco.s_score)
-- 算出各科的总学生数量
SELECT COUNT(s_id) FROM score WHERE c_id = '01';
-- 各科成绩
SELECT s_score FROM score WHERE c_id = '01' AS 语文
SELECT s_score FROM c_id = '02' AS 数学
SELECT s_score FROM c_id = '03' AS 英语
SELECT cou.c_id, cou.c_name, MAX(sco.s_score), MIN(sco.s_score), AVG(sco.s_score),
ROUND(100 * (SUM(CASE WHEN sco.s_score >= 60 THEN 1 ELSE 0 END) / SUM(CASE WHEN sco.s_score THEN 1 ELSE 0 END)), 2) AS 及格率,
ROUND(100 * (SUM(CASE WHEN sco.s_score BETWEEN 70 AND 80 THEN 1 ELSE 0 END) / SUM(CASE WHEN sco.s_score THEN 1 ELSE 0 END)), 2) AS 中等率,
ROUND(100 * (SUM(CASE WHEN sco.s_score BETWEEN 80 AND 90 THEN 1 ELSE 0 END) / SUM(CASE WHEN sco.s_score THEN 1 ELSE 0 END)), 2) AS 优良率,
ROUND(100 * (SUM(CASE WHEN sco.s_score >= 90 THEN 1 ELSE 0 END) / SUM(CASE WHEN sco.s_score THEN 1 ELSE 0 END)), 2) AS 优秀率
FROM score AS sco LEFT JOIN course AS cou
ON cou.c_id = sco.c_id
GROUP BY sco.c_id;
-- 19、按各科成绩进行排序,并显示排名(实现不完全)
-- 20、查询学生的总成绩并进行排名
SELECT sco.s_id, sco.sum_score, (@i:=@i+1) AS 排名
FROM (SELECT s_id, SUM(s_score) AS sum_score FROM score GROUP BY s_id) AS sco, (SELECT @i:=0) AS t
ORDER BY sco.sum_score DESC;
-- 21、查询不同老师所教不同课程平均分从高到低显示
-- 22、查询所有课程的成绩第2名到第3名的学生信息及该课程成绩
-- 23、统计各科成绩各分数段人数:课程编号,课程名称,[100-85],[85-70],[70-60],[0-60]及所占百分比
-- 24、查询学生平均成绩及其名次
-- 25、查询各科成绩前三名的记录
-- 1.选出b表比a表成绩大的所有组
-- 2.选出比当前id成绩大的 小于三个的
-- 26、查询每门课程被选修的学生数
-- 27、查询出只有两门课程的全部学生的学号和姓名
-- 28、查询男生、女生人数
-- 29、查询名字中含有"风"字的学生信息
-- 30、查询同名同性学生名单,并统计同名人数
select a.s_name,a.s_sex,count(*) from student a JOIN
student b on a.s_id !=b.s_id and a.s_name = b.s_name and a.s_sex = b.s_sex
GROUP BY a.s_name,a.s_sex
-- 31、查询1990年出生的学生名单
-- 32、查询每门课程的平均成绩,结果按平均成绩降序排列,平均成绩相同时,按课程编号升序排列
-- 33、查询平均成绩大于等于85的所有学生的学号、姓名和平均成绩
-- 34、查询课程名称为"数学",且分数低于60的学生姓名和分数
-- 35、查询所有学生的课程及分数情况;
-- 36、查询任何一门课程成绩在70分以上的姓名、课程名称和分数;
-- 37、查询不及格的课程
-- 38、查询课程编号为01且课程成绩在80分以上的学生的学号和姓名;
-- 39、求每门课程的学生人数
-- 40、查询选修"张三"老师所授课程的学生中,成绩最高的学生信息及其成绩
-- 41、查询不同课程成绩相同的学生的学生编号、课程编号、学生成绩
-- 42、查询每门功成绩最好的前两名
-- 43、统计每门课程的学生选修人数(超过5人的课程才统计)。要求输出课程号和选修人数,查询结果按人数降序排列,若人数相同,按课程号升序排列
-- 44、检索至少选修两门课程的学生学号
-- 45、查询选修了全部课程的学生信息
-- 46、查询各学生的年龄
-- 按照出生日期来算,当前月日 < 出生年月的月日则,年龄减一
-- 47、查询本周过生日的学生
-- 48、查询下周过生日的学生
-- 49、查询本月过生日的学生
-- 50、查询下月过生日的学生