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;

update emp set salary=20000 where time>20101231;
点击查看

4、修改emp表中年龄大于30岁,并且入职时间在2010年后的员工工资为22000;

update emp set salary=22000 where time>20101231 and age>30;
点击查看

5、修改emp表中姓名为'HMM',性别为'女'的员工年龄为18;

update emp set age=18 where name='HMM' and sex='女';
点击查看

6、删除emp表中工资大于20000的员工信息;

delete from emp where salary>20000;
点击查看

7、删除emp表中工资小于8000,且入职时间晚于2020-01-01的员工信息;

delete from emp where salary<8000 and time>2020-01-01;
点击查看

8、查询emp表中的所有员工姓名,年龄以及工资信息;

select name,age,salary from emp;
点击查看

9、查询emp表中年龄大于28岁的所有员工相关信息;

select * from emp where age>28;
点击查看

10、查询emp表中年龄小于25岁,性别为女的员工的姓名,履历以及入职时间等信息;

select name,resume,time from emp where age<25 and sex='女';
点击查看

11、查询emp表中年龄大于20岁,或是性别为女的所有员工的姓名,年龄,工资及入职时间等信息;

select name,age,salary,time from emp where age>20 or sex='女';
点击查看

12、给emp表中入职时间大于1年的员工工资加1000,datediff(now(),time) /timestampdiff(year,time,now())  计算时间差方法

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:查询出部门中张姓员工的相关信息;

select * from emp where name like '张_';
点击查看

#4:查询出部门中年龄在18岁到25岁之间的所有员工相关信息

select * from emp where age between 18 and 25;
点击查看

# 5:查询出部门中工资高于20000的员工的相关信息;

select * from emp where salary >20000;
点击查看

# 6:查询出部门中工资高于15000并且年龄小于25并且性别的女的所有员工相关信息;

select * from emp where salary>15000 and age<25 and sex='女';
点击查看

# 7:查询出部门中工资不大于20000的所有员工相关信息;

select * from emp where salary<2000;
点击查看

# 8:查询出部门中员工名字中包含“风”字的员工信息;

select * from emp where name like '%风%';
点击查看

# 9:删除部门中工资大于20000且年龄大于30岁且性别不为女的员工相关信息;

delete from emp where salary>20000 and age>30 and sex!='女';
点击查看

# 10:为部门中入职时间在2010年之前的所有员工增加工资2000;

update emp set salary=salary+2000 where year(time)<2010;
点击查看

# 11:查询出部门名为“软件测试部”中所有员工的一个月工资开销总和;sum()

select sum(salary) from emp where dep='软件测试部';
点击查看

# 12:查询出部门为“软件测试部”中一共有多少员工;count()

select count(*) from emp where dep='软件测试部';
点击查看

# 13:查询出部门为“软件测试部”的最高工资;max()

select max(salary) from emp where dep='软件测试部';
点击查看

# 14:查询出部门为“软件测试部”的平均工资;avg()

select avg(salary) from emp where dep='软件测试部';
点击查看

# 15:查询出部门为“软件测试部”的员工相关信息,并根据工资从低到高进行排序;order by+属性名,asc:升序,desc:降序  

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.查询所有学生考试成绩信息

select * from exam;
点击查看

-- 2.查询所有学生的姓名和英语成绩

select name,english from exam;
点击查看

-- 3.查询英语成绩信息(不显示重复的值)

select distinct(english) from exam;
点击查看

-- 4.查看学生姓名和学生的总成绩

select name,(english+chinese+math) zcj from exam group by id;
点击查看

-- 5.查询学生的姓名和平均分,平均分用avg别名展示

select name,round(sum(english+chinese+math)/3,2) ang from exam group by id;
点击查看

-- 6.查询李四学生的成绩:

select * from exam where name='李四';
点击查看

-- 7.查询名称叫李四学生并且英文大于90分

select * from exam where name='李四' and english>90;
点击查看

-- 8.查询姓李的学生的信息 like

select * from exam where name like '李%';
点击查看

-- 9.查询英语成绩是69,75,89学生的信息 in() 包含在

select * from exam where english in(69,75,89);
点击查看

-- 10.查询数学成绩在80-90之间的学生信息 between 80 and 90 两者之间

select * from exam where math between 80 and 90;
点击查看

-- 11.只要有一门不及格,就找出来 or 或者

select * from exam where english<60 or chinese<60 or math<60;
点击查看

-- 12.查询学生信息,并且按照语文成绩进行排序:order by asc 升序(默认),desc降序

select * from exam order by chinese asc;
点击查看

-- 13.查询学生信息,并且按照语文成绩倒序排序:

select * from exam order by chinese desc;
点击查看

-- 14.查询学生信息,先按照语文成绩进行倒序排序,如果成绩相同再按照英语成绩升序排序

select * from exam order by chinese desc,english asc;
点击查看

-- 15.查询姓李的学生的信息,按照英语成绩降序排序

select * from exam where name like '李%' order by english desc;
点击查看

-- 16.查询学生信息,按照总成绩排序,只展示学生的姓名和总分(SUM)

select name,(english+chinese+math) sum from exam order by sum;
点击查看

-- 17.获取所有学生的英语成绩的总和:

select sum(english) from exam;
点击查看

-- 18.获取所有学生的英语成绩和数学成绩总和:

select sum(english),sum(math) from exam;
点击查看

-- 19.查询姓李的学生的英语成绩的总和

select sum(english) from exam where name like '李%';
点击查看

-- 20.查询所有学生各科的总成绩:

select sum(english),sum(chinese),sum(math) from exam;
点击查看

-- 21.获得姓李的学生的个数

select count(1) from exam where name like '李%';
点击查看

-- 22.获得数学成绩的最高分:

select max(math) from exam;
点击查看

-- 23.获得语文成绩的最小值

select min(chinese) from exam;
点击查看

-- 24.获取语文成绩的平均值

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;

posted on 2022-08-15 19:19  ×不知该怎么√  阅读(1019)  评论(0编辑  收藏  举报

导航