MySQL 50题练习
表名和字段
– 1.学生表
- Student(s_id,s_name,s_birth,s_sex) –学生编号,学生姓名, 出生年月,学生性别
– 2.课程表
- Course(c_id,c_name,t_id) – –课程编号, 课程名称, 教师编号
– 3.教师表
- Teacher(t_id,t_name) –教师编号,教师姓名
– 4.成绩表
- Score(s_id,c_id,s_score) –学生编号,课程编号,分数
- set sql_mode = '';
- set sql_mode = 'NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES';
测试数据
建表
-
学生表
CREATE TABLEStudent
(
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 TABLECourse
(
c_id
VARCHAR(20),
c_name
VARCHAR(20) NOT NULL DEFAULT '',
t_id
VARCHAR(20) NOT NULL,
PRIMARY KEY(c_id
)
); -
教师表
CREATE TABLETeacher
(
t_id
VARCHAR(20),
t_name
VARCHAR(20) NOT NULL DEFAULT '',
PRIMARY KEY(t_id
)
); -
成绩表
CREATE TABLEScore
(
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);
MySQL50题
-
1、查询"01"课程比"02"课程成绩高的学生的信息及课程分数
- 解法1:分别把课程01的成绩和课程02的成绩查询出来之后再比较
SELECT a.*, d.01_score, d.02_score FROM student a INNER JOIN
(SELECT b.s_id, b.01_score, c.02_score FROM
(SELECT s_id, s_score as 01_score FROM score WHERE c_id='01')b INNER JOIN
(SELECT s_id, s_score as 02_score FROM score WHERE c_id='02')c ON b.s_id=c.s_id
WHERE b.01_score > c.02_score)d ON a.s_id=d.s_id; - 解法2:全部查出来多重条件筛选
SELECT a.*, b.s_score as 01_score, c.s_score as 02_score FROM student a, score b, score c
WHERE a.s_id = c.s_id AND
a.s_id = b.s_id AND
b.c_id = '01' AND
c.c_id = '02' AND
b.s_score > c.s_score; - 解法3:
select a.* ,b.s_score as 01_score,c.s_score as 02_score from
student a
join score b on a.s_id=b.s_id and b.c_id='01'
left join score c on a.s_id=c.s_id and c.c_id='02' or c.c_id = NULL where b.s_score>c.s_score;
- 解法1:分别把课程01的成绩和课程02的成绩查询出来之后再比较
-
2、查询"01"课程比"02"课程成绩低的学生的信息及课程分数
- 同上题解法
-
3、查询平均成绩大于等于60分的同学的学生编号和学生姓名和平均成绩
SELECT a.s_name, ROUND(AVG(b.s_score), 2) as avg_score
FROM student a JOIN score b ON a.s_id=b.s_id
GROUP BY a.s_id
HAVING avg_score >= 60
ORDER BY avg_score DESC; -
4、查询平均成绩小于60分的同学的学生编号和学生姓名和平均成绩 (包括有成绩的和无成绩的)
SELECT b.s_name, b.s_id, ROUND(SUM(a.s_score)/COUNT(a.c_id),2) as avg_score
FROM score a JOIN student b ON a.s_id=b.s_id
GROUP BY a.s_id HAVING avg_score < 60
UNION SELECT a.s_name, a.s_id, 0 as avg_score FROM student a WHERE a.s_id NOT in (SELECT DISTINCT s_id FROM score); -
5、查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩
SELECT a.s_id, a.s_name, COUNT(b.c_id)as sum_course, SUM(b.s_score) as sum_score FROM student a JOIN score b on a.s_id=b.s_id GROUP BY a.s_id,a.s_name
UNION SELECT a.s_id, a.s_name, 0 as sum_sourse, 0 as sum_score FROM student a WHERE a.s_id NOT in (SELECT DISTINCT s_id FROM score); -
6、查询"李"姓老师的数量
SELECT COUNT(t_id) as '李姓老师数量' FROM teacher WHERE t_name LIKE "李%" GROUP BY t_id; -
7、查询学过"张三"老师授课的同学的信息
SELECT a.* FROM student a, score b, teacher c, course d
WHERE a.s_id=b.s_id AND
b.c_id = d.c_id AND
c.t_id = d.t_id AND
c.t_name = '张三'; -
8、查询没学过"张三"老师授课的同学的信息
SELECT a.* FROM student a WHERE a.s_id NOT in (SELECT a.s_id FROM student a, score b, teacher c, course d
WHERE a.s_id=b.s_id AND
b.c_id = d.c_id AND
c.t_id = d.t_id AND
c.t_name = '张三'); -
9、查询学过编号为"01"并且也学过编号为"02"的课程的同学的信息
SELECT a.* FROM student a JOIN
(SELECT a.s_id FROM (SELECT s_id FROM score WHERE c_id='01')a INNER JOIN (SELECT s_id FROM score WHERE c_id='02')b on a.s_id=b.s_id)d on a.s_id=d.s_id; -
10、查询学过编号为"01"但是没有学过编号为"02"的课程的同学的信息
SELECT c.* FROM student c JOIN
(SELECT a.s_id FROM score a WHERE a.c_id='01' AND a.s_id NOT in (SELECT s_id FROM score WHERE c_id='02'))d ON c.s_id=d.s_id; -
11、查询没有学全所有课程的同学的信息
SELECT a.*, COUNT(b.c_id) as sum_course FROM student a JOIN score b ON a.s_id=b.s_id GROUP BY a.s_id HAVING sum_course < 3; -
12、查询至少有一门课与学号为"01"的同学所学相同的同学的信息
SELECT DISTINCT a.* FROM student a JOIN score b ON a.s_id=b.s_id WHERE b.c_id in
(SELECT c.c_id FROM score c WHERE c.s_id='01'); -
13、查询和"01"号的同学学习的课程完全相同的其他同学的信息
- GROUP_CONCAT(expr) 配合 GROUP BY 可以将同一个分组中产生的值连接起来
-
SELECT c.*, d.course FROM student c JOIN
(SELECT a.s_id, a.course FROM
(SELECT s_id, GROUP_CONCAT(c_id) as course FROM score GROUP BY s_id)a JOIN (SELECT s_id, GROUP_CONCAT(c_id) as course FROM score GROUP BY s_id HAVING s_id='01')b
ON a.course=b.course)d ON c.s_id=d.s_id WHERE c.s_id not in (1); -
14、查询各科成绩最高分、最低分和平均分: 以如下形式显示:
-
课程 ID,课程 name,最高分,最低分,平均分,及格率,中等率,优良率,优秀率
-
及格为>=60,中等为:70-80,优良为:80-90,优秀为:>=90 要求输出课程号和选修人数,查询结果按人数降序排列,若人数相同,按课程号升序排列
SELECT a.c_id, b.c_name, MAX(a.s_score) as max_score, MIN(a.s_score) as min_score, ROUND(AVG(a.s_score), 2) as avg_score, COUNT(a.s_id)as count_stu,
CONCAT(ROUND(SUM(CASE when a.s_score >= 60 THEN 1 ELSE 0 END)/COUNT(), 2)100, '%') as '及格率',
CONCAT(ROUND(SUM(CASE when a.s_score >= 70 and a.s_score < 80 THEN 1 ELSE 0 END)/COUNT(), 2)100, '%') as '中等率',
CONCAT(ROUND(SUM(CASE when a.s_score >= 80 and a.s_score < 80 THEN 1 ELSE 0 END)/COUNT(),2)100, '%') as '优良率',
CONCAT(ROUND(SUM(CASE when a.s_score >= 90 THEN 1 ELSE 0 END)/COUNT(),2)100, '%') as '优秀率'
FROM score a JOIN course b ON a.c_id=b.c_id GROUP BY a.c_id ORDER BY count_stu DESC, a.c_id ASC; -
15、按各科成绩进行排序,并显示排名, Score 重复时保留名次空缺
-
15.1 按各科成绩进行排序,并显示排名, Score 重复时合并名次
select a.s_id,a.c_id,
@i:=@i +1 as i, #保留排名,
@k:=(case when @score=a.s_score then @k else @i end) as rank, #不保留排名,
@score:=a.s_score as score
from (
select s_id,c_id,s_score from score GROUP BY s_id,c_id,s_score ORDER BY s_score DESC
)a,(select @k:=0,@i:=0,@score:=0)s; -
16、查询学生的总成绩,并进行排名,总分重复时保留名次空缺
-
16.1 查询学生的总成绩,并进行排名,总分重复时不保留名次空缺
SELECT a.s_id, a.sum_score,
@i:=@i+1 as i, # 保留排名
@k:=(CASE WHEN @score=a.sum_score THEN @k ELSE @i END) as rank, # 不保留排名
@score:=a.sum_score as score FROM
(SELECT s_id, sum(s_score)as sum_score FROM score GROUP BY s_id ORDER BY sum_score DESC)a, (SELECT @k:=0, @i:=0, @score:=0)s; -
17、统计各科成绩各分数段人数:课程编号,课程名称,[100-85],[85-70],[70-60],[60-0] 及所占百分比
SELECT score.c_id, c_name,
CONCAT(ROUND(sum(CASE WHEN s_score <=100 and s_score>85 THEN 1 ELSE 0 END)/COUNT(),2)100,'%') as '[100-85]',
CONCAT(ROUND(sum(CASE WHEN s_score <=85 and s_score>70 THEN 1 ELSE 0 END)/COUNT(),2)100,'%') as '[85-70]',
CONCAT(ROUND(sum(CASE WHEN s_score <=70 and s_score>60 THEN 1 ELSE 0 END)/COUNT(),2)100,'%') as '[70-60]',
CONCAT(ROUND(sum(CASE WHEN s_score <=60 and s_score>=0 THEN 1 ELSE 0 END)/COUNT(),2)100,'%') as '[60-0]'
FROM score JOIN course ON score.c_id=course.c_id GROUP BY score.c_id; -
18、查询各科成绩前三名的记录
(SELECT c_id, s_score FROM score WHERE c_id = '01' ORDER BY s_score DESC LIMIT 3)
UNION (SELECT c_id, s_score FROM score WHERE c_id = '02' ORDER BY s_score DESC LIMIT 3)
UNION (SELECT c_id, s_score FROM score WHERE c_id = '03' ORDER BY s_score DESC LIMIT 3); -
19、查询每门课程被选修的学生数
SELECT c_id, COUNT(s_id)as sum_stu FROM score GROUP BY c_id; -
20、查询出只选修两门课程的学生学号和姓名
SELECT a.s_id, b.s_name, COUNT(a.c_id)as sum_course FROM score a JOIN student b ON a.s_id=b.s_id GROUP BY a.s_id HAVING sum_course = 2; -
21、查询男生、女生人数
SELECT s_sex, COUNT(s_id)as sex_count FROM student GROUP BY s_sex; -
22、查询名字中含有「风」字的学生信息
SELECT * FROM student WHERE s_name LIKE '%风%'; -
23、查询同名同性学生名单,并统计同名人数
SELECT s_name,s_sex, count(*
)as '同名人数'
FROM student
GROUP BY s_name,s_sex
HAVING COUNT(*
) >1; -
24、查询 1990 年出生的学生名单
SELECT * FROM student WHERE YEAR(s_birth)='1990'; -
25、查询每门课程的平均成绩,结果按平均成绩降序排列,平均成绩相同时,按课程编号升序排列
SELECT c_id, ROUND(AVG(s_score),2)as avg_score FROM score GROUP BY c_id ORDER BY avg_score DESC, c_id; -
26、查询平均成绩大于等于 85 的所有学生的学号、姓名和平均成绩
SELECT a.s_id,b.s_name, ROUND(SUM(a.s_score)/COUNT(a.c_id), 2) as avg_score FROM score a JOIN student b on a.s_id=b.s_id GROUP BY a.s_id ORDER BY avg_score DESC -
27、查询课程名称为「数学」,且分数低于 60 的学生姓名和分数
SELECT a.s_id, b.c_name,c.s_name, a.s_score FROM score a JOIN course b on a.c_id=b.c_id JOIN student c ON a.s_id=c.s_id WHERE b.c_name='数学' AND a.s_score < 60; -
28、查询所有学生的课程及分数情况(存在学生没成绩,没选课的情况)
SELECT * FROM score -
29、查询任何一门课程成绩在 70 分以上的学生姓名、课程名称和分数
- 取反,先把所有课程成绩在70分以下的学生id找出来(总课程计数和70以下课程计数一样的就是全部课程成绩在70分以下的),取反就是任何一门课程成绩在70分以上的同学
SELECT d.s_name, f.c_name, e.s_score FROM student d JOIN score e ON d.s_id=e.s_id JOIN course f ON e.c_id = f.c_id WHERE d.s_id NOT in
(SELECT a.s_id FROM
(SELECT s_id, COUNT(c_id)as course_count FROM score GROUP BY s_id)a JOIN
(SELECT s_id, COUNT(c_id)as course_count FROM score WHERE s_score < 70 GROUP BY s_id)b ON (a.course_count=b.course_count and a.s_id=b.s_id));
- 取反,先把所有课程成绩在70分以下的学生id找出来(总课程计数和70以下课程计数一样的就是全部课程成绩在70分以下的),取反就是任何一门课程成绩在70分以上的同学
-
30、查询不及格的课程
SELECT a.s_id,b.s_name, c.c_name, a.s_score FROM score a JOIN student b ON a.s_id=b.s_id JOIN course c ON a.c_id=c.c_id WHERE s_score < 60; -
31、查询课程编号为01且课程成绩在80分以上的学生的学号和姓名
SELECT a.s_id, b.s_name, a.c_id,a.s_score FROM score a JOIN student b ON a.s_id=b.s_id WHERE a.c_id='01' AND a.s_score >= 80; -
32、求每门课程的学生人数
SELECT c_id, COUNT(s_id) as count_stu FROM score GROUP BY c_id; -
33、查询选修"张三"老师所授课程的学生中,成绩最高的学生信息及其成绩
SELECT d.*, b.c_name, c.s_score FROM teacher a
JOIN course b ON a.t_id=b.t_id
JOIN score c ON b.c_id=c.c_id
JOIN student d ON c.s_id=d.s_id
WHERE a.t_name='张三' ORDER BY c.s_score DESC LIMIT 1; -
select a.*,b.s_score,b.c_id,c.c_name from student a
LEFT JOIN score b on a.s_id = b.s_id
LEFT JOIN course c on b.c_id=c.c_id
where b.c_id =(select c_id from course c,teacher d where c.t_id=d.t_id and d.t_name='张三')
and b.s_score in (select MAX(s_score) from score where c_id='02') -
34、查询不同课程成绩相同的学生的学生编号、课程编号、学生成绩
select DISTINCT b.s_id,b.c_id,b.s_score from score a,score b where a.c_id != b.c_id and a.s_score = b.s_score; -
35、查询每门功成绩最好的前两名
-
查询两张score表,找出b表中比a表中单科成绩高的计数(双循环比较 O(n^2)),小于等于2那就是找到了前两位
select a.s_id,a.c_id,a.s_score from score a
where (select COUNT(1) from score b where b.c_id=a.c_id and b.s_score>=a.s_score)<=2 ORDER BY a.c_id; -
36、统计每门课程的学生选修人数(超过5人的课程才统计)。要求输出课程号和选修人数,查询结果按人数降序排列,若人数相同,按课程号升序排列
SELECT c_id, COUNT(s_id)as stu_count FROM score GROUP BY c_id HAVING stu_count > 5 ORDER BY stu_count DESC, c_id; -
37、检索至少选修两门课程的学生学号
SELECT s_id, COUNT(c_id)as course_count FROM score GROUP BY s_id HAVING course_count >= 2; -
38、查询选修了全部课程的学生信息
SELECT b.*,COUNT(a.c_id) as course_count FROM score a JOIN student b ON a.s_id=b.s_id GROUP BY a.s_id HAVING course_count=(SELECT COUNT(c_id) FROM course); -
39、查询各学生的年龄
- 按照出生日期来算,当前月日 < 出生年月的月日则,年龄减一
-
SELECT s_name, s_birth, (DATE_FORMAT(CURDATE(),'%Y')-DATE_FORMAT(s_birth,'%Y') -
(CASE WHEN DATE_FORMAT(CURDATE(),'%m%d') > DATE_FORMAT(s_birth,'%m%d')THEN 0 ELSE 1 END ))as age FROM student; -
40、查询本周过生日的学生
SELECT s_name FROM student WHERE WEEK(CURDATE()) = WEEK(s_birth); -
41、查询下周过生日的学生
SELECT s_name FROM student WHERE WEEK(CURDATE())+1 = WEEK(s_birth); -
42、查询本月过生日的学生
SELECT s_name FROM student WHERE MONTH(CURDATE()) = MONTH(s_birth) -
43、查询本月过生日的学生
SELECT s_name FROM student WHERE MONTH(CURDATE())+1 = MONTH(s_birth)