MySQL课后习题

本文列举了一系列关于MySQL数据库的查询操作,包括查询特定列、筛选特定条件、排序、分组统计、多表连接等复杂SQL语句,涵盖了学生、教师、课程、成绩等多个场景。通过这些实例,读者可以深入理解如何在实际中运用SQL进行数据检索。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

创建四张表 分别存储 学生信息 课程信息 分数 讲师信息表 存储相应数据 

student学生信息表

 course课程信息表

 scores分数表

 

 teacher讲师表

 

目录

1、 查询Student表中的所有记录的Sname、Ssex和Class列。

​2、 查询教师所有的单位即不重复的Depart列。

​3、 查询Student表的所有记录。

​4、 查询Score表中成绩在60到80之间的所有记录。

​5、 查询Score表中成绩为85,86或88的记录。

​6、 查询Student表中“95031”班或性别为“女”的同学记录。

​7、 以Class降序查询Student表的所有记录。 

​8、 以Cno升序、Degree降序查询Score表的所有记录。

9、 查询“95031”班的学生人数。

​10、查询Score表中的最高分的学生学号和课程号。

​11、查询‘3-105’号课程的平均分。

12、查询Score表中至少有5名学生选修的并以3开头的课程的平均分数。

13、查询最低分大于70,最高分小于90的Sno列。

​14、查询所有学生的Sname、Cno和Degree列。

​15、查询所有学生的Sno、Cname和Degree列。

​16、查询所有学生的Sname、Cname和Degree列。

17、查询“95033”班所选课程的平均分。

 18、假设使用如下命令建立了一个grade表:create table grade(low int(3),upp int(3),rank char(1));insert into grade values(90,100,'A');insert into grade values(80,89,'B');insert into grade values(70,79,'C');insert into grade values(60,69,'D');insert into grade values(0,59,'E');commit;现查询所有同学的Sno、Cno和rank列。

​19、查询选修“3-105”课程的成绩高于“109”号同学成绩的所有同学的记录。

​20、查询score中选学一门以上课程的同学中分数为非最高分成绩的记录。

21、查询成绩高于学号为“109”、课程号为“3-105”的成绩的所有记录。

​22、查询和学号为108的同学同年出生的所有学生的Sno、Sname和Sbirthday列。

​23、查询“张旭“教师任课的学生成绩。

​24、查询选修某课程的同学人数多于5人的教师姓名。

​26、查询存在有85分以上成绩的课程Cno.

​27、查询出“计算机系“教师所教课程的成绩表。

 ​ 28、查询“计算机系”与“电子工程系“不同职称的教师的Tname和Prof。

​29、查询选修编号为“3-105“课程且成绩至少高于选修编号为“3-245”的同学的Cno、Sno和Degree,并按Degree从高到低次序排序。

​30、查询选修编号为“3-105”且成绩高于选修编号为“3-245”课程的同学的Cno、Sno和Degree.

31、查询所有教师和同学的name、sex和birthday.

32、查询所有“女”教师和“女”同学的name、sex和birthday.

​33、查询成绩比该课程平均成绩低的同学的成绩表。

​34、查询所有任课教师的Tname和Depart.

​35 查询所有未讲课的教师的Tname和Depart.

​ 36、查询至少有2名男生的班号。

​37、查询Student表中不姓“王”的同学记录。

​38、查询Student表中每个学生的姓名和年龄。

​39、查询Student表中最大和最小的Sbirthday日期值。

​40、以班号和年龄从大到小的顺序查询Student表中的全部记录。

​41、查询“男”教师及其所上的课程。

​42、查询最高分同学的Sno、Cno和Degree列。

select sno,cno,Degree from score order by Degree desc limit 0,1;select sno,cno,Degree from score where Degree=(select max(Degree)from score);​43、查询和“李军”同性别的所有同学的Sname.

select Sname from student where Ssex=(select Ssex from student where Sname='李军');​44、查询和“李军”同性别并同班的同学Sname.

​45、查询所有选修“计算机导论”课程的“男”同学的成绩表


1、 查询Student表中的所有记录的Sname、Ssex和Class列。

select sname.ssex.class from student;

 查询结果:

 


2、 查询教师所有的单位即不重复的Depart列。

select distinct Depart from Teacher;


3、 查询Student表的所有记录。

select *from student;


4、 查询Score表中成绩在60到80之间的所有记录。

select * from Score where degree between 60 and 80;


5、 查询Score表中成绩为85,86或88的记录。

select *from score where degree in(85,86,88);


6、 查询Student表中“95031”班或性别为“女”的同学记录。

