MySQL 50道练习题及答案

练习题参考知乎
https://zhuanlan.zhihu.com/p/50662216
如下是根据本身思路编写的代码,部分参考了知乎,途中遇到了很多问题
整个实践过程当中老是忘记指定字段所在的表名,出现以下错误
ERROR 1052 (23000): Column ‘’ in field list is ambiguous,
指定列重复,即两张表存在相同字段,没有在表字段前指明表名,致使指代不清楚,只要表名.字段名 指定便可web

#创建学生表 编号(主键) 学生姓名 生日 性别

CREATE TABLE student(
s_id int PRIMARY key auto_increment,
s_name VARCHAR(20) not null,
s_birthday VARCHAR(20) not null,
s_sex VARCHAR(20) not null
);数据库

#创建课程表 编号(主键) 课程名 教师

CREATE TABLE course(
c_id int auto_increment,
c_name VARCHAR(20) not null,
t_id int not null,
PRIMARY KEY(c_id)
);svg

#创建教师表 编号(主键)教师姓名

CREATE TABLE teacher(
t_id int auto_increment ,
t_name VARCHAR(20) not null
);
ALTER TABLE teacher ADD CONSTRAINT pk_id PRIMARY key(t_id);学习

#创建成绩表 编号(主键) 课程编号(主键) 成绩

CREATE TABLE score(
s_id int ,
c_id int ,
s_score FLOAT(4,2),
PRIMARY KEY(s_id,c_id)
);xml

#插入学生表数据

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’);rem

#插入教师表数据

insert into Teacher values(‘01’ , ‘张三’);
insert into Teacher values(‘02’ , ‘李四’);
insert into Teacher values(‘03’ , ‘王五’);get

#插入成绩表数据

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);数学

#查询表

SELECT * from student;
SELECT * from course;
SELECT * from score;
SELECT * from teacher;it

#一、查询课程编号为“01”的课程比“02”的课程成绩高的全部学生的学号。

SELECT s_id from score s1 inner join score s2 using(s_id)
where s1.s_id=s2.s_id and s1.c_id=‘01’ and s2.c_id=‘02’ and s1.s_score>s2.s_score;

#二、查询平均成绩大于60分的学生的学号和平均成绩

SELECT s_id,avg(s_score) 平均成绩 from score
GROUP BY s_id HAVING avg(s_score)>60;

#三、查询全部学生的学号、姓名、选课数、总成绩

SELECT s_id 学号,s_name 姓名,count(s_id) 选课数,sum(s_score) 总成绩 FROM student inner join score USING(s_id) GROUP BY s_id;

#四、查询姓“张”的老师的个数

SELECT count(t_id) from teacher where t_name like ‘张%’;

#5.查询没学过“张三”老师课的学生的学号、姓名(重点)

SELECT s_id,s_name from student where s_id not in
(SELECT s_id from score where c_id = (SELECT c_id from course inner join teacher USING(t_id) where t_name=‘张三’));

#六、查询学过“张三”老师所教的全部课的同窗的学号、姓名

SELECT s_id,s_name from student where s_id in
(SELECT s_id from score inner join course USING(c_id) join teacher USING(t_id) WHERE t_name=‘张三’);

#七、查询学过编号为“01”的课程而且也学过编号为“02”的课程的学生的学号、姓名

SELECT s_id,s_name from student inner join score USING(s_id)
where s_id in(SELECT s_id from score left join course USING(c_id)
where c_id=‘01’) and c_id=‘02’;

#八、查询课程编号为“02”的总成绩

SELECT sum(s_score) 总成绩 from score where c_id=‘02’;

#九、查询全部课程成绩小于60分的学生的学号、姓名

SELECT s_id,s_name from student where s_id not in
(SELECT s_id from score GROUP BY s_id HAVING max(s_score)>=60);

#十、查询没有学全全部课的学生的学号、姓名

SELECT s_id,s_name from student join score USING(s_id)
GROUP BY s_id HAVING count(c_id) <(SELECT count(1) from course);

#十一、查询至少有一门课与学号为“01”的学生所学课程相同的学生的学号和姓名

SELECT DISTINCT s_id,s_name from student join score USING(s_id)
where c_id in
( SELECT c_id from score where s_id=‘01’)
and s_id <>‘01’;

#十二、查询和“01”号同窗所学课程彻底相同的其余同窗的学号

