Mysql练习题

 1.创建数据库

show DATABASES;
create database test default charset utf8;
use test;
View Code

2.创建数据表

-- 1.创建表table
create table class(
cid int auto_increment primary key,
caption char(6)not null)engine=innodb default charset=utf8;

-- 2.创建表teacher
create table teacher(
tid int auto_increment primary key,
tname char(6) not null) engine=innodb default charset=utf8;

-- 3.创建表student
create table student(
sid int auto_increment primary key,
sname char(6) not null,
gender enum("男","女"),
class_id int,
constraint fk_stu_class foreign key(class_id) REFERENCES class(cid)
) engine=innodb default charset=utf8;

-- 4.创建表course
create table course(
cid int auto_increment primary key,
cname char(6) not null,
teacher_id int,
constraint fk_course_teacher FOREIGN key(teacher_id) REFERENCES teacher(tid)
)engine=innodb default charset=utf8;

-- 5.创建表score
create table score(
sid int auto_increment primary key,
student_id int,
course_id int,
num int,
constraint fk_score_student foreign key (student_id) REFERENCES student(sid),
constraint fk_score_course foreign key (course_id) REFERENCES course(cid)
)engine=innodb default charset=utf8;
创建数据表

3.插入数据

INSERT INTO `class` VALUES ('1', '三年二班'), ('2', '三年三班'), ('3', '一年二班'), ('4', '二年九班');

INSERT INTO `course` VALUES ('1', '生物', '1'), ('2', '物理', '2'), ('3', '体育', '3'), ('4', '美术', '2');

INSERT INTO `score` VALUES ('1', '1', '1', '10'), ('2', '1', '2', '9'), ('5', '1', '4', '66'), ('6', '2', '1', '8'), ('8', '2', '3', '68'), ('9', '2', '4', '99'), ('10', '3', '1', '77'), ('11', '3', '2', '66'), ('12', '3', '3', '87'), ('13', '3', '4', '99'), ('14', '4', '1', '79'), ('15', '4', '2', '11'), ('16', '4', '3', '67'), ('17', '4', '4', '100'), ('18', '5', '1', '79'), ('19', '5', '2', '11'), ('20', '5', '3', '67'), ('21', '5', '4', '100'), ('22', '6', '1', '9'), ('23', '6', '2', '100'), ('24', '6', '3', '67'), ('25', '6', '4', '100'), ('26', '7', '1', '9'), ('27', '7', '2', '100'), ('28', '7', '3', '67'), ('29', '7', '4', '88'), ('30', '8', '1', '9'), ('31', '8', '2', '100'), ('32', '8', '3', '67'), ('33', '8', '4', '88'), ('34', '9', '1', '91'), ('35', '9', '2', '88'), ('36', '9', '3', '67'), ('37', '9', '4', '22'), ('38', '10', '1', '90'), ('39', '10', '2', '77'), ('40', '10', '3', '43'), ('41', '10', '4', '87'), ('42', '11', '1', '90'), ('43', '11', '2', '77'), ('44', '11', '3', '43'), ('45', '11', '4', '87'), ('46', '12', '1', '90'), ('47', '12', '2', '77'), ('48', '12', '3', '43'), ('49', '12', '4', '87'), ('52', '13', '3', '87');

INSERT INTO `student` VALUES ('1',  '理解','', '1'), ('2','钢蛋','', '1'), ('3','张三', '', '1'), ('4','张一', '', '1'), ('5', '张二', '', '1'), ('6', '张四', '', '1'), ('7', '铁锤', '', '2'), ('8', '李三', '', '2'), ('9','李一', '', '2' ), ('10', '李二', '', '2'), ('11', '李四', '', '2'), ('12', '如花', '', '3'), ('13', '刘三','', '3'), ('14', '刘一', '', '3'), ('15', '刘二','', '3'), ('16', '刘四','', '3');

INSERT INTO `teacher` VALUES ('1', '张磊老师'), ('2', '李平老师'), ('3', '刘海燕老师'), ('4', '朱云海老师'), ('5', '李杰老师');
插入数据

4.查询数据

select * from class;
select * from student;
select * from score;
select * from teacher;
select * from course;
View Code

5.成绩表中大于60分的学生

select score.sid,student.sname from score
left join student on student.sid = score.student_id
where num>60;
Q5

6.查询“生物”课程比“物理”课程成绩高的所有学生的学号;

select sw.student_id,sw.num as"生物成绩",wl.num as "物理成绩"from
(select sid,student_id,num from score left join course on course.cid = score.course_id where course.cname="生物") as sw 
left join 
(select sid,student_id,num from score left join course on course.cid = score.course_id where course.cname="物理") as wl 
on sw.student_id = wl.student_id
where sw.num> if(isnull(wl.num),0,wl.num)
Q6