select *from student where class=95031 or Ssex='女';


7、 以Class降序查询Student表的所有记录。 

select *from student order by class desc;


8、 以Cno升序、Degree降序查询Score表的所有记录。

select *from score order by cno,degree desc;

103    3-105    92.0
107    3-105    91.0
105    3-105    88.0
108    3-105    78.0
109    3-105    76.0
101    3-105    64.0
103    3-245    86.0
105    3-245    75.0
109    3-245    68.0
107    6-106    79.0
101    6-166    85.0
108    6-166    81.0


9、 查询“95031”班的学生人数。

select count(class) from student where class=95031;


10、查询Score表中的最高分的学生学号和课程号。

select sno,cno from score where degree=(select max(degree)from score);


11、查询‘3-105’号课程的平均分。

select avg(degree)from score where cno='3-105' group by cno; 


12、查询Score表中至少有5名学生选修的并以3开头的课程的平均分数。

select avg(degree)  from score
where cno = (select cno from score group by cno having count(cno)>=5 ) and cno like '3%';

此处有踩坑,踩坑原因 :字符编码问题,神奇的空格

运行结果



13、查询最低分大于70,最高分小于90的Sno列。

思路:这里需要注意sql语句的执行顺序,分组函数不能再where的子句中,所以不可以直接用where,需要用having分组之后筛选。

select sno from score group by sno having min(degree)>70 ang max(degree)<90;


14、查询所有学生的Sname、Cno和Degree列。

分析:sname在student表,cno与degree在score表。找到两表关联,用sno字段连接两表;

select stu.sname,s.cno,s.degree from student stu,score s where stu.sno=s.sno;


15、查询所有学生的Sno、Cname和Degree列。

分析:cname在course表,sno与degree在score表,找到两表关联,用cno字段连接两表;

select sno,cname,degree from course c,score s where c.cno=s.cno;


16、查询所有学生的Sname、Cname和Degree列。

分析sname在student表,cname在course表,degree在score表

三表连接student,course,score

先用student连接course,再连接score

select stu.sname,c.cname,s.degree,from student stu,course c,score s where stu.sno=s.sno and c.cno=s.cno;


17、查询“95033”班所选课程的平均分。

分析:student中的class为95033

 select avg(degree)from student stu,score s where stu.sno=s.sno and stu.class=95033;

 

 
18、假设使用如下命令建立了一个grade表:
create table grade(low int(3),upp int(3),rank char(1));
insert into grade values(90,100,'A');
insert into grade values(80,89,'B');
insert into grade values(70,79,'C');
insert into grade values(60,69,'D');
insert into grade values(0,59,'E');
commit;
现查询所有同学的Sno、Cno和rank列。

select s.sno,s.cno,g.rank from score s,grade g where s.degree between g.low and g.upp;


19、查询选修“3-105”课程的成绩高于“109”号同学成绩的所有同学的记录。

select*from student stu,score s where s.cno='3-105' and stu.sno=s.sno and s.degree>(select degree from score s where s.cno='3-105' and s.sno='109');


20、查询score中选学一门以上课程的同学中分数为非最高分成绩的记录。

分析:两个条件

一个是非最高成绩:degree<(select max(degree) from score s2 where s.cno=s2.cno)

使用了自连接。

一个是选修课程大于1:sno in(select sno from score group by sno having count(*)>1)

使用了分组函数。

 select s.degree from score s where degree<(select max(degree) from score s2 where s.cno=s2.cno) and sno in(select sno from score group by sno having count(*)>1);

 


21、查询成绩高于学号为“109”、课程号为“3-105”的成绩的所有记录。

分析:查询所有记录,自然是student表与score表,先将表等值连接,再通过条件查询出大于题目所出学员的成绩人员信息。

select*from student,score where student.sno=score.sno and degree>(select degree from score s where  s.sno='109' and s.cno='3-105');


22、查询和学号为108的同学同年出生的所有学生的Sno、Sname和Sbirthday列。

思路:使用了date类型的year关键字,找出与该学员同年出生的学员信息

select sno,sname,sbirthday from student stu where year(stu.Sbirthday)=(select year(Sbirthday)from student s where s.sno='107'); 


23、查询“张旭“教师任课的学生成绩。

 思路,连接表score,teacher还需要一个create表create通过cno连接score,通过tno连接teacher

找出张旭任课的学生,在找出他们的成绩。

select degree from score s,course c,teacher t where s.cno=c.cno and c.tno =t.tno and t.Tname='张旭';

扩展:如果还需要显示学员的姓名

