您当前的位置:首页 > 网站建设 > Mysql
| php | asp | css | H5 | javascript | Mysql | Dreamweaver | Delphi | 网站维护 | 帝国cms | React | 考试系统 | ajax | jQuery |

mysql的经典练习题及答案

51自学网 2019-09-02 09:21:17
  Mysql

创建如下各表

班级表:class

\

学生表:student

\

教师表:teacher

\

课程表:course

\

成绩表:score

\

创建各表的语句:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
class表创建语句
create table class(cid int not null auto_increment primary key, caption varchar(32) not null)engine=innodb default charset=utf8;
 
student表创建语句
create table student(
    -> sid int not null auto_increment primary key,
    -> name varchar(32) not null,
    -> gender varchar(8) not null,
    -> class_id int not null)engine=innodb default charset=utf8;
 
teacher表创建语句
create table teacher(
    -> tid int not null auto_increment primary key,
    -> tname varchar(32) not null)engine=innodb default charset=utf8;
 
course表创建语句
create table course(
    -> cid int not null auto_increment primary key,
    -> cname varchar(16) not null,
    -> teacher_id int not null)engine=innodb default charset=utf8;
 
score表创建语句
create table score(
    -> sid int not null auto_increment primary key,
    -> student_id int not null,
    -> corse_id int not null,
    -> number int not null)engine=innodb default charset=utf8;

题目及答案:

1、查询“生物”课程比“体育”课程成绩高的所有学生的学号;

?
1 select A.student_id,sw,ty from (select student_id,number as sw from score left join course on score.corse_id = course.cid where course.cname = '生物') as A left join (select student_id,number as ty from score left join course on score.corse_id = course.cid where course.cname = '体育') as B on A.student_id = B.student_id where sw > if(isnull(ty),0,ty)

2、查询平均成绩大于60分的同学的学号和平均成绩;

?
1 select student_id,avg(number) from score group by student_id having avg(number)>60;

增加显示学生名

?
1 select student_id,student.name,avg(number) from score left join student on score.student_id=student.sid group by student_id having avg(number)>60;

第二种实现方式 个人觉得这种方式好理解一些,语法结构是 先通过select student_id,avg(number) as stu_num from score group by student_id语句分组将数据取出并起临时表别名为SCORE,然后在和student表进行连表。

?
1 select SCORE.student_id,SCORE.stu_num from (select student_id,avg(number) as stu_num from score group by student_id) as SCORE left join student on SCORE.student_id=student.sid where SCORE.stu_num>60

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

?
1 select score.student_id,student.name,count(score.corse_id),sum(score.number) from score left join student on score.student_id = student.sid group by score.student_id;

4、查询姓“赵”的老师的个数;

?
1 select count(tname) from teacher where tname like "赵%";

5、查询没学过赵老师课的同学的学号、姓名;

(1)查出“赵老师”所受的课

?
1 select cid from course left join teacher on course.teacher_id=teacher.tid where teacher.tname="赵";

(2)查出选择赵老师讲课的学生
?
1 select * from score where score.corse_id in (select cid from course left join teacher on course.teacher_id=teacher.tid where teacher.tname="赵")

(3)排除选择赵老师讲课的学生

?
1 select sid,name from student where student.sid not in (select student_id from score where score.corse_id in (select cid from course left join teacher on course.teacher_id=teacher.tid where teacher.tname="赵"));

6、查询学过“1”并且也学过编号“2”课程的同学的学号、姓名;

(1)取出课程id是1和2的课程

?
1 select * from score where corse_id=1 or corse_id=2;

(2)通过student_id来进行分组根据having来过来选择两门的学生

?
1 select NEW_C.student_id,count(NEW_C.corse_id) as NUM from (select student_id,corse_id from score where corse_id=1 or corse_id=2) as NEW_C group by NEW_C.student_id having NUM=2;

(3)连表

?
1 select A.id,student.name from (select NEW_C.student_id as ID,count(NEW_C.corse_id) as NUM from (select student_id,corse_id from score where corse_id=1 or corse_id=2) as NEW_C group by NEW_C.student_id having NUM=2) as A left join student on A.ID=student.sid;

7、查询学过赵老师所教的所有课的同学的学号、姓名;

(1)连表查询课程和老师,并过滤出赵老师

?
1 select cid,cname,teacher.tname from course left join teacher on course.teacher_id=teacher.tid where teacher.tname='赵';

(2)查出选择赵平老师课程的学生id

?
1 select * from score where corse_id in (select cid from course left join teacher on course.teacher_id=teacher.tid where teacher.tname='赵') group by student_id

