建表语句:
create table student33(id int(4),age int(8),sex int(4),
name varchar(20),class int(4),math int(4));
INSERT into student33 VALUES (1,25,1,'zhangsan',1833,90),
(2,25,1,'lisi',1833,67),(3,28,0,'xiaowang',1835,79),
(4,35,1,'xiaoliu',1835,96),(5,27,0,'xiaoli',1833,86),
(6,32,1,'xiaochen',1835,48),(7,22,1,'wangwu',1834,70),
(8,31,0,'xiaoqi',1825,88), (9,27,0,'xiaoqi',1833,74),
(10,27,1,'niuqi',null,74)
select *from student33
1、查询1833班信息的2,4行的数据
select * from student33 where id in(
(select id from student33 limit 1,1),(select id from student33 limit 3,1)
)
2、显示班级为空的id和姓名、和数学分数
select id,name, math from student33 where class is null
3、统计每个班级人数
select count(id),class from student33 group by class
4、取1833班数学成绩最大的ID年龄和姓名
select id,age,name from student33 where class=1833 and math=(select max(math) from student33 where class=1833)
5、求数学分最小的班级 ID年龄和姓名
select class,id,age,name from student33 where math=(select min(math) from student33)
6、求1833班数学分总和
select sum(math) from student33 where class=1833
7、求所有班级分数总和
select sum(math) from student33 where class is not null
8、求年纪最大的班级并显示班级年龄和姓名分数
select class,age,name,math from student33 where age=(select max(age) from student33)
9、统计sex 1和0个总数
select count(sex) from student33 group by sex
10、求出每个班级年纪平均数
select avg(age),class from student33 group by class
11、求出1835班年纪的平均数
select avg(age) from student33 where class=1835
12、求出1833班年纪的平均数
select avg(age) from student33 where class=1833
13、将所有数据按照年纪进行降序后显示年纪姓名和班级
select age,name,class from student33 order by age desc
14、将所有数据按照年纪升序显示年纪姓名班级和数学分数
select age,name,class,math from student33 order by age asc
15、按照班级将进行分组
select class,count(*) from student33 group by class
16、根据age字段进行降序排序;
select *from student33 order by age desc
17、根据math字段进行升序排序,并显示前5行所有数据;
select *from student33 order by math asc limit 5
18、把lisi的数学成绩改为69分
update student33 set math=69 where name="lisi"
select * from student33
19、查找性别不为1的所有数据
select * from student33 where sex!=1
20、只显示表中姓名,且将相同的姓名名称去重
select distinct name from student33
21、统计表中行数
select count(*) from student33
22、统计年纪在27岁的有多少
select count(*) from student33 where age=27
23、统计年纪大于25小于35的有多少
select count(*) from student33 where age>25 and age<35
24、求数学分总和
select sum(math) from student33
25、求分数最小
select min(math) from student33
26、求平均分
select avg(math) from student33
27、只显示3-8行的数据
select * from student33 limit 2,6
28、查找姓名尾号为qi的所有数据
select * from student33 where name like "%qi"
29、查询姓名开头为xiao的所有数据
select * from student33 where name like "xiao%"
30、查询中间值为ao开头的所有数据
select * from student33 where name like "%ao%"
