创建一个学生表,插入数据
1 | create table student( |
查询表中所有学生的信息
1 | select * from student; |
查询表中所有学生的姓名和对应的英语成绩。
1 | select name,english from student; |
过滤表中重复数据
1 | select english from student; |
在所有学生英语分数上加10分特长分。
1 | select name,english+10 from student; |
统计每个学生的总分。
1 | select english+chinese+math from student; |
使用别名表示学生分数
1 | select name,english+chinese+math as 总分 from student; |
查询姓名为何东的学生成绩
1 | select * from student where name='何东'; |
查询英语成绩大于90分的同学
1 | select * from student where english>90; |
查询总分大于250分的所有同学
1 | select * from student where english+chinese+math>250; |
查询英语分数在 85-95之间的同学。
1 | select * from student where english>=85 and english<=95; |
查询数学分数为84,90,91的同学。
1 | select * from student where math=84 or math=90 or math=91; |
查询所有姓何的学生成绩。
1 | select * from student where name like '何%'; |
查询数学分>85,语文分>90的同学。
1 | select * from student where math>85 and chinese>90; |
对数学成绩排序后输出。
1 | select * from student order by math; |
对总分排序后输出,然后再按从高到低的顺序输出
1 | select * from student order by math+chinese+english desc; |
对姓何的学生成绩排序输出
1 | select * from student where name like '何%' order by math+chinese+english desc; |
统计一个班级共有多少学生?
1 | select count(*) from student; |
统计数学成绩大于90的学生有多少个?
1 | select count(*) from student where math>90; |
统计总分大于250的人数有多少?
1 | select count(*) from student where math+chinese+english>250; |
统计一个班级数学总成绩?
1 | select sum(math) from student; |
统计一个班级语文、英语、数学各科的总成绩
1 | select sum(math), sum(chinese), sum(english) from student; |
统计一个班级语文、英语、数学的成绩总和
1 | select sum(math+chinese+english)from student; |
求一个班级数学平均分?
1 | select avg(math) from student; |
求一个班级总分平均分
1 | select avg(math+chinese+english)from student; |
求班级最高分和最低分
1 | select max(math+chinese+english),min(math+chinese+english) from student; |
top-N问题:
1 | 按math成绩从小大的排序, 求math成绩在5-8名的 |
分组数据
为学生表,增加一个班级列,练习分组查询。
1 | alter table student add column class_id int; |
更新表:
1 | update student set class_id=1 where id<=5; |
查出各个班的总分,最高分。
1 | select class_id, sum(chinese+english+math) "总成绩", max(chinese+english+math) "最高分" from student group by class_id; |
求各个班级 英语的平均分:
1 | select classid, avg(english) from student group by class_id |
如根据组函数的语法要求,将select后增加name列,而不加至group by 之后:
1 | select name, classid, avg(english)from student group by classid; |
查询出班级总分大于1300分的班级ID
1 | select class_id from student group by class_id having sum(math+chinese+english)>1300; |