MySQL 数据库练习题

所有题目:

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

select Sname,Ssex,Class from student

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

 select distinct depart from teacher

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

select * from student

4、 查询Score表中成绩在60到80之间的所有记录。  (在...之间:between...and...)

select * from score where degree between 60 and 80

 5、 查询Score表中成绩为85,86或88的记录。  (在几个值范围内:in())

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

6、 查询Student表中“95031”班或性别为“女”的同学记录。(逻辑运算:or)

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

7、 以Class降序查询Student表的所有记录。 (降序:order by ... desc)

select * from student order by class desc

 8、 以Cno升序、Degree降序查询Score表的所有记录        --需要复习

select * from Score order by cno asc,degree desc

易错点:以多个字段排序,order by 只要出现一个;如果是降序,asc可省略

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

 select class,count(*) from student where class='95031'

10、查询Score表中的最高分的学生学号和课程号。(子查询或者排序)

子查询

select sno,cno from Score where degree=(select max(degree) from Score)

排序

select sno,cno from Score order by degree desc limit 0,1

可用法:order by...desc 后 加 limit;limit 也可直接加在简单查询后

11、查询每门课的平均成绩。        --需要复习

select cno,avg(degree) from Score group by cno

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

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

拆分 :至少有5名学生选修的并以3开头的课程:以3开头,至少有5名    

13、查询分数大于70,小于90的Sno列。

select sno from Score where degree>70 or degree<90

14、查询所有学生的Sname、Cno和Degree列。    --需要复习    

select sname,cno,degree from student inner join score where student.sno = score.sno;

分析:两个表中有相同字段的是sno,连接查询时,以sno相同为条件。正常查询结构有12条,有三个学生,每个考4门

错误示范:

        select sname,Cno,Degree from student,score;

       查出的值多种组合,有72条,学生表中有6个学生,每个12个值

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

 select Sno,Cname,Degree from score INNER JOIN course on course.cno=score.cno

分析:两个表中有相同字段的是cno,连接查询时,以cno相同为条件

16、查询所有学生的Sname、Cname和Degree列。(内连接两个表)   --需要多练

select Sname,Cname,Degree from student inner join course,score where student.sno=score.sno and course.cno=score.cno;

此题:此处inner join 用where可以,直接改成用on报错,为什么此处不能用on?

 (1) 待解答:同时连接多个表的问题?

 (2)错误如:select sname,cname,degree from student inner join score,course on student.sno = score.sno and course.cno = score.cno;

用on的另一写法可以:

select sname,cname,degree from score inner join student on student.sno=score.sno inner join course on  course.cno=score.cno;

疑问:两个条件 inner join后加where或on都可以?对比第14-16题

 解答: inner join 后面加on和where的差别:

   1.当使用inner join时,on和where的用法相同,但是如上16题会报错,待解答

   2.当使用left join时,

 (1)on:无论on的条件是否满足,都会返回左表的所有记录,对于满足的条件的记录

 (2)where:在临时表生成好后,再对临时表进行过滤的条件

17、查询“95033”班学生的平均分。(选)    --需要复习   此时查出来是空,再看?

法一:子查询

select avg(degree) from score where sno in (select sno from student where class='95033')

法二:连接查询      

此处:inner join 用on,加where都可以,这种可同时添加其中一表的其他条件

select avg(degree) from student inner join score where score.sno=student.sno and  class='95033'

 18、假设使用如下命令建立了一个grade表,查询所有同学的Sno、Cno和rank列。-----需要再试

create table grade(low int(3),high int(3),rank varchar(20));

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;
———————————————

select sno,cno,rank from score inner join grade on degree < high and degree >low

select sno,cno,rank from score inner join grade where degree < high and degree >low

分析:此处用on、where都可以

连接查询:两个表的不同字段可不加表名

连接查询注意:不是两个表有相同字段才能使用连接查询;但是要查询两个表下的列,一定要同连接查询 才查出来

