mysql基础练习题

创建数据库

create database mydb

使用数据库

use mydb

创建表

create table exam(
id int primary key auto_increment,
name varchar(20) not null,
chinese double,
math double,
english double
);

插入数据

insert into exam values(null,‘关羽’,85,76,70);
insert into exam values(null,‘张飞’,70,75,70);
insert into exam values(null,‘赵云’,90,65,95);
insert into exam values(null,‘刘备’,97,50,50);
insert into exam values(null,‘曹操’,90,89,80);
insert into exam values(null,‘司马懿’,90,67,65);

查询

– 1、查询表中所有学生的信息。
select * from exam;
– 2、查询表中所有学生的姓名和对应的英语成绩。
select name,english from exam;
– 3、过滤表中重复数据。
select distinct name from exam;
– 4、在所有学生分数上加10分特长分。
select name,chinese+10 from exam;
– 5、统计每个学生的总分。
select chinese+math+english,name from exam;
– 6、使用别名表示学生分数。
select chinese+math+english as 总分,name 姓名 from exam;

查询使用where子句

– 7、查询姓名为刘备的学生成绩
select * from exam where name=‘刘备’;
– 8、查询英语成绩大于90分的同学
select * from exam where english>90;
– 9、查询总分大于200分的所有同学
select * from exam where chinese+math+english>200;
– 10、查询英语分数在 80-90之间的同学。
select * from exam where english>80 and english<90
– 11、查询数学分数为89,75,91的同学。
select * from exam where math in(89,90,91);
– 12、查询所有姓刘的学生成绩。
select * from exam where name like ‘刘%’;
– 13、查询所有姓刘两个字的学生成绩。
select * from exam where name like ‘刘_’;
– 14、查询数学分>80并且语文分>80的同学。
select * from exam where math>80 and chinese >80;
– 15、查询数学分>80 或者 语文分>80的同学。
select * from exam where math>80 or chinese >80;

查询使用order by 排序

– 16、对数学成绩排序后输出。
select * from exam order by math asc;
– 17、对总分排序按从高到低的顺序输出
select *,math+chinese+english as 总分 from exam order by (math+chinese+english) desc;
– 18、对姓刘的学生成绩排序输出
select name,math+chinese+english as 总分 from exam where name like ‘刘%’ order by 总分 desc

查询使用count(函数)

– 19、统计一个班级共有多少学生?
select count() from exam;
– 20、统计数学成绩大于或等于90的学生有多少个?
select count(
) from exam where math>=90;
– 21、统计总分大于250的人数有多少?
select count(*) from exam where (math+chinese+english)>=250;

查询使用sum函数

– 22、统计一个班级数学总成绩?
select sum(math) from exam;
– 23、统计一个班级语文、英语、数学各科的总成绩
select sum(chinese) 语文,sum(english) 英语,sum(math) 数学 from exam;
– 24、统计一个班级语文、英语、数学的成绩总和
select sum(math+chinese+english) 总和 from exam;
– 25、统计一个班级语文成绩平均分
select sum(chinese) /count(*)from exam;

查询使用avg函数

– 26、求一个班级数学平均分?
select sum(math) /count()from exam;
– 27、求一个班级总分平均分
select sum(math+chinese+english)/count(
) 总和 from exam;

查询使用max,min函数

– 28、求班级最高分和最低分(数值范围在统计中特别有用)
select max(math+chinese+english) 最高分,min(math+chinese+english) 最低分 from exam;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值