(3)关联学生表显示姓名

?
1 select A.student_id,student.name from(select * from score where corse_id in (select cid from course left join teacher on course.teacher_id=teacher.tid where teacher.tname='赵') group by student_id)as A left join student on A.student_id=student.sid

8、查询课程编号“2”的成绩比课程编号“1”课程低的所有同学的学号、姓名;

?
1 select student_id,name from(select A.student_id,num_1,num_2 from(select student_id, number as num_1 from score where corse_id=1)as A left join (select student_id,number as num_2 from score where corse_id=2)as B on A.student_id=B.student_id where num_1>if (isnull(num_2),0,num_2))as C left join student on C.student_id=sid

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

?
1 select student_id,name from(select *from score where score.number<60)as A left join student on A.student_id=student.sid

10、查询没有学全所有课的同学的学号、姓名;

?
1 select student.sid,student.name from (select student_id,count(corse_id)as S_NUM from score group by student_id having S_NUM<(select count(cname) from course))as A right join student on A.student_id=student.sid

11、查询至少有一门课与学号为“1”的同学所学相同的同学的学号和姓名;

(1)查询学生所选课程是否在学生id为1的学生的课程里面

?
1 select student_id from score where student_id !=1 and score.corse_id in (select corse_id from score where student_id=1) group by student_id

(2)和学生表关联取出相关的ID和姓名

?
1 select student_id,name from (select student_id from score where student_id !=1 and score.corse_id in (select corse_id from score where student_id=1) group by student_id)as A left join student on A.student_id=student.sid

12、查询和“2”号的同学学习的课程完全相同的其他同学学号和姓名;

(1)和2号同学选择的课程相同的学生id(其中的count(1)表示满足条件的记录数量)

?
1 select student_id,count(student_id) from score group by student_id having count(student_id) = (select count(1) as a from score where student_id=2)

(2)在筛选和2号同学选择课程名相同的学生id

?
1 select student_id,name from score left join student on score.student_id = student.sid where student_id in (select student_id from score group by student_id having count(student_id) = (select count(1) as a from score where student_id=2)) and corse_id in (select corse_id from score where student_id =2) group by student_id having count(corse_id)= (select count(1) as a from score where student_id=2);

13、向score表中插入一些记录,这些记录要求符合以下条件:①没有上过编号“2”课程的同学学号;②插入“2”号课程的平均成绩;

?
1 insert into score (student_id,corse_id,number) select student_id,2,(select avg(number) from score where corse_id =2) as a from score where student_id not in (select student_id from score where corse_id=2) group by student_id;

14、按平均成绩从低到高显示所有学生的“生物”、“物理”、“体育”三门的课程成绩,按如下形式显示: 学生ID,生物,物理体育,有效课程数,有效平均分;

?
1 select SC.student_id,(select number from score left join course on score.corse_id = course.cid where course.cname = "生物" and score.student_id = SC.student_id) as sw,(select number from score left join course on score.corse_id = course.cid where course.cname = "物理" and score.student_id = SC.student_id) as wl,(select number from score left join course on score.corse_id = course.cid where course.cname = "体育" and score.student_id = SC.student_id) as ty,count(SC.corse_id),avg(SC.number) from score as SC group by SC.student_id order by avg(SC.number) desc;

第二种实现方式:

?
1 select SC.student_id,(select number from score left join course on score.corse_id = course.cid where course.cname = "生物" and score.student_id = SC.student_id) as sw,(select number from score left join course on score.corse_id = course.cid where course.cname = "物理" and score.student_id = SC.student_id) as wl,(select number from score left join course on score.corse_id = course.cid where course.cname = "体育" and score.student_id = SC.student_id) as ty,count(SC.corse_id),avg(SC.number) from score as SC group by SC.student_id order by avg(SC.number) desc;

15、查询各科成绩最高和最低的分:以如下形式显示:课程ID,最高分,最低分;思路:通过课程id来进行分组,这个时候会显示四行,然后用聚合函数max,min来找出最大值和最小值。

?
1 select corse_id,max(number) as max_num,min(number) as min_num from score group by corse_id;

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

新知识点:case when then相当于if判断

?
1 select corse_id,avg(number),sum(case when score.number>=60 then 1 else 0 end)/count(1)*100 as jgl from score group by corse_id;

17、课程平均分从高到低显示(现实任课老师);

?
1 select A.avg_num,course.cname,teacher.tname from (select avg(number) as avg_num,corse_id from score group by corse_id) as A left join course on course.cid = A.corse_id left join teacher on teacher.tid = course.teacher_id order by A.avg_num desc;