select sname,degree from  student stu,score s,course c,teacher t where stu.Sno=s.Sno and s.cno=c.cno and c.tno =t.tno and t.Tname='张旭';


24、查询选修某课程的同学人数多于5人的教师姓名。

思路:连接teacher表与course表,找出学员cno分组大于5的学员对应的tname

select t.tname from teacher t,course c where t.tno=c.tno and c.cno=(select cno from score group by cno having count(*)>5);
25、查询95033班和95031班全体学生的记录。

思路三表连接与条件查询

select * from student stu,course c,score s where stu.sno=s.sno and s.cno=c.cno and stu.Class in('95033','95031');


26、查询存在有85分以上成绩的课程Cno.

select cno from score where degree>85;


27、查询出“计算机系“教师所教课程的成绩表。

思路:三表连接

 select s.sno,s.cno,s.degree from score s,course c,teacher t where s.cno=c.cno and c.Tno=t.Tno and t.depart='计算机系';

  
28、查询“计算机系”与“电子工程系“不同职称的教师的Tname和Prof。

此处踩坑,题意不好理解,简单说就是去掉相同职称的教师,所以使用not in

select Tname,Prof from teacher where Prof not in(select Prof from teacher where Depart='计算机系' or Depart='电子工程系'group by Prof having count(*)>1);


29、查询选修编号为“3-105“课程且成绩至少高于选修编号为“3-245”的同学的Cno、Sno和Degree,并按Degree从高到低次序排序。

思路:先条件,在order by,思路清晰,很容易想到,难点在至少高于与高于的区别。在30题中说明。

select cno,sno,degree from score where cno ='3-105' and Degree>any(select Degree from score where cno='3-245') order by Degree desc;


30、查询选修编号为“3-105”且成绩高于选修编号为“3-245”课程的同学的Cno、Sno和Degree.

select cno,sno,degree from score where cno ='3-105' and Degree>all(select Degree from score where cno='3-245') ;

高于就是必须高于3-245课程所有学员的成绩,必须高于最高分,所以用all

至少高于,则也可以高于最低分,所以用any


31、查询所有教师和同学的name、sex和birthday.

select sname as '学生',Ssex,Sbirthday from student
union
select Tname as '老师',Tsex,Tbirthday from teacher;


32、查询所有“女”教师和“女”同学的name、sex和birthday.

select sname as '学生',Ssex,Sbirthday from student where Ssex='女'
union
select Tname as '老师',Tsex,Tbirthday from teacher where Tsex='女';


33、查询成绩比该课程平均成绩低的同学的成绩表。

select * from score s where Degree<(select avg(Degree) from score s2 where s.Cno=s2.Cno );


34、查询所有任课教师的Tname和Depart.

方法一:表连接查询

select Tname,Depart from teacher t,course c where t.Tno=c.Tno; 

方法二:子查询

select Tname,Depart from Teacher where Tno in (select Tno from Course);


35 查询所有未讲课的教师的Tname和Depart.

select Tname,Depart from teacher where tno not in (select tno from course);

 
36、查询至少有2名男生的班号。

select class from student where Ssex='男' group by Class having count(*)>1;


37、查询Student表中不姓“王”的同学记录。

select * from Student where Sname not like '王%';


38、查询Student表中每个学生的姓名和年龄。

select Sname,year(now())-year(Sbirthday) age from Student;


39、查询Student表中最大和最小的Sbirthday日期值。

select max(Sbirthday),min(Sbirthday) from Student;


40、以班号和年龄从大到小的顺序查询Student表中的全部记录。

select * from Student order by Class desc,Sbirthday;


41、查询“男”教师及其所上的课程。

select Cno,Cname,Tname from Course,Teacher where Course.Tno=Teacher.Tno and Tsex='男';

select Cno,Cname,Tname from Course inner join Teacher on Course.Tno=Teacher.Tno where Tsex='男';


42、查询最高分同学的Sno、Cno和Degree列。

select sno,cno,Degree from score order by Degree desc limit 0,1;
select sno,cno,Degree from score where Degree=(select max(Degree)from score);

43、查询和“李军”同性别的所有同学的Sname.

select Sname from student where Ssex=(select Ssex from student where Sname='李军');

44、查询和“李军”同性别并同班的同学Sname.

select Sname from Student where Ssex=(select Ssex from Student where Sname='李军')and Class=(select Class from Student where Sname='李军');


45、查询所有选修“计算机导论”课程的“男”同学的成绩表

 select * from score where cno=(select Cno from course where Cname='计算机导论')and sno in (select sno from student where Ssex='男');

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值