子查询:要查询两个表下的列,不适合子查询

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

select* from score where degree>(select degree from score where sno='109' and cno='3-105')

分析:高于某一个成绩,子查询-标量子查询

20、查询和学号为108的同学同年出生的所有学生的Sno、Sname和Sbirthday列。(选)   --需要再试 

select Sno,Sname,Sbirthday from student where Sbirthday=(select Sbirthday from student where sno='108')

 21、查询“张旭“教师任课的学生成绩。(选)    --需要再试 

select sname,degree from student,course,teacher,score where score.cno=course.cno and score.sno=student.sno and teacher.tno = course.tno and teacher.tname = '张旭'

分析:简单查询,只有一个条件,没想象中复杂

成绩表score关联了课程表course中的cno、student表中的sno

课程表course中tno关联了teacher表中的tno

 22、查询95033班和95031班全体学生的记录。(in)

 select * from student where class in('95033','95031')

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

法一:去除重复行

select distinct cno from score where degree>='85'

-法二:分组之后在过滤,判断最大值有大于85   --需要再试

select cno from score group by cno having max(degree) >= 85;

24、查询出“计算机系“教师所教课程的成绩表。(选)

select degree from teacher,score,course,student where teacher.tno = course.tno and course.cno = score.cno and score.sno = student.sno and teacher.depart = '计算机系';

分析:同21简单查询

25、查询成绩比该课程平均成绩低的同学的成绩表。(选)  --需要再试

select s.sno,s.cno,s.degree from score as s,

(select cno,avg(degree) as avgd from score group by cno ) as avgs
 where s.cno = avgs.cno and s.degree < avgs.avgd;

分析(通过看答案分析):

 算出每个课程(用group by)的平均值,重命名方便后面用,

 将子查询结果:cno分类及平均值作为一个表

26、查询所有任课教师的Tname和Depart.(选)

select Tname,depart from teacher

 27、查询至少有2名男生的班号。(group by having)

select class,count(*) from student group by class having count(*)>=2

分析:聚合函数 count()等,可以加在select后,也可加在having后

28、查询Student表中不姓“王”的同学记录。(not like)

select * from student where sname not like '王%'

29、查询Student表中每个学生的姓名和年龄。year(日期):取日期中的年份

select sname,2022-year(sbirthday) as age from student

 分析:运算结果2022-year(sbirthday) 可以直接作为一列

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

select max(Sbirthday),min(Sbirthday) from student

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

select * from student order by Class desc,Sbirthday asc;

select * from student order by class desc,sbirthday;

分析:以多个字段排序,order by 只要出现一个;如果是降序,asc可省略

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

select t.tname,c.cname from teacher as t inner join course as c on t.tno=c.tno and tsex = '男';

33、查询每一门科目最高分同学的Sno、Cno和Degree列。(选)--需要再试

select sno,cno,max(Degree) from score group by cno

需要数据:

1.新建数据库

设有一个数据库,包括四个表:学生表(student)、课程表(course)、成绩表(score)以及教师信息表(teacher)。用SQL语句创建四个表并完成相关题目。

创建学生表

create table student(

sno varchar(20) not null primary key,

sname varchar(20) not null,

ssex varchar(20) not null,

sbirthday datetime,

class varchar(20)

) engine = innodb default charset = utf8;

创建教师表

create table teacher(

tno varchar(20) not null primary key,

tname varchar(20) not null,

tsex varchar(20) not null,

tbirthday datetime,

prof varchar(20),

depart varchar(20) not null

)engine = innodb default charset = utf8;

创建课程表

create table course(

cno varchar(20) not null primary key,

cname varchar(20) not null,

tno varchar(20) not null,

foreign key(tno) references teacher(tno)

) engine = innodb default charset = utf8;

创建成绩表

create table score(

sno varchar(20) not null,

foreign key(sno) references student(sno),

cno varchar(20) not null,

foreign key(cno) references course(cno)

degree decimal,

) engine = innodb default charset = utf8;

