mysql基础练习题(单表练习)
mysql基础练习题
test1
1、创建emp表,设计字段为id,name,age,sex, salary(工资),resume(履历),time(入职时间)。
2、往表中添加数据如下:
insert into emp(name,age,sex,salary,resume,time) values ('a1',18,'女',8888.88,'tester a1','2018-08-08'),
('a2',18,'女',8888.88,'tester a2','2018-08-08'),
('a3',18,'女',8888.88,'tester a3','2018-08-08'),
('a4',18,'女',8888.88,'tester a4','2018-08-08'),
('a5',18,'女',8888.88,'tester a5','2018-08-08');
3、修改入职时间在2010年后的员工工资为20000;
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
update emp set salary=20000 where time>20101231;
4、修改emp表中年龄大于30岁,并且入职时间在2010年后的员工工资为22000;
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
update emp set salary=22000 where time>20101231 and age>30;
5、修改emp表中姓名为'HMM',性别为'女'的员工年龄为18;
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
update emp set age=18 where name='HMM' and sex='女';
6、删除emp表中工资大于20000的员工信息;
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
delete from emp where salary>20000;
7、删除emp表中工资小于8000,且入职时间晚于2020-01-01的员工信息;
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
delete from emp where salary<8000 and time>2020-01-01;
8、查询emp表中的所有员工姓名,年龄以及工资信息;
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
select name,age,salary from emp;
9、查询emp表中年龄大于28岁的所有员工相关信息;
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
select * from emp where age>28;
10、查询emp表中年龄小于25岁,性别为女的员工的姓名,履历以及入职时间等信息;
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
select name,resume,time from emp where age<25 and sex='女';
11、查询emp表中年龄大于20岁,或是性别为女的所有员工的姓名,年龄,工资及入职时间等信息;
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
select name,age,salary,time from emp where age>20 or sex='女';
12、给emp表中入职时间大于1年的员工工资加1000,datediff(now(),time) /timestampdiff(year,time,now()) 计算时间差方法
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
update emp set salary = salary + 1000 where datediff(now(),time) > 365;
update emp set salary = salary + 1000 where timestampdiff(year,time,now()) > 0;
test2
#1:创建表emp表,设计字段为id,name,age,sex, salary(工资),dep(部门),time(入职时间)。#2:往表中添加如下数据:
insert into emp values (1,'a1',18,'女',8888.88,'tester a1','2018-08-08'),
(2,'a2',18,'女',8888.88,'tester a2','2018-08-08'),
(3,'a3',18,'女',8888.88,'tester a3','2018-08-08'),
(4,'a4',18,'女',8888.88,'tester a4','2018-08-08'),
(5,'a5',18,'女',8888.88,'tester a5','2018-08-08');
#3:查询出部门中张姓员工的相关信息;
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
select * from emp where name like '张_';
#4:查询出部门中年龄在18岁到25岁之间的所有员工相关信息
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
select * from emp where age between 18 and 25;
# 5:查询出部门中工资高于20000的员工的相关信息;
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
select * from emp where salary >20000;
# 6:查询出部门中工资高于15000并且年龄小于25并且性别的女的所有员工相关信息;
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
select * from emp where salary>15000 and age<25 and sex='女';
# 7:查询出部门中工资不大于20000的所有员工相关信息;
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
select * from emp where salary<2000;
# 8:查询出部门中员工名字中包含“风”字的员工信息;
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
select * from emp where name like '%风%';
# 9:删除部门中工资大于20000且年龄大于30岁且性别不为女的员工相关信息;
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
delete from emp where salary>20000 and age>30 and sex!='女';
# 10:为部门中入职时间在2010年之前的所有员工增加工资2000;
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
update emp set salary=salary+2000 where year(time)<2010;
# 11:查询出部门名为“软件测试部”中所有员工的一个月工资开销总和;sum()
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
select sum(salary) from emp where dep='软件测试部';
# 12:查询出部门为“软件测试部”中一共有多少员工;count()
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
select count(*) from emp where dep='软件测试部';
# 13:查询出部门为“软件测试部”的最高工资;max()
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
select max(salary) from emp where dep='软件测试部';
# 14:查询出部门为“软件测试部”的平均工资;avg()
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
select avg(salary) from emp where dep='软件测试部';
# 15:查询出部门为“软件测试部”的员工相关信息,并根据工资从低到高进行排序;order by+属性名,asc:升序,desc:降序
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
select * from emp where dep='软件测试部' order by salary;
test3
CREATE TABLE exam(
id INT PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(20),
english INT,
chinese INT,
math INT
);
INSERT INTO exam VALUES (NULL,'张三',85,74,91);
INSERT INTO exam VALUES (NULL,'李四',95,90,83);
INSERT INTO exam VALUES (NULL,'王五',85,84,59);
INSERT INTO exam VALUES (NULL,'赵六',75,79,76);
INSERT INTO exam VALUES (NULL,'田七',69,63,98);
INSERT INTO exam VALUES (NULL,'李老八',89,90,83);
-- 1.查询所有学生考试成绩信息
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
select * from exam;
-- 2.查询所有学生的姓名和英语成绩
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
select name,english from exam;
-- 3.查询英语成绩信息(不显示重复的值)
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
select distinct(english) from exam;
-- 4.查看学生姓名和学生的总成绩
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
select name,(english+chinese+math) zcj from exam group by id;
-- 5.查询学生的姓名和平均分,平均分用avg别名展示
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
select name,round(sum(english+chinese+math)/3,2) ang from exam group by id;
-- 6.查询李四学生的成绩:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
select * from exam where name='李四';
-- 7.查询名称叫李四学生并且英文大于90分
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
select * from exam where name='李四' and english>90;
-- 8.查询姓李的学生的信息 like
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
select * from exam where name like '李%';
-- 9.查询英语成绩是69,75,89学生的信息 in() 包含在
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
select * from exam where english in(69,75,89);
-- 10.查询数学成绩在80-90之间的学生信息 between 80 and 90 两者之间
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
select * from exam where math between 80 and 90;
-- 11.只要有一门不及格,就找出来 or 或者
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
select * from exam where english<60 or chinese<60 or math<60;
-- 12.查询学生信息,并且按照语文成绩进行排序:order by asc 升序(默认),desc降序
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
select * from exam order by chinese asc;
-- 13.查询学生信息,并且按照语文成绩倒序排序:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
select * from exam order by chinese desc;
-- 14.查询学生信息,先按照语文成绩进行倒序排序,如果成绩相同再按照英语成绩升序排序
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
select * from exam order by chinese desc,english asc;
-- 15.查询姓李的学生的信息,按照英语成绩降序排序
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
select * from exam where name like '李%' order by english desc;
-- 16.查询学生信息,按照总成绩排序,只展示学生的姓名和总分(SUM)
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
select name,(english+chinese+math) sum from exam order by sum;
-- 17.获取所有学生的英语成绩的总和:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
select sum(english) from exam;
-- 18.获取所有学生的英语成绩和数学成绩总和:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
select sum(english),sum(math) from exam;
-- 19.查询姓李的学生的英语成绩的总和
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
select sum(english) from exam where name like '李%';
-- 20.查询所有学生各科的总成绩:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
select sum(english),sum(chinese),sum(math) from exam;
-- 21.获得姓李的学生的个数
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
select count(1) from exam where name like '李%';
-- 22.获得数学成绩的最高分:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
select max(math) from exam;
-- 23.获得语文成绩的最小值
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
select min(chinese) from exam;
-- 24.获取语文成绩的平均值
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
select round(avg(chinese),2) from exam;
test4
CREATE TABLE stu_2(
id INT NOT NULL,
NAME VARCHAR(20) NOT NULL,
age INT NOT NULL,
class INT NOT NULL,
grade VARCHAR(10) NOT NULL,
chinese INT,
english INT,
math INT,
createtime DATE,
birthday DATE,
PRIMARY KEY(id)
);
INSERT INTO stu_2 VALUES (1,'张三',21,19330,'大专',90,80,85,19980101,20010211);
INSERT INTO stu_2 VALUES (2,'李四',22,19330,'大学',90,80,85,19980101,20010211);
INSERT INTO stu_2 VALUES (3,'王五',24,19330,'硕士',90,80,85,19980101,20010211);
INSERT INTO stu_2 VALUES (4,'赵六',25,19330,'博士',90,80,85,19980101,20010211);
UPDATE stu_2 SET chinese=75 WHERE NAME='李四';
UPDATE stu_2 SET chinese=85 WHERE NAME='张三';
UPDATE stu_2 SET class="19331" WHERE NAME='赵六'
select * from stu_2;
# 3:查询出stu_2表中张姓同学的各科成绩信息;
select * from stu_2 where name like '张%';
# 4:查询出stu_2表中姓名包含“王”的同学的相关信息;
select * from stu_2 where name like '%王%';
# 5:查询出stu_2表中年龄在18到28之间,语文成绩在80分以上的所有学员姓名,年龄信息;
select name,age from stu_2 where age between 18 and 28 and chinese>80;
# 6:查询出stu_2表中年龄在18到28之间,总分成绩在180分以上的所有学员的姓名,年龄,各科成绩信息;
select name,age from stu_2 where age between 18 and 28 and chinese>80;
# 7:查询出stu_2表中数学成绩为88,或是英语成绩大于70,或是语文成绩大于90的所有学员相关信息;
select * from stu_2 where math=88 or english>70 or chinese>90;
# 8:查询出stu_2表中数学成绩在77,88,90之间的所有学员相关信息;
select * from stu_2 where math in (77,88,90);
# 9:查询出stu_2表中语文成绩最高分,数学成绩最低分,语文成绩的平均分;
select max(chinese),min(math),avg(chinese) from stu_2;
# 10:查询出stu_2表中学员各科成绩的平均分;
select round((sum(chinese))/count(id),2),round((sum(math))/count(id),2),round((sum(english))/count(id),2) from stu_2;
# 11:统计出19330班一共有多少学员;
select count(id) from stu_2 where class="19330";
# 12:统计出19330班学员的语文成绩总分,各科成绩总分;
select sum(chinese),sum(english),sum(math) from stu_2;
# 13:查询出二年级的班级信息;
select * from stu_2 where grade='大专';
# 14:查询出2010后出生的学员姓名及班级信息;
select name,class,birthday from stu_2 where year(birthday)>2010;
# 15:查询出19330班中总分成绩在200分以上的学员相关信息
select * from stu_2 where class="19330" and (chinese+english+math);
# 16:求出每个班中的各单科成绩最高分,最低分,及平均分,班级综合成绩最高分,班级总分;
select max(chinese),MAX(english),MAX(math),MIN(chinese),MIN(english),MIN(math)
,avg(chinese),AVG(english),AVG(math),max(chinese+math+english),
(chinese+math+english) from stu_2 group by class;
作者:×不知该怎么√ 收藏一直进行,学习从未开始!地址https://www.cnblogs.com/hqh2021/p/16581271.html