18、查询每门课程被选修的学生数;

?
1 select corse_id,count(score.student_id) from score group by score.corse_id;

19、查询出只选修了两门课程的全部学生的学号和姓名;

?
1 select A.c_id,student.name from (select count(corse_id) as c_id,student_id from score group by student_id) as A left join student on student.sid=A.student_id having A.c_id =2;

20、查询男生、女生的人数;

?
1 select * from (select count(1) as M from student where gender="男") as A,(select count(1) as W from student where gender="女") as B;

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

?
1 select corse_id,avg(number) from score group by corse_id order by avg(number) asc,corse_id desc;

22、查询平均成绩大于75的所有学生的学号、姓名和平均成绩;

?
1 select A.student_id,student.name,A.avg_num from (select avg(number) as avg_num,student_id from score group by student_id having avg_num > 75) as A left join student on student.sid = A.student_id;

23、查询课程名称为“体育”,且分数低于60的学生姓名和分数;

?
1 select A.student_id,student.sname,A.number from (select student_id,number from score where corse_id=(select cid from course where cname="生物") having number < 60) as A left join student on student.sid = A.student_id;

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

?
1 select A.student_id,student.name from (select student_id,number from score where corse_id=3 group by student_id having number > 80) as A left join student on student.sid=A.student_id;

第二种方式:

?
1 select * from score where score.corse_id = 3 and score.number > 80

25、求选了课程的学生人数

?
1 select count(A.student_id) from (select student_id from score group by student_id) as A;

26、查询选修赵老师所授课程的学生中,成绩最高的学生姓名及其成绩;

?
1 select score.corse_id,student.name,score.number from score left join student on score.student_id=student.sid where score.corse_id in (select course.cid from course left join teacher on course.teacher_id=teacher.tid where tname = "赵") order by score.number desc

27、查询各个课程及相应的选修人数;

?
1 select A.c_num,course.cname from (select count(student_id) as c_num,corse_id from score where corse_id in (select cid from course) group by corse_id) as A left join course on course.cid =A.corse_id ;

第二种方式:

?
1 select course.cname,count(1) from score left join course on score.corse_id = course.cid group by corse_id;

28、查询不同课程但成绩相同的学生的学号、课程号、学生成绩;

?
1 select DISTINCT s1.corse_id,s2.corse_id,s1.number,s2.number from score as s1, score as s2 where s1.number = s2.number and s1.corse_id != s2.corse_id;

29、检索至少选修两门课程的学生学号;

?
1 select student_id,count(corse_id) from score group by student_id having count(corse_id)>1

30、查询全部学生都选修的课程的课程号和课程名;

?
1 select corse_id,cname,count(student_id)  from score left join course on score.corse_id=course.cid group by corse_id having count(student_id) = (select count(sid) from student)

31、查询没学过孙老师讲授的任一门课程的学生姓名;

?
1 select name from(select A.student_id as n_s_id from (select student_id,corse_id from score) as A left join course on course.cid=A.corse_id where course.teacher_id not in (select tid from teacher where tname="孙") group by A.student_id ) as B left join student on student.sid=B.n_s_id;

第二种方法:

?
1 select student_id,student.sname from score left join student on score.student_id = student.sid where score.corse_id not in (select cid from course left join teacher on course.teacher_id = teacher.tid where tname = "李平老师") group by student_id;

32、查询一门以上不及格课程的同学的学号及其平均成绩;

?
1 select student_id,avg(number) from score where student_id in (select student_id from score where number < 60 group by student_id having count(corse_id) >=1);

33、查询课程号为2且分数小于60的记录,按分数降序排列的同学学号;

?
1 select A.number,student.name,student.sid from (select score.number,score.student_id from score where score.corse_id = 2 and score.number<60) as A left join student on student.sid = A.student_id order by A.number desc;

34、删除学习孙老师课的score表记录;

?
1 delete from score where corse_id in(select cid from course left join teacher on course.teacher_id=teacher.tid where teacher.tname='孙')

35、删除“1”同学的“2”课程的成绩;

?
1 delete from score where corse_id = 1 and student_id = 2

 
下载地址:
电脑如何远程连接mysql
php读取文本数据插入mysql对应的表列中
51自学网,即我要自学网,自学EXCEL、自学PS、自学CAD、自学C语言、自学css3实例,是一个通过网络自主学习工作技能的自学平台,网友喜欢的软件自学网站。
京ICP备13026421号-1