2.插入数据库

插入学生表数据

INSERT INTO STUDENT (SNO,SNAME,SSEX,SBIRTHDAY,CLASS) VALUES (108 ,'曾华' ,'男' ,'1977-09-01',95033);

INSERT INTO STUDENT (SNO,SNAME,SSEX,SBIRTHDAY,CLASS) VALUES (105 ,'匡明' ,'男' ,'1975-10-02',95031);

INSERT INTO STUDENT (SNO,SNAME,SSEX,SBIRTHDAY,CLASS) VALUES (107 ,'王丽' ,'女' ,'1976-01-23',95033);

INSERT INTO STUDENT (SNO,SNAME,SSEX,SBIRTHDAY,CLASS) VALUES (101 ,'李军' ,'男' ,'1976-02-20',95033);

INSERT INTO STUDENT (SNO,SNAME,SSEX,SBIRTHDAY,CLASS) VALUES (109 ,'王芳' ,'女' ,'1975-02-10',95031);

INSERT INTO STUDENT (SNO,SNAME,SSEX,SBIRTHDAY,CLASS) VALUES (103 ,'陆君' ,'男' ,'1974-06-03',95031);

插入教师表数据

INSERT INTO TEACHER(TNO,TNAME,TSEX,TBIRTHDAY,PROF,DEPART) VALUES (804,'李诚','男','1958-12-02','副教授','计算机系');

INSERT INTO TEACHER(TNO,TNAME,TSEX,TBIRTHDAY,PROF,DEPART) VALUES (856,'张旭','男','1969-03-12','讲师','电子工程系');

INSERT INTO TEACHER(TNO,TNAME,TSEX,TBIRTHDAY,PROF,DEPART) VALUES (825,'王萍','女','1972-05-05','助教','计算机系');

INSERT INTO TEACHER(TNO,TNAME,TSEX,TBIRTHDAY,PROF,DEPART) VALUES (831,'刘冰','女','1977-08-14','助教','电子工程系');

插入课程表数据

INSERT INTO COURSE(CNO,CNAME,TNO)VALUES ('3-105' ,'计算机导论',825);

INSERT INTO COURSE(CNO,CNAME,TNO)VALUES ('3-245' ,'操作系统' ,804);

INSERT INTO COURSE(CNO,CNAME,TNO)VALUES ('6-166' ,'数据电路' ,856);

INSERT INTO COURSE(CNO,CNAME,TNO)VALUES ('9-888' ,'高等数学' ,831);

插入成绩表数据

INSERT INTO SCORE(SNO,CNO,DEGREE)VALUES (103,'3-245',86);

INSERT INTO SCORE(SNO,CNO,DEGREE)VALUES (105,'3-245',75);

INSERT INTO SCORE(SNO,CNO,DEGREE)VALUES (109,'3-245',68);

INSERT INTO SCORE(SNO,CNO,DEGREE)VALUES (103,'3-105',92);

INSERT INTO SCORE(SNO,CNO,DEGREE)VALUES (105,'3-105',88);

INSERT INTO SCORE(SNO,CNO,DEGREE)VALUES (109,'3-105',76);

INSERT INTO SCORE(SNO,CNO,DEGREE)VALUES (101,'3-105',64);

INSERT INTO SCORE(SNO,CNO,DEGREE)VALUES (107,'3-105',91);

INSERT INTO SCORE(SNO,CNO,DEGREE)VALUES (108,'3-105',78);

INSERT INTO SCORE(SNO,CNO,DEGREE)VALUES (101,'6-166',85);

INSERT INTO SCORE(SNO,CNO,DEGREE)VALUES (107,'9-888',79);

INSERT INTO SCORE(SNO,CNO,DEGREE)VALUES (108,'6-166',81);
————————————————
版权声明:本文为CSDN博主「欧阳娟频」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/weixin_32304839/article/details/113149030

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值