肖sir __数据库练习__001

建表语句:

create table student ( id int(4),age int(8),sex int(4),name varchar(20), class int(4), math int(4)) DEFAULT charset=utf8;

INSERT into student VALUES(1,25,1,'zhansan',1833,90);

INSERT into student VALUES(2,25,1,'lisi',1833,67);

INSERT into student VALUES(3,28,0,'wangwu',1835,79);

INSERT into student VALUES(4,35,1,'xiaoliu',1835,96);

INSERT into student VALUES(5,27,0,'xiaoli',1833,86);

INSERT into student VALUES(6,32,1,'xiaochen',1835,48);

INSERT into student VALUES(7,22,1,'xiaowu',1834,70);

INSERT into student VALUES(8,31,0,'xiaoqi',1825,88);

INSERT into student VALUES(9,27,0,'xiaoqi',1833,74);

INSERT into student VALUES(10,27,1,'niuqi',null,80);

select * from student ;

表结构:

表:

题目:

1、查询1833班信息的2,4行的数据

结果: 信息 所有 用的*

条件:class=1833 ,limit 1,3

语句:select * from student where class=1833 LIMIT 1,3 ;

2、显示班级为空的id和姓名、和数学分数

结果: id,name, math

条件:class is null

语句:select id,name,math from student where class is null ;

3、统计每个班级人数

结果: 班级class,人数 conut(name)

条件:每个班级 gourp by

语句:

SELECT class,count(name) from student group by class;

SELECT class,count(name) from student group by class;

SELECT class,count(id) from student group by class;

截图:

4、最1833班数学成绩最大的ID年龄和姓名

结果:id,age,name

条件:class=1833,max(math)

语句:

方法1:

Select id,class,name from student where class=1833 and math=(select max(math) from student where class=1833);

方法2:弊端,有重复第一名,只显示一个

Select id,class,name from student where class=1833 order by math desc LIMIT 0,1 ;

截图:

5、求数学分最小的班级 ID年龄和姓名

结果: id ,age, name

条件:数学分最小 min(math)

语句:

方法1:select class,id,name from student where math=(select min(math) from student);

方法2:select class,id,age,name from student ORDER BY math ASC limit 0,1;

截图:

6、求1833班数学分总和

结果:sum(math)

条件:class=1833

语句:select sum(math) from student where class=1833;

截图:

7、求所有班级分数总和

结果:sum(math)

条件:

语句:select sum(math) from student

截图:

8、求年纪最大的班级并显示班级年龄和姓名分数

结果:class,age,name,math

条件: max(age) 的class

语句:select class,age,name,math from student where class=(select class from student where age=(select max(age) from student ));

截图:

9、统计sex中1和0每个的总数

结果:count(sex)

条件: group by sex

语句:

方法1:select sex,count(sex) FROM student group by sex;

方法2:select count(case when sex=1 then 1 end) as sex1,count(case when sex=0 then 0 end)as sex0 from student;

截图:

10、求出所有班级年纪平均数

11、求出1835班年纪的平均数

12、求出1833班年纪的平均数

13、将所有数据按照年纪进行降序后显示年纪姓名和班级

14、将所有数据按照年纪升序显示年纪姓名班级和数学分数

15、按照班级将进行分组

16、根据age字段进行降序排序;

17、根据math字段进行升序排序,并显示前5行所有数据;

18、把lisi的数学成绩改为69分

19、查找性别不为1的所有数据

20、只显示表中姓名,且将相同的姓名名称去重

21、统计表中行数

22、统计年纪在27岁的有多少

23、统计年纪大于25小于35的有多少

24、求数学分总和

25、求分数最小

26、求平均分

27、只显示3-8行的数据

28、查找姓名尾号为qi的所有数据

29、查询姓名开头为xiao的所有数据

30、查询中间值为ao开头的所有数据

批量注释:ctrl+/

批量取消注释ctrl+shift+/

相关推荐
RestCloud12 小时前
SQL Server到Hive:批处理ETL性能提升30%的实战经验
数据库·api
RestCloud12 小时前
为什么说零代码 ETL 是未来趋势?
数据库·api
ClouGence14 小时前
CloudCanal + Paimon + SelectDB 从 0 到 1 构建实时湖仓
数据库
DemonAvenger21 小时前
NoSQL与MySQL混合架构设计:从入门到实战的最佳实践
数据库·mysql·性能优化
AAA修煤气灶刘哥1 天前
后端人速藏!数据库PD建模避坑指南
数据库·后端·mysql
RestCloud2 天前
揭秘 CDC 技术:让数据库同步快人一步
数据库·api
得物技术2 天前
MySQL单表为何别超2000万行?揭秘B+树与16KB页的生死博弈|得物技术
数据库·后端·mysql
可涵不会debug2 天前
【IoTDB】时序数据库选型指南:工业大数据场景下的技术突围
数据库·时序数据库
ByteBlossom2 天前
MySQL 面试场景题之如何处理 BLOB 和CLOB 数据类型?
数据库·mysql·面试
麦兜*2 天前
MongoDB Atlas 云数据库实战:从零搭建全球多节点集群
java·数据库·spring boot·mongodb·spring·spring cloud