SELECT s_id,count(score.c_id) from student sc join score USING(s_id) where c_id in
(SELECT c_id from score where s_id=‘01’) and
s_id<>‘01’ GROUP BY s_id HAVING count(1)=
(SELECT count(s_id) from score where s_id=‘01’) and
(SELECT count(1) from score where s_id=‘01’)=(SELECT count(1) from score where s_id=sc.s_id);

#13.把“Score”表中“张三”老师教的课的成绩都更改成此课程的平均成绩

update score a join
(select avg(s_score) t, Score.c_id from score
join course USING(c_id)
join teacher USING(t_id)
where t_name =‘张三’ group by c_id) b
USING(c_id)
set a.s_score= b.t;

#1四、查询和“02”号的同窗学习的课程彻底相同的其余同窗学号和姓名(同12题)

SELECT s_id,count(score.c_id) from student sc join score USING(s_id)
where c_id in
(SELECT c_id from score where s_id=‘02’) and
s_id<>‘02’ GROUP BY s_id HAVING count(1)=
(SELECT count(s_id) from score where s_id=‘02’) and
(SELECT count(1) from score where s_id=‘02’)=
(SELECT count(1) from score where s_id=sc.s_id);

#1五、删除学习“张三”老师课的SC表记录

DELETE from score where c_id in
(SELECT c_id from course JOIN teacher USING(t_id) where t_name=‘张三’);

#16.检索"01"课程分数小于60,按分数降序排列的学生信息

SELECT * from student JOIN score USING(s_id)
where c_id=‘01’ and s_score<60
ORDER BY s_score desc

#1七、按平均成绩从高到低显示全部学生的“数据库”(c_id=‘04’)、“企业管理”(c_id=‘01’)、“英语”(c_id=‘06’)三门的课程成绩,按以下形式显示:学生ID,数据库,企业管理,英语,有效课程数,有效平均分

select s_id as ‘学生ID’,
(case when c_id=‘04’ then s_score else NULL end) as ‘数据库’,
(case when c_id=‘01’ then s_score else NULL end) as ‘企业管理’,
(case when c_id=‘06’ then s_score else NULL end) as ‘英语’,
count(c_id) as 有效课程数,
avg(s_score) as 有效平均分
from Score
group by s_id
order by avg(s_score) DESC;

#1八、查询各科成绩最高和最低的分:以以下的形式显示:课程ID,最高分,最低分(以前删除了张三老师的课程,因此不存在02课程)

SELECT c_id 课程ID,max(s_score) 最高分,min(s_score) 最低分 from score GROUP BY c_id;

#1九、按各科平均成绩从低到高和及格率的百分数从高到低排列,以以下形式显示:课程号,课程名,平均成绩,及格百分数

SELECT c_id 课程号,c_name 课程名, avg(s_score) 平均成绩,
concat((SELECT count(1) from score b where a.c_id=b.c_id and s_score>=60)/count(*)*100,"%") 及格百分数
from score a join course USING(c_id)
GROUP BY c_id
ORDER BY avg(s_score)

#2一、查询不一样老师所教不一样课程平均分从高到低显示

SELECT *,avg(s_score) 平均分 from score
join course USING(c_id)
join teacher USING(t_id)
GROUP BY t_id ORDER BY avg(s_score) desc

#2三、使用分段[100-85],[85-70],[70-60],[<60]来统计各科成绩,分别统计各分数段人数:课程ID和课程名称

SELECT score.c_id 课程ID,c_name 课程名称,
sum(case when s_score BETWEEN 85 and 100 then 1 else 0 end) ‘[100-85]’,
sum(case when s_score>=70 and s_score<85 then 1 else 0 end) ‘[85-70]’,
sum(case when s_score>=60 and s_score<70 then 1 else 0 end) ‘[70-60]’,
sum(case when s_score<60 then 1 else 0 end) ‘[<60]’
from score join course USING(c_id) GROUP BY score.c_id,c_name;

#2四、查询学平生均成绩及其名次

SELECT s_id 学号,avg,
@a:=@a+1 排名
from (SELECT *, avg(s_score) avg from score sc
GROUP BY s_id ORDER BY avg(s_score) desc) as a ,(SELECT @a:=0) as b;

#2五、查询各科成绩前三名的记录

SELECT * from score s1 where
(SELECT count(1) from score s2
where s1.s_score<s2.s_score
and s1.c_id=s2.c_id)<=2
order by c_id ,s_score desc;

#2六、查询每门课程被选修的学生数

SELECT count(1) from score
GROUP BY c_id;

#2七、查询出只选修了两门课程的所有学生的学号和姓名

SELECT s_id,s_name from student where s_id in
(SELECT s_id from score GROUP BY s_id HAVING count(c_id)=2);

#2八、查询男生、女生人数

SELECT s_sex,count(1) from student GROUP BY s_sex;

#2九、查询名字中含有“风”字的学生信息

SELECT * from student where s_name like ‘%风%’;

#30、查询同名同姓学生名单并统计同名人数

SELECT s_name,count(1) from student a
where s_name in (SELECT s_name from student b where a.s_id<>b.s_id);

#3一、1990年出生的学生名单

SELECT * from student where s_birthday like ‘1990%’;

#3二、查询平均成绩大于85的全部学生的学号、姓名和平均成绩

SELECT student.*,avg(s_score) from student join score
USING(s_id) GROUP BY s_id HAVING avg(s_score)>85 ;

#3三、查询每门课程的平均成绩,结果按平均成绩升序排序,平均成绩相同时,按课程号降序排列

SELECT avg(s_score) from score GROUP BY c_id ORDER BY avg(s_score),c_id desc;

#3四、查询课程名称为“数学”且分数低于60的学生姓名和分数

SELECT s_name,s_score from student
join score USING(s_id)
join course USING(c_id)
where c_name=‘数学’ and s_score<60;

#3五、查询全部学生的选课状况

SELECT DISTINCT s_id,s_name,c_id,c_name from student
join score USING(s_id)
join course USING(c_id);

#3六、查询任何一门课程成绩在70分以上的姓名、课程名称和分数

SELECT s_name,c_name,s_score from student
join score USING(s_id)
join course USING(c_id)
where s_id in
(SELECT s_id from score where s_score>70);

#3七、查询不及格的课程并按课程号从大到小排列

SELECT c_id,c_name,s_score from score
join course USING(c_id)
where s_score<60 ORDER BY c_id desc;

#3八、查询课程编号为03且课程成绩在80分以上的学生的学号和姓名

SELECT s_id,s_name from student
join score USING(s_id)
WHERE c_id=‘03’ and s_score>80;

#3九、查询选了课程的学生人数

SELECT count(DISTINCT s_id) from score ;

#40、查询选修“张三”老师所授课程的学生中成绩最高的学生姓名及其成绩

SELECT s_name,s_score from student
join score USING(s_id)
join course USING(c_id)
join teacher USING(t_id)
where t_name=‘张三’ and
s_id=(SELECT s_id from score ORDER BY s_score desc LIMIT 1)

#4一、查询各个课程及相应的选修人数

SELECT c_name,count(1) 选课人数 from score join course USING(c_id) GROUP BY c_id;

#4二、查询不一样课程成绩相同的学生的学生编号、课程编号、学生成绩

SELECT DISTINCT s1.s_id, s1.c_id,s1.s_score from score s1
join score s2 USING(s_id)
where s1.c_id<>s2.c_id and s1.s_score=s2.s_score;

#4三、查询每门课程成绩最好的前两名

SELECT * from score s1 where
(SELECT count(1) from score s2
where s1.c_id=s2.c_id and s1.s_score<s2.s_score)<=1
ORDER BY c_id;

#4四、统计每门课程的学生选修人数(超过5人的课程才统计)。要求输出课程号和选修人数,查询结果按人数降序排序,若人数相同,按课程号升序排序

SELECT c_name,count(s_id) from score
join course USING(c_id)
GROUP BY c_id HAVING count(s_id)>5
ORDER BY count(s_id) desc,c_id;

#4五、查询至少选修两门课程的学生学号

SELECT s_id from score GROUP BY s_id HAVING count(c_id)>=2;

#4六、查询选修了所有课程的学生信息

SELECT *from student
join score USING(s_id)
GROUP BY s_id HAVING count(c_id)=
(SELECT count(c_id) from course);

#4七、查询没学过“张三”老师讲授的任一门课程的学生姓名

SELECT * from student where s_id not in
(SELECT s_id from score
join course USING(c_id)
join teacher USING(t_id)
where t_name=‘张三’);

#4八、查询两门以上不及格课程的同窗的学号及其平均成绩

SELECT s_id,avg(s_score) from score
where s_score<60
GROUP BY s_id
HAVING count(c_id)>=2;

#4九、检索课程编号为“03”且分数小于60的学生学号,结果按按分数降序排列

SELECT s_id from score where c_id=‘03’ and s_score<60
ORDER BY s_score desc;

#50、删除学生编号为“02”的课程编号为“01”的成绩

DELETE from score where s_id=‘02’ and c_id=‘01’;