7.查询平均成绩大于60分的同学的学号和平均成绩

select student_id,avg(num) from score group by student_id having avg(num)>60;
Q7

8. 查询所有同学的学号、姓名、选课数、总成绩

select student_id as "学号",student.sname as"姓名",sum(num)as "总成绩",count(course_id)as "选课数"from score left join student on 
student.sid = score.student_id group by student_id
Q8

9.查询姓“李”的老师的个数

select count(1) from teacher where tname like "李%";
Q9

10.查询没学过“叶平”老师课的同学的学号、姓名;

select * from student where student.sid not in(
select score.student_id from score where score.course_id in(
select course.cid from course left join teacher on course.teacher_id = teacher.tid where tname="李平老师") group by student_id)
Q10

11.查询学过“001”并且也学过编号“002”课程的同学的学号、姓名

select score.student_id,student.sname from score left join student 
on student.sid = score.student_id 
where score.course_id =1 or score.course_id = 2 group by student_id having count(course_id) >1
Q11

12.查询学过“叶平”老师所教的所有课的同学的学号、姓名;

select  student_id,sname from score left join student on score.student_id = student.sid where score.course_id in
(select course.cid from course left join teacher on teacher.tid = course.teacher_id where teacher.tname= "李平老师")
group by student_id 
having count(course_id) = (select count(course.cid)
from course left join teacher on teacher.tid = course.teacher_id where teacher.tname= "李平老师")
Q12

13.查询课程编号“002”的成绩比课程编号“001”课程低的所有同学的学号、姓名

select student.sid,student.sname from
(select A.student_id from
(select score.student_id,score.num as num1 from score where course_id = 1) as A
left join
(select score.student_id,score.num as num2 from score where course_id = 2) as B
on A.student_id = B.student_id where num1<if(isnull(num2),0,num2))as C
left join 
student on student.sid = C.student_id
Q13

14.查询有课程成绩小于60分的同学的学号、姓名;

select student_id,student.sname from score left join student on student.sid = score.student_id where num<60 group by student_id
Q14

15.查询没有学全所有课的同学的学号、姓名

select student_id,student.sname,count(course_id) from score left join student on score.student_id = student.sid
group by student_id having count(1)<(select count(1)from course)
Q15

16.查询至少有一门课与学号为“001”的同学所学相同的同学的学号和姓名;

获取 001 同学选择的所有课程
获取课程在其中的所有人以及所有课程
根据学生筛选,获取所有学生信息
select student_id,sname from score left join student on student.sid= score.student_id where student_id !=1 and course_id in(select course_id from score where student_id = 1) group by student_id
Q16

17.查询至少学过学号为“001”同学所有课的其他同学学号和姓名

 先找到和001的学过的所有人
select student.sid,student.sname from score left join student on student.sid = score.student_id 
where course_id in (select course_id from score where student_id = 1) and student_id != 1
group by student_id 
having count(course_id) >= (select count(course_id) from score where student_id = 1)
Q17

18.查询和“002”号的同学学习的课程完全相同的其他同学学号和姓名;

select student_id from score where student_id in(
--  获取与学号2相同课程数的学生id
    select student_id from score where student_id !=2 group by student_id having count(1) = (select count(1) from score where student_id = 2))
    and 
--     获取与学号2相同的课程数(只到这一步没有having显示课程数的话可能出现相同课程数,但是选课有不同的学生)
    course_id in (select course_id from score where student_id =2 ) 
--     必须与学号2相同课程数且相同选课的限制条件
    group by student_id having count(1) = (select count(1) from score where student_id = 2)
Q18

19.删除学习“叶平”老师课的score表记录;

delete from score left join course on course.cid = score.course_id
left join teacher on teacher.tid = course.teacher_id
where teacher.tname = "叶平老师"
Q19

20.向SC表中插入一些记录,这些记录要求符合以下条件:

①没有上过编号“002”课程的同学学号;②插入“002”号课程的平均成绩;

insert into score(student_id,course_id,num) values
select sid,2,(select avg(num)from score where course_id = 2)
from student where sid in 
(select distinct student_id from score where student_id not in
(select student_id from score where course_id = 2))
Q20

21.按平均成绩从低到高 显示所有学生的“生物”、“物理”、“体育”三门的课程成绩,

按如下形式显示: 学生ID,语文,数学,英语,有效课程数,有效平均分;

