MySQL经典练习题及答案,常用SQL语句练习50题,题目来源
https://www.cnblogs.com/Diyo/p/11424844.html
环境准备
- 建表语句
#–1.学生表
#Student(s_id,s_name,s_birth,s_sex) –学生编号,学生姓名, 出生年月,学生性别
CREATE TABLE `Student` (
`s_id` VARCHAR(20),
s_name VARCHAR(20) NOT NULL DEFAULT '',
s_brith VARCHAR(20) NOT NULL DEFAULT '',
s_sex VARCHAR(10) NOT NULL DEFAULT '',
PRIMARY KEY(s_id)
);
#–2.课程表
#Course(c_id,c_name,t_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)
);
/*
–3.教师表
Teacher(t_id,t_name) –教师编号,教师姓名
*/
CREATE TABLE Teacher(
t_id VARCHAR(20),
t_name VARCHAR(20) NOT NULL DEFAULT '',
PRIMARY KEY(t_id)
);
/*
–4.成绩表
Score(s_id,c_id,s_score) –学生编号,课程编号,分数
*/
Create table Score(
s_id VARCHAR(20),
c_id VARCHAR(20) not null default '',
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);
数据预览
- 学生表
s_id | s_name | s_bin | s_sex |
---|---|---|---|
01 | 赵雷 | 1990-01-01 | 男 |
02 | 钱电 | 1990-12-21 | 男 |
03 | 孙风 | 1990-05-20 | 男 |
04 | 李云 | 1990-08-06 | 男 |
05 | 周梅 | 1991-12-01 | 女 |
06 | 吴兰 | 1992-03-01 | 女 |
07 | 郑竹 | 1989-07-01 | 女 |
08 | 王菊 | 1990-01-20 | 女 |
- 课程表
c_id | c_name | t_id |
---|---|---|
01 | 语文 | 02 |
02 | 数学 | 01 |
03 | 英语 | 03 |
- 教师表
t_id | t_name |
---|---|
01 | 张三 |
02 | 李四 |
03 | 王五 |
- 成绩表
s_id | c_id | s_score |
---|---|---|
01 | 01 | 80 |
01 | 02 | 90 |
01 | 03 | 99 |
02 | 01 | 70 |
02 | 02 | 60 |
02 | 03 | 80 |
03 | 01 | 80 |
03 | 02 | 80 |
03 | 03 | 80 |
04 | 01 | 50 |
04 | 02 | 30 |
04 | 03 | 20 |
05 | 01 | 76 |
05 | 02 | 87 |
06 | 01 | 31 |
06 | 03 | 34 |
07 | 02 | 89 |
07 | 03 | 98 |
题目解析
1. 查询"01"课程比"02"课程成绩高的学生的信息及课程分数
select s.*,
a.s_score as score_01,
b.s_score as score_02
from score a,
score b,
student s
where a.s_id = b.s_id
and a.c_id = '01'
and b.c_id = '02'
and a.s_score > b.s_score
and s.s_id = a.s_id;
2. 查询"01"课程比"02"课程成绩低的学生的信息及课程分数
select s.*,
a.s_score as score_01,
b.s_score as score_02
from score a,
score b,
student s
where a.s_id = b.s_id
and a.c_id = '01'
and b.c_id = '02'
and a.s_score < b.s_score
and s.s_id = a.s_id;
3. 查询平均成绩大于等于60分的同学的学生编号和学生姓名和平均成绩
select stu.*, avg(s_score) as avg_score
from score sc,
student stu
where sc.s_id = stu.s_id
group by s_id
having avg(s_score) >= 60;
4. 查询平均成绩小于60分的同学的学生编号和学生姓名和平均成绩
select stu.*, IFNULL(avg(s_score), 0) as avg_score
from student stu
left join score sc
on sc.s_id = stu.s_id
group by s_id
having IFNULL(avg(s_score), 0) < 60;
5. 查询所有同学的学生编号. 学生姓名. 选课总数. 所有课程的总成绩
select stu.*, count(sc.c_id) as course_sum,
ifnull(sum(sc.s_score), 0) as score_sum
from student stu
left join score sc
on stu.s_id = sc.s_id
group by stu.s_id;
select count(1)
from teacher
where t_name like '李%';
select stu.*,c.c_name,t.t_name
from student stu,
course c,
score sc,
teacher t
where c.c_id = sc.c_id
and t.t_id = c.t_id
and sc.s_id = stu.s_id
and t.t_name = '张三';
8. 查询没学过"张三"老师授课的同学的信息
这个题目有个小技巧,就是用not in
select *
from student
where s_id not in
(select stu.s_id
from student stu,
course c,
score sc,
teacher t
where c.c_id = sc.c_id
and t.t_id = c.t_id
and sc.s_id = stu.s_id
and t.t_name = '张三');
9. 查询学过编号为"01"并且也学过编号为"02"的课程的同学的信息
select stu.*
from score a,
score b,
student stu
where a.c_id = '01'
and b.c_id = '02'
and a.s_id = b.s_id
and stu.s_id = a.s_id;
10. 查询学过编号为"01"但是没有学过编号为"02"的课程的同学的信息
# 长型变整型
select stu.*
from (select sc.s_id,
max(case when sc.c_id = '01' then s_score end) as score_01,
max(case when sc.c_id = '02' then s_score end) as score_02,
max(case when sc.c_id = '03' then s_score end) as score_03
from score sc
group by s_id) t,
student stu
where t.score_01 is not null
and t.score_02 is null
and t.s_id = stu.s_id;
select stu.*
from student stu
left join score sc
on stu.s_id = sc.s_id
group by stu.s_id
having count(sc.c_id) < (select count(c_id)
from course);
12. 查询至少有一门课与学号为"01"的同学所学相同的同学的信息
select distinct stu.*
from student stu
left join score sc
on stu.s_id = sc.s_id
where sc.c_id in (select c_id from score where s_id = '01');
13. 查询和"01"号的同学学习的课程完全相同的其他同学的信息
select *
from student
where s_id not in
(select s_id
from (select t.*, sc.c_id cid2
from (select *
from student
, (select c_id from score where s_id = '01') t) t
left join score sc
on t.s_id = sc.s_id
and t.c_id = sc.c_id
union
select t.*, sc.c_id cid2
from (select *
from student
, (select c_id from score where s_id = '01') t) t
right join score sc
on t.s_id = sc.s_id
and t.c_id = sc.c_id) tt
where cid2 is null
group by s_id)
and s_id != '01';
select stu.*
from student stu
where s_id not in
(select s_id
from score
where score.c_id in
(select c_id
from course
where t_id = (select t_id from teacher where t_name = '张三'))
group by s_id);
15. 查询两门及其以上不及格课程的同学的学号,姓名及其平均成绩
select stu.s_id, stu.s_name, ifnull(avg(score.s_score), 0)
from student stu
left join score
on stu.s_id = score.s_id
group by stu.s_id
having sum(case when score.s_score >= 60 then 0 else 1 end) >= 2
16. 检索"01"课程分数小于60,按分数降序排列的学生信息
17. 按平均成绩从高到低显示所有学生的所有课程的成绩以及平均成绩
18. 查询各科成绩最高分. 最低分和平均分:以如下形式显示:课程ID,课程name,最高分,最低分,平均分,及格率,中等率,优良率,优秀率
22. 查询所有课程的成绩第2名到第3名的学生信息及该课程成绩
23. 统计各科成绩各分数段人数:课程编号,课程名称,[100-85],[85-70],[70-60],[0-60]及所占百分比
32. 查询每门课程的平均成绩,结果按平均成绩降序排列,平均成绩相同时,按课程编号升序排列
33. 查询平均成绩大于等于85的所有学生的学号. 姓名和平均成绩
34. 查询课程名称为"数学",且分数低于60的学生姓名和分数
36. 查询任何一门课程成绩在70分以上的姓名. 课程名称和分数;
40. 查询选修"张三"老师所授课程的学生中,成绩最高的学生信息及其成绩
41. 查询不同课程成绩相同的学生的学生编号. 课程编号. 学生成绩
43. 统计每门课程的学生选修人数(超过5人的课程才统计)。要求输出课程号和选修人数,查询结果按人数降序排列,若人数相同,按课程号升序排列
评论区