题目:
请创建如下表,并创建相关约束
班级表:class 学生表:student
cid caption grade_id sid sname gender class_id
1 一年一班 1 1 乔丹 女 1
2 二年一班 2 2 艾弗森 女 1
3 三年二班 3 3 科比 男 2
老师表:teacher 课程表:course
tid tname cid cname teacher_id
1 张三 1 生物 1
2 李四 2 体育 1
3 王五 3 物理 2
成绩表:score 年级表:class_grade
sid student_id course_id score gid gname
1 1 1 60 1 一年级
2 1 2 59 2 二年级
3 2 2 99 3 三年级
班级任职表:teach2cls
tcid tid cid
1 1 1
2 1 2
3 2 1
4 3 2
二、操作表
1、自行创建测试数据;
2、查询学生总人数;
3、查询“生物”课程和“物理”课程成绩都及格的学生id和姓名;
4、查询每个年级的班级数,取出班级数最多的前三个年级;
5、查询平均成绩最高和最低的学生的id和姓名以及平均成绩;
6、查询每个年级的学生人数;
7、查询每位学生的学号,姓名,选课数,平均成绩;
8、查询学生编号为“2”的学生的姓名、该学生成绩最高的课程名、成绩最低的课程名及分数;
9、查询姓“李”的老师的个数和所带班级数;
10、查询班级数小于5的年级id和年级名;
11、查询班级信息,包括班级id、班级名称、年级、年级级别(12为低年级,34为中年级,56为高年级),示例结果如下;
班级id 班级名称 年级 年级级别
1 一年一班 一年级 低
12、查询学过“张三”老师2门课以上的同学的学号、姓名;
13、查询教授课程超过2门的老师的id和姓名;
14、查询学过编号“1”课程和编号“2”课程的同学的学号、姓名;
15、查询没有带过高年级的老师id和姓名;
16、查询学过“张三”老师所教的所有课的同学的学号、姓名;
17、查询带过超过2个班级的老师的id和姓名;
18、查询课程编号“2”的成绩比课程编号“1”课程低的所有同学的学号、姓名;
19、查询所带班级数最多的老师id和姓名;
20、查询有课程成绩小于60分的同学的学号、姓名;
21、查询没有学全所有课的同学的学号、姓名;
22、查询至少有一门课与学号为“1”的同学所学相同的同学的学号和姓名;
23、查询至少学过学号为“1”同学所选课程中任意一门课的其他同学学号和姓名;
24、查询和“2”号同学学习的课程完全相同的其他同学的学号和姓名;
25、删除学习“张三”老师课的score表记录;
26、向score表中插入一些记录,这些记录要求符合以下条件:①没有上过编号“2”课程的同学学号;②插入“2”号课程的平均成绩;
27、按平均成绩从低到高显示所有学生的“语文”、“数学”、“英语”三门的课程成绩,按如下形式显示: 学生ID,语文,数学,英语,有效课程数,有效平均分;
28、查询各科成绩最高和最低的分:以如下形式显示:课程ID,最高分,最低分;
29、按各科平均成绩从低到高和及格率的百分数从高到低顺序;
30、课程平均分从高到低显示(现实任课老师);
31、查询各科成绩前三名的记录(不考虑成绩并列情况)
32、查询每门课程被选修的学生数;
33、查询选修了2门以上课程的全部学生的学号和姓名;
34、查询男生、女生的人数,按倒序排列;
35、查询姓“张”的学生名单;
36、查询同名同姓学生名单,并统计同名人数;
37、查询每门课程的平均成绩,结果按平均成绩升序排列,平均成绩相同时,按课程号降序排列;
38、查询课程名称为“数学”,且分数低于60的学生姓名和分数;
39、查询课程编号为“3”且课程成绩在80分以上的学生的学号和姓名;
40、求选修了课程的学生人数
41、查询选修“王五”老师所授课程的学生中,成绩最高和最低的学生姓名及其成绩;
42、查询各个课程及相应的选修人数;
43、查询不同课程但成绩相同的学生的学号、课程号、学生成绩;
44、查询每门课程成绩最好的前两名学生id和姓名;
45、检索至少选修两门课程的学生学号;
46、查询没有学生选修的课程的课程号和课程名;
47、查询没带过任何班级的老师id和姓名;
48、查询有两门以上课程超过80分的学生id及其平均成绩;
49、检索“3”课程分数小于60,按分数降序排列的同学学号;
50、删除编号为“2”的同学的“1”课程的成绩;
51、查询同时选修了物理课和生物课的学生id和姓名;
答案:
创建表:
create table class(
cid int primary key auto_increment,
caption char(6),
grade_id int not null
);
insert class(caption,grade_id) values ('一年一班',1),('二年一班',2),('三年二班',3);
create table student(
sid int primary key auto_increment,
sname char(6) not null,
gender enum('女','男') default '男',
class_id int not null ,
foreign key(class_id) references class(cid)
on delete cascade
on update cascade
);
insert student(sname,gender,class_id) values ('乔丹','女',1),('艾弗森','女',1),('科比','男',2);
create table teacher(
tid int auto_increment,
tname char(6) not null,
primary key(tid)
);
insert teacher (tname) values ('张三'),('李四'),('王五');
create table course(
cid int primary key auto_increment,
cname char(6) not null,
teacher_id int,
foreign key(teacher_id) references teacher(tid)
on delete cascade
on update cascade
);
insert course(cname,teacher_id) values('生物',1),('体育',1),('物理',2);
create table score(
sid int auto_increment,
student_id int not null,
course_id int not null,
score int ,
primary key (sid),
foreign key (student_id) references student(sid) on delete cascade on update cascade,
foreign key (course_id) references course(cid) on delete cascade on update cascade);
insert score (student_id, course_id,score) values(1,1,60),(1,2,59),(2,2,99);
create table class_grade(
gid int auto_increment,
gname char(6) not null,
primary key (gid)
);
insert class_grade(gname) values ('一年级'),('二年级'),('三年级');
create table teach2cls(
tcid int primary key auto_increment,
tid int not null,
cid int not null,
foreign key(tid) references teacher(tid) on delete cascade on update cascade,
foreign key (cid) references class(cid) on delete cascade on update cascade);
insert teach2cls (tid,cid) values(1,1),(1,2),(2,1),(3,2);
补充数据
insert class_grade(gname) values ('四年级'),('五年级'),('六年级');
insert class(caption,grade_id) values ('四年一班',4),('五年三班',5),('五年一班',5),('六年二班',6);
insert teacher (tname) values ('赵柳'),('孙策'),('狄仁杰'),("李新");
insert teach2cls (tid,cid) values(4,6),(5,5),(6,4),(6,2);
insert course(cname,teacher_id) values('语文',4),('数学',5),('英语',6),('化学',3),('美术',4);
insert student(sname,gender,class_id) values
('alsa','女',4),('lily','女',5),('jack','男',6),('alsa','女',7),
('lucy','女',1),('bob','男',3),('乔丹','女',4),('nico','女',5),
('alex','男',3),('张良',男',4),('lily','女',2),('jack','男',7),
("kitty",'女',5),('诸葛','男',5),('妲己','女',4),('甄姬','女',6);
insert score (student_id, course_id,score) values
(4,1,58),(5,2,88),(6,3,73),(16,4,75),(16,5,92),
(7,4,65),(8,5,98),(9,6,72),(16,6,88),(17,7,63),(18,7,77),(19,7,90),
(11,3,81),(10,5,85),(12,1,63),(12,2,87),
(13,6,55),(14,3,55),(15,5,41),(6,1,80);
答案:
# 2、查询学生总人数;
select
count(sid)
from
student;
# 3、查询“生物”课程和“物理”课程成绩都及格的学生id和姓名;
select
sid,sname
from
student where sid in(
select
student_id
from
score
inner join
course
on
score.course_id = course.cid
where
cname='生物' or cname='物理' and score>=60
group by
score.student_id
having
count(course_id)=2
);
# 4、查询每个年级的班级数,取出班级数最多的前三个年级;
select
grade_id,
count(grade_id) as num
from
class
group by
grade_id
order by count(grade_id) desc
limit 3;
# 5、查询平均成绩最高和最低的学生的id和姓名以及平均成绩;
select
sid,
sname,
t1.avg_score
from
student
inner join(
select
student_id,
avg(score)as avg_score
from
score
group by
student_id
having
avg(score) in(
(select avg(score)as low_score from score group by student_id order by avg(score) limit 1),
(select avg(score)as high_score from score group by student_id order by avg(score) desc limit 1))
)as t1
on
student.sid=t1.student_id;
# 6、查询每个年级的学生人数;
select
gname,
count(sid)as student_count
from
student
inner join(
select
*
from
class_grade
inner join
class
on
class_grade.gid=class.grade_id
)as t1
on
student.class_id=t1.cid
group by
gid;
# 7、查询每位学生的学号,姓名,选课数,平均成绩;
select
sid,
sname,
t1.total_course,
t1.avg_score
from
student
left join(
select
student_id,
count(course_id)as total_course,
avg(score)as avg_score
from
score
group by
student_id
)as t1
on
student.sid=t1.student_id;
# 8、查询学生编号为“2”的学生的姓名、该学生成绩最高的课程名、成绩最低的课程名及分数;
select
sid,
sname,
t1.course_id,
t1.score
from
student
inner join(
select
student_id,
course_id,
score
from
score
where
score in(
(select score from score where student_id=2 order by score desc limit 1),
(select score from score where student_id=2 order by score limit 1 )
)
)as t1
on
student.sid=t1.student_id;
# 9、查询姓“李”的老师的个数和所带班级数;
select
total_teach,
count(teach2cls.cid)as total_class
from
teach2cls
inner join(
select
tid,
count(tid)as total_teach
from
teacher
where
tname like "李%"
)as teach
on
teach2cls.tid=teach.tid
group by
teach2cls.tid;
# 10、查询班级数小于5的年级id和年级名;
select
gid, gname from class_grade
inner join(
select
grade_id,
count(cid) as class_count
from
class
group by
grade_id
) as t1
on
class_grade.gid = t1.grade_id
where
t1.class_count<5;
# 11、查询班级信息,包括班级id、班级名称、年级、年级级别(12为低年级,34为中年级,56为高年级),示例结果如下;
班级id 班级名称 年级 年级级别
1 一年一班 一年级 低
select
class.cid as '班级id',
class.caption as '班级名称',
class_grade.gname as '年级',
case when class_grade.gid between 1 and 2 then '低'
when class_grade.gid between 3 and 4 then '中'
when class_grade.gid between 5 and 6 then '高' else 0 end as '年级级别'
from
class,
class_grade
where
class.grade_id = class_grade.gid;
# 12、查询学过“张三”老师2门课以上的同学的学号、姓名;
select
sid,
sname
from
student
where
sid in(
select
student_id
from
score
inner join(
select
cid
from
course
where
teacher_id =(
select
tid
from
teacher
where
tname='张三'
)
)as t1
on
score.course_id=t1.cid
group by
student_id
having
count(course_id)=2
);
# 13、查询教授课程超过2门的老师的id和姓名;
select
tid,
tname
from
teacher
where tid in (
select
teacher_id
from
course
group by
teacher_id
having
count(cid)>=2
);
# 14、查询学过编号“1”课程和编号“2”课程的同学的学号、姓名;
select
sid,
sname
from
student
where
sid in (
select
student_id
from
score
where
course_id='1' or course_id='2'
group by
student_id
having
count(course_id)=2
);
# 15、查询没有带过高年级的老师id和姓名
select
tid,
tname
from
teacher
where
tid not in(
select
distinct tid
from
teach2cls
where
cid in (
select
cid
from
class
where
grade_id in (
select
gid
from
class_grade
where
gname='五年级' or gname='六年级'
)
)
);
# 16、查询学过“张三”老师所教的所有课的同学的学号、姓名;
select
sid,
sname
from
student
where sid in (
select distinct student_id from score where course_id in (
select cid from course where teacher_id in(
select tid from teacher where tname='张三'
)
)
);
# 17、查询带过超过2个班级的老师的id和姓名;
select
tid,
tname
from
teacher
where tid in (
select
tid
from
teach2cls
group by
tid
having
count(cid)>2
);
# 18、查询课程编号“2”的成绩比课程编号“1”课程低的所有同学的学号、姓名;
select
sid,
sname
from student
where sid in (
select s1.student_id
from score as s1 inner join score as s2 on s1.student_id = s2.student_id and s1.course_id = 1 and s2.course_id = 2
where s1.score < s2.score);
# 19、查询所带班级数最多的老师id和姓名;
select
tid,
tname
from
teacher
where
tid in (
select
tid
from
teach2cls
group by
tid
having
count(cid)=(
select
count(cid)
from
teach2cls
group by
tid
order by
count(cid) desc
limit 1
)
);
# 20、查询有课程成绩小于60分的同学的学号、姓名;
select
sid,
sname
from
student
where
sid in (
select
student_id
from
score
where
score<60
);
# 21、查询没有学全所有课的同学的学号、姓名;
select
sid,
sname
from
student
left join (
select
student_id
from
score
group by
student_id
having
count(course_id)=(
select count(cid) from course)
)as t1
on
student.sid=t1.student_id
where
t1.student_id is null;
# 22、查询至少有一门课与学号为“1”的同学所学相同的同学的学号和姓名;
select
sid,sname
from
student
where
sid in (
select
distinct student_id
from
score
where
course_id in(
select
course_id
from
score
where
student_id =1
)
)
and sid !=1;
# 23、查询至少学过学号为“1”同学所选课程中任意一门课的其他同学学号和姓名;
select
distinct sid,
sname
from
student
where
sid in (
select
student_id
from
score
where
course_id in (
select
course_id
from
score
where
student_id=1
)
and student_id!=1
);
# 24、查询和“2”号同学学习的课程完全相同的其他同学的学号和姓名;
select
sid,
sname
from
student
where sid in (
select student_id from score,
(select course_id from score where student_id=2)as t1
where score.course_id = t1.course_id and score.student_id !=2 group by score.student_id
having count(score.course_id)=(select count(course_id)from score where student_id=2)
);
# 25、删除学习“张三”老师课的score表记录;
delete from score where course_id in (
select cid from course where teacher_id =(
select tid from teacher where tname='张三')
);
# 26、向score表中插入一些记录,这些记录要求符合以下条件:①没有上过编号“2”课程的同学学号;②插入“2”号课程的平均成绩;
insert score(student_id,course_id,score)
select t1.sid,2,t2.avg_score from(
(select sid from student where sid not in (select student_id from score where course_id = 2)) as t1,
(select avg(score)as avg_score from score where course_id = 2) as t2);
# 27、按平均成绩从低到高显示所有学生的“语文”、“数学”、“英语”三门的课程成绩,按如下形式显示: 学生ID,语文,数学,英语,有效课程数,有效平均分;
select
student_id,
(select score from score where course_id =(select cid from course where cname='语文') and score.student_id =s1.student_id ) as '语文',
(select score from score where course_id =(select cid from course where cname='数学') and score.student_id =s1.student_id ) as '数学',
(select score from score where course_id =(select cid from course where cname='英语') and score.student_id =s1.student_id ) as '英语',
count(course_id) as '有效课程数',
avg(score) as '有效平均分'
from
score as s1
group by
student_id
order by
avg(score);
# 28、查询各科成绩最高和最低的分:以如下形式显示:课程ID,最高分,最低分;
select
course_id as id,
max(score.score) as '最高分',
min(score.score) as '最低分'
from
course
left join score
on score.course_id=course.cid
group by course_id;
# 29、按各科平均成绩从低到高和及格率的百分数从高到低顺序;
select course_id,
avg(score) as avg_score,
sum(case when score.score >= 60 then 1 else 0 end) / count(sid) * 100 as percent
from
score
group by
course_id
order by
avg(score) asc,percent desc;
# 30、课程平均分从高到低显示(现实任课老师);
select
t1.cid,
t1.tname,
t2.avg_score
from(
select
teacher.tid as tid,
teacher.tname as tname,
course.cid as cid
from
teacher
inner join
course
on teacher.tid = teacher_id
)as t1
inner join
(select course_id,avg(score)as avg_score from score group by course_id )as t2
on
t1.cid=t2.course_id
order by
avg_score desc;
# 31、查询各科成绩前三名的记录(不考虑成绩并列情况)
select
student_id,
score,
course_id
from score r1
where (SELECT count(1)
from (select distinct
score,
course_id
from score) r2
where r2.course_id = r1.course_id AND r2.score > r1.score) <= 2
order by course_id, score DESC;
# 32、查询每门课程被选修的学生数;
select
course_id,
count(student_id)
from
score
group by
course_id;
# 33、查询选修了2门以上课程的全部学生的学号和姓名;
select
sid,
sname
from
student
where sid in(
select
student_id
from
score
group by
student_id
having
count(course_id)>2);
# 34、查询男生、女生的人数,按倒序排列;
select
gender,
count(sid)
from
student
group by
gender
order by
count(sid) desc;
# 35、查询姓“张”的学生名单;
select
*
from
student
where
sname like "张%";
# 36、查询同名同姓学生名单,并统计同名人数;
select
sname,
count(sid)
from
student
group by
sname
having
count(sid)>1;
# 37、查询每门课程的平均成绩,结果按平均成绩升序排列,平均成绩相同时,按课程号降序排列;
select
course_id,
avg(score)
from
score
group by
course_id
order by
avg(score),
course_id desc;
# 38、查询课程名称为“数学”,且分数低于60的学生姓名和分数;
select
student.sname,
t1.score
from
student
inner join (
select
student_id,
score
from
score
where score.score<60 and course_id in (
select
cid
from
course
where cname='数学'
)
)as t1
on
t1.student_id=student.sid;
# 39、查询课程编号为“3”且课程成绩在80分以上的学生的学号和姓名;
select
sid,
sname
from
student
where sid in(
select
student_id
from
score
where
course_id=3 and score>80
);
# 40、求选修了课程的学生人数
select
coalesce(student_id,"总人数")as id,
count(course_id)
from
score
group by
student_id
with rollup;
# 41、查询选修“王五”老师所授课程的学生中,成绩最高和最低的学生姓名及其成绩;
select
s1.student_id,
low_score,
s2.student_id,
high_score
from(
select
tid,
student_id,
score as low_score
from
(select student_id,cid,cname,score,tid
from score
inner join
(select tid,tname,cid,cname from teacher inner join course on teacher.tid=course.teacher_id where tname='王五')as t1
on score.course_id=t1.cid)as t2 order by score limit 1) as s1
inner join (
select tid,student_id,score as high_score from
(select student_id,cid,cname,score,tid
from score
inner join
(select tid,tname,cid,cname from teacher
inner join
course
on teacher.tid=course.teacher_id where tname='王五')as t1
on score.course_id=t1.cid)as t2 order by score desc limit 1) as s2
on s1.tid=s2.tid;
# 42、查询各个课程及相应的选修人数;
select
course_id ,
count(student_id)
from
score
group by
course_id;
# 43、查询不同课程但成绩相同的学生的学号、课程号、学生成绩;
select distinct
s1.course_id,
s1.student_id,
s1.score,
s2.course_id,
s2.student_id,
s2.score
from
score as s1,
score as s2
where
s1.score = s2.score and s1.course_id != s2.course_id;
# 44、查询每门课程成绩最好的前两名学生id和姓名;
select student.sid,student.sname,course.cname, score.score
from score
inner join (
select course_id, score, ranking
from (
select a.course_id, a.score, count(1) as ranking
from
(select course_id, score from score group by course_id, score order by course_id, score desc)as a
inner join
(select course_id, score from score group by course_id, score order by course_id, score desc)as b
on a.course_id = b.course_id and a.score <= b.score group by course_id, score
) as t1
where ranking in (1, 2) order by course_id, ranking)as s1
on score.course_id = s1.course_id and score.score = s1.score
inner join student
on score.student_id = student.sid
inner join course
on score.course_id = course.cid;
# 45、检索至少选修两门课程的学生学号;
select
student_id
from
score
group by
student_id
having
count(course_id)>=2;
# 46、查询没有学生选修的课程的课程号和课程名;
select
course.cid,
course.cname
from
course
left join
score
on
course.cid=score.course_id
where
score.student_id is null;
# 47、查询没带过任何班级的老师id和姓名;
select
teacher.tid,
tname
from
teacher
left join
teach2cls
on
teacher.tid=teach2cls.tid
where
teach2cls.tcid is null;
# 48、查询有两门以上课程超过80分的学生id及其平均成绩;
select
student_id,
avg(score)
from
score
where
score>80
group by
student_id
having
count(course_id)>2;
# 49、检索“3”课程分数小于60,按分数降序排列的同学学号;
select
student_id,
score
from
score
where
score<60 and course_id=3
order by
score desc;
# 50、删除编号为“2”的同学的“1”课程的成绩;
delete from score where student_id='2' and course_id='1';
# 51、查询同时选修了物理课和生物课的学生id和姓名;
select
student.sid,
student.sname
from
student
where sid in (
select
student_id
from
score
where
course_id IN (
select
cid
from
course
where cname = '物理' or cname = '生物'
)
group by
student_id
having
count(course_id) = 2
);