肖SIR__数据库之单表练习__12.0

 

 建表语句:

一个学生表

分别记录姓名,年龄,性别,班级,语文,数学,英语字段

create table student2(

id int primary key ,

name char(20),

sex char(10),

age int(3),

mobile char(20),

class char(10),

english int(10),

chinese int(10),

math int(10))engine=innodb default charset=utf8;

insert into student2 values

(1,'小红','女',23,'13813828824','1719',77,88,98),

(2,'小明','男',23,'13713713711','1720',56,66,55),

(3,'小李','男',23,'15915913911','1719',78,64,87),

(4,'小张','男',23,'15915913912','1720',77,76,77),

(5,'小白','女',24,'15915913913','1719',90,89,98),

(6,'小陈','女',19,'15915913914','1719',84,100,81),

(7,'小钱','女',20,'15915913915',null,45,99,93);

==========================================

 

题目

题目1

查询1719班学生的成绩信息

结果:english ,chinese ,math

条件:class=1719

方法1:SELECT  english,chinese,math  from  student2 where class="1719" ;(精准)

 方法2: select name,english,chinese,math from student where class=1719;

 

 

题目2

查询1719班学生语文成绩大于80小于90的学生信息

 结果: 学生信息     指的是所有信息   * 表示所有

条件:Chinese>80  ,chinese <90     ,class=1719

方法1:SELECT  * FROM student2 WHERE chinese>80 and chinese<90 and class="1719";

方法2:select  *  from  student where class=1719 and chinese between 81 and 89;

 

题目3

查询学生表中第5-7行的数据信息

结果:数据信息 ,   所有信息  * 表示

条件:limit   567 三行     5的索引是4(索引从0开始)  ,步长3

方法1:SELECT * from student2 limit 4,3;

 错误:

 

1、 select * from  student2 where id BETWEEN 5 and 7 ;(id不能确定5-7行)

2、SELECT * from student2 limit 4,7; (7表示7行,实际要3行)

3、select * from student2 where id in(5,6,7);  只能取到id5,6,7

 

题目4

显示1719班英语成绩为90,数学成绩为98的namemobile信息

 结果: name ,moblie

条件: class=1719  ,english=90, math=98

方法1:select name,mobile FROM student2 WHERE class="1719" and english=90 AND math=98;(不建议in)

 

题目5

显示1719班学生信息并且以语文成绩降序排序

结果:学生信息      所有*

条件 :class=1719            order  by      chinese  desc  

方法1:select* from student2 where class="1719" order by chinese desc

方法2:SELECT *FROM student2 WHERE class IN (1719) ORDER BY chinese DESC;

 

 

题目6

查询1719与1720班,语文成绩与数学成绩都小于80的namemobile信息

 结果:name ,mobile

条件:class=1719,class=1720 , chinese<80, math<80;

方法1:SELECT name,mobile from student2 WHERE class in (1719,1720)and chinese<80 and math < 80;

方法2:select name,mobile from student2 where(class=1719 or class="1720") and chinese<80 and math<80;

 方法3:SELECT NAME,mobile from student2 WHERE (class=1719 or class=1720) and (chinese<80 and math<80);

 方法4:select name, mobile from student2 where( chinese<80 and math<80 and class="1719") or (class=1720 and chinese<80 and math<80);

 

题目7

查询英语平均分大于80分的班级,英语平均分

结果:class , avg(english)

条件   : group  by   class      ,  avg(english)>80

方法1:

Select class,avg(english) from student2 group by class having avg(english)>80;

方法2:  

SELECT  class,avg(english) s from student2  GROUP BY  class  having s>80 ;

 

题目8

按班级查出数学最高分

 结果:显示 class   max(math)

条件:group  by   class

 方法1:select class,max(math) from student2 GROUP BY class;

 

题目9

查询出每班数学最低分

 结果:class   min(math)

条件:group  by   class

方法1:select  class,min(math) from  student2 GROUP BY  class ;

 

题目10

查询每班数学总分

 

 结果:class   sum(math)

条件:group  by   class

方法1:select  class,sum(math) from  student2 GROUP BY  class 

 方法2:select sum(math),class from student2 where class  is not null group by class ;

 

题目11

查询每班数学平均分

 

 结果:class   avg(math)

条件:group  by   class

方法1:select  class,avg(math)   from  student2 GROUP BY  class 

 

题目12

查询出每班学生总数

 注意:统计人数可以(*,字段名)

 结果:class   count(math)

条件:group  by   class

方法1:select  class,count(id)   from  student2 GROUP BY  class  ;

方法2:select count(*),class from student2 where class  is not null group by class ;

 方法3:select class,count(name) from student2 group by class ;

题目13

在表中插入一条小谢的成绩数据

 结果:显示一条数据

条件 :  name=小谢

方法1:INSERT INTO  student2  VALUES(8,'小谢','女',20,'15915913615',1719,50,80,92);

方法2:INSERT INTO student2 (id ,name) VALUES (9,'小谢') ;

 

题目14

把英语分数小于60的同学分数改为60分

 结果:显示所有信息

条件: update     english =60

方法1:update student2  set english=60 where english<60;

posted @ 2022-03-04 09:06  xiaolehua  阅读(495)  评论(0编辑  收藏  举报