select * from score left join
(select* from score left join course on course.cid = score.course_id where course.cname="生物")as"sw"
on 
(select* from score left join course on course.cid = score.course_id where course.cname="物理")as"wl"

(select* from score left join course on course.cid = score.course_id where course.cname="体育")as"ty"
Q21

22.查询各科成绩最高和最低的分:以如下形式显示:课程ID,最高分,最低分

select course_id,max(num),min(num) from score group by course_id 
Q22

23.按各科平均成绩从低到高和及格率的百分数从高到低顺序;

select course_id,avg(num) as avg, sum(case when num<60 then 0 else 1 end)/sum(1) as "jgl"
from score  group by course_id order by avg desc,jgl desc
Q23

24. 课程平均分从高到低显示(现实任课老师)

select course.cname,teacher.tname,avg(num) as avg from score
left join course on course.cid = score.course_id
left join teacher on teacher.tid = course.teacher_id
group by course_id order by avg desc;
Q24

25.查询各科成绩前三名的记录:(不考虑成绩并列情况)

select * from
(
select 
    student_id,
    course_id,
    num,
    (select num from score as s2    where s2.course_id=s1.course_id group by num order by s2.num desc limit 0,1) as aa,
    (select num from score as s2    where s2.course_id=s1.course_id group by num order by s2.num desc limit 2,1)as cc
from score as s1 ) as B
where B.num >B.cc
Q25

26.查询每门课程被选修的学生数;

select course_id,count(1) from score group by course_id 
Q26

27.查询出只选修了一门课程的全部学生的学号和姓名;

select student_id,student.sname,count(1) from score left join student
on student.sid = score.student_id 
group by student_id having count(1) = 1
Q27

28.查询男生、女生的人数

select gender,count(1) from student group by gender
Q28

29.查询姓“张”的学生名单

select sname from student where sname like "张%"
Q29

30.查询同名同姓学生名单,并统计同名人数

select sname,count(1) from student group by sname 
Q30

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

select course_id,avg(num)as avg from score group by course_id order by avg asc,course_id desc
Q31

32.查询平均成绩大于85的所有学生的学号、姓名和平均成绩

select  student_id,sname,avg(num) as avgnum from score left join student on student.sid = score.student_id
group by student_id having avgnum >85
Q32

33.查询课程名称为“生物”,且分数低于60的学生姓名和分数

select sname,num from score 
left join student on student.sid = score.student_id
left join course on course.cid = score.course_id 
where num<60 and cname="生物"
Q33

34.查询课程编号为003且课程成绩在80分以上的学生的学号和姓名

select student_id,sname from score
left join student on student.sid = score.student_id
where course_id = 3 and num>80
Q34

35.求选了课程的学生人数

select distinct student_id from score
Q35

36.查询选修“刘海燕老师"所授课程的学生中,成绩最高的学生姓名及其成绩

select course_id,num,sname from score
left join student on student.sid= score.student_id
left join course on score.course_id = course.cid
left join teacher on teacher.tid = course.teacher_id
where teacher.tname="刘海燕老师"
order by num desc limit 1
Q36

37. 查询各个课程及相应的选修人数

select course_id,count(1) from score group by course_id
Q37

38.查询不同课程但成绩相同的学生的学号、课程号、学生成绩;笛卡尔积

select s1.student_id from score as s1,score as s2 where s1.sid != s2.sid and s1.course_id != s2.course_id and s1.num = s2.num
Q38

39.查询每门课程成绩最好的前两名

select * from
(
select
course_id,student_id,num,1,
(select num from score as s2 where course_id = s1.course_id group by s2.num order by s2.num desc limit 0,1 ) as aa,
(select num from score as s2 where course_id = s1.course_id group by s2.num order by s2.num desc limit 2,1) as cc
from score as s1)as B
where B.num > B.cc order by B.course_id 
Q39

40.检索至少选修两门课程的学生学号

select student_id,count(1) from score group by student_id having count(1) >=2
Q40

41.查询没学过"李平老师"讲授的任一门课程的学生姓名

select sname from student where sid not in (select student_id from score left join course on course.cid = score.course_id
left join teacher on teacher.tid = course.teacher_id 
where tname = "李平老师")
Q41

42.查询两门以上不及格课程的同学的学号及其平均成绩

select student_id,count(1),avg(num) from score where num < 60 group by student_id having count(1)>2
Q42

43.检索“004”课程分数小于60,按分数降序排列的同学学号

select student_id,num from score where course_id = 4 and num < 60 order by num desc
Q43

44.删除“002”同学的“001”课程的成绩

delete from score where student_id = 2 and course_id = 1
Q44
原文地址:https://www.cnblogs.com/groundcontrol/p/13174056.html