mysql之单表练习题
一.建表语句
create table grade(class int(4),chinese int(8),english int(4),math int(8),name varchar(20),age int(8),sid int(4)primary key auto_increment) DEFAULT charset=utf8;
insert into grade(class,chinese,english,math,name,age)values(1833,86,90,40,‘张三’,21),(1832,55,86,66,‘李四’,22),(1833,93,57,98,‘赵六’,23),(1832,84,90,88,‘王五’,24),(1833,93,57,22,‘李九’,25),(1832,84,98,77,‘牛七’,26),(1832,56,57,77,‘刘丽’,27),(1833,48,58,88,‘王博’,28),(1832,78,57,88,‘王三’,29),(1833,87,60,65,‘王安’,30),(1832,80,76,88,‘王平’,31),(1833,null,79,88,‘王辉’,32);
select * from grade ;
表结构:
表:
二.题目
1、查询1832班的所有信息
条件:
语句: class =1832
方法1:select * from grade where class=1832; #显示所有信息
截图:
2,查询1833班,语文成绩大于80小于90的所有信息
结果: * ,或chinese,english,math
条件:chinese>80,chinese<90
语句:
select * from grade where class=1833 and chinese<90 and chinese>80;
截图:
3,查询学生表中5到10行的所有数据
结果: *
条件:limit 4,6
语句:select * from grade limit 4,6;
4,显示1832班英语成绩为98,数学成绩为77的姓名与学号,
结果: name,sid
条件:class=1832,english=98,math=77
语句:Select name,sid from grade where class=1832 and english=98 and math=77;
截图:
5,查询出1832班所有信息并且按语文成绩排序(降序)
结果:*
条件:class=1832, order by chinese desc
语句:Select *from grade where class=1832 order by chinese desc;
截图:
6,查询1833班与1832班,语文成绩与数学成绩都小于80的姓名。
结果:name
条件:class =1833 ,class=1832 hinese<80 and math <80
语句:
方法1:select name from grade where class in (1832,1833) and chinese<80 and math<80;
方法2:select name from grade where (class=1833 and chinese<80 and math<80) or (class=1832 and chinese<80 and math<80);
方法3:select name from grade where ( class=1832 or class=1833) and chinese<80 and math<80;
截图:
7,查询出没有参加语文考试的学生姓名和班级名称。
结果:name,class
条件:chinese is null
语句:Select name,class from grade where chinese is null;
截图:
8,求出班上语文成绩不及格的学生姓名
结果:name
条件:chinese <60
语句:Select name from grade where chinese <60 or chinese is null;
截图:
9,求出每个班的数学平均成绩
结果: avg(math),class
条件:group by class
语句:select class,avg(math) from grade group by class;
截图:
10、求出每个班级语文成绩总分
结果: sum(chinese),class
条件:group by class
语句:select class,sum(chinese) from grade group by class;
截图:
11、将语文成绩不及格的学生成绩改为60分
条件:chinses<60 替换 chinses<60
语句:update grade set chinese=60 where chinese<60;
12、三科分数都大于70分的人名和年纪
结果:name,age
条件:chinese>70 and english>70 and math>70;
语句:
13、求出英语分数高于70且其它任何一科目大于60分的人和班级
结果:name,calss
条件:english>70 and (chinese>60 or math>60)
语句:
select name,class from grade where english>70 and (chinese>60 or math>60);
14、统计每个班的人数
结果:calss,count(name)
条件: group by class ,
语句:
select class,count(*) from grade group by class;
截图:
15、求每个班数学成绩大于80的人数
结果:class,count()
条件:group by class ,math>80
语句:select class,count() from grade where math>80 group by class;
截图:
16、查询姓名王开头的人的班级姓名年纪
结果:class,name,age
条件:like ‘王%’
语句:select name,age from grade where name like ‘王%’;
截图:
17、查询三科成绩都及格的班级并显示成绩姓名和年纪
结果: lass,name,age,chinese,english,math
条件: chinese>60 and english>60 and math>60
语句:Select name,age,chinese,english,math from grade where chinese>60 and english>60 and math>60;
截图:
18、查询1833班语文成绩及格人数并显示及格成绩姓名,年纪和ID号
结果:hinese,name,age,sid
条件:class=1833 and chinese>=60;
语句:select chinese,name,age,sid from grade where class=1833 and chinese>=60;
截图:
19、查询数学分数为88分的班级分数姓名和年纪
结果:class,chinese,english,math,name,age
条件:class,chinese,english,math,name,age
语句:select class,chinese,english,math,name,age from grade where math=88;
截图:
20、查询英语成绩和数学成绩都不及格的班级成绩姓名年纪
结果:class,chinese,english,math,name,age
条件:math<60 and english<60;
语句:select class,chinese,english,math,name,age from grade where math<60 and english<60;
截图:
在这里插入图片描述
21、查询语文成绩和英语成绩都低于60分的班级,并显示成绩姓名和ID号
结果:chinese,english,math,name,sid
条件:chinese<60 and english<60;
语句:
select chinese,english,math,name,sid from grade where chinese<60 and english<60;
截图:
22、统计1833班语文成绩总和
结果: sum(chinese)
条件:class=1833
语句:select sum(chinese) from grade where class=1833;
截图:
23、统计1833班语文成绩大于所有语文平均成绩的人的姓名,成绩和ID号
结果:name,chinese,english,math,sid
条件:calss=1833 , avg(chinese)
语句:Select class,chinese,english,math,sid from grade where class=1833 and chinese>(select avg(chinese) from grade);
截图:
24、查询姓名结尾为三的人,并显示班级成绩姓名
结果: class,chinese,english,math,name
条件:‘%三’;
语句:Select class,chinese,english,math,name from grade where name like ‘%三’;
截图:
25、查询英语成绩分数不为57的班级姓名年纪
结果:class,name,ag
条件: english!=57 或<>57 或 not in (57)
语句:select class,name,age from grade where english!=57;
截图:
26、给表增加3个字段(数据类型及长度自定义,建议要合理),
语句:alter table grade add (age1 int(20),age2 int(20),age3 int(20));
截图: