数据库之单表练习(题目)
建表语句:
一个学生表
分别记录姓名,年龄,性别,班级,语文,数学,英语字段
create table student2(id int primary key ,name char(20),sex char(10),age int(3),mobile char(20),class char(10),english int(10),chinese int(10),
math int(10))engine=innodb default charset=utf8;
insert into student2 values(1,'小红','女',23,'13813828824','1719',77,88,98),(2,'小明','男',23,'13713713711','1720',56,66,55),
(3,'小李','男',23,'15915913911','1719',78,64,87),(4,'小张','男',23,'15915913912','1720',77,76,77),(5,'小白','女',24,'15915913913','1719',90,89,98),
(6,'小陈','女',19,'15915913914','1719',84,100,81),(7,'小钱','女',20,'15915913915',null,45,99,93);
==========================================
题目1:查询1719班学生的成绩信息
结果:english ,chinese ,math
条件:class=1719
方法1:SELECT english,chinese,math from student2 where class="1719" ;(精准)
方法2: select name,english,chinese,math from student where class=1719;
题目2:查询1719班学生语文成绩大于80小于90的学生信息
结果: 学生信息 指的是所有信息 * 表示所有
条件:Chinese>80 ,chinese <90 ,class=1719
方法1:SELECT * FROM student2 WHERE chinese>80 and chinese<90 and class="1719";
方法2:select * from student where class=1719 and chinese between 81 and 89;
题目3:查询学生表中第5-7行的数据信息
结果:数据信息 , 所有信息 * 表示
条件:limit 567 三行 5的索引是4(索引从0开始) ,步长3
方法1:SELECT * from student2 limit 4,3;
错误:
1、 select * from student2 where id BETWEEN 5 and 7 ;(id不能确定5-7行)
2、SELECT * from student2 limit 4,7; (7表示7行,实际要3行)
3、select * from student2 where id in(5,6,7); 只能取到id5,6,7
题目4:显示1719班英语成绩为90,数学成绩为98的name与mobile信息
结果: name ,moblie
条件: class=1719 ,english=90, math=98
方法1:select name,mobile FROM student2 WHERE class="1719" and english=90 AND math=98;(不建议in)
题目5:显示1719班学生信息并且以语文成绩降序排序
结果:学生信息 所有*
条件 :class=1719 order by chinese desc
方法1:select* from student2 where class="1719" order by chinese desc
方法2:SELECT *FROM student2 WHERE class IN (1719) ORDER BY chinese DESC;
题目6:查询1719与1720班,语文成绩与数学成绩都小于80的name与mobile信息
结果:name ,mobile
条件:class=1719,class=1720 , chinese<80, math<80;
方法1:SELECT name,mobile from student2 WHERE class in (1719,1720)and chinese<80 and math < 80;
方法2:select name,mobile from student2 where(class=1719 or class="1720") and chinese<80 and math<80;
方法3:SELECT NAME,mobile from student2 WHERE (class=1719 or class=1720) and (chinese<80 and math<80);
方法4:select name, mobile from student2 where( chinese<80 and math<80 and class="1719") or (class=1720 and chinese<80 and math<80);
题目7:查询英语平均分大于80分的班级,英语平均分
结果:class , avg(english)
条件: group by class , avg(english)>80
方法1:Select class,avg(english) from student2 group by class having avg(english)>80;
方法2: SELECT class,avg(english) s from student2 GROUP BY class having s>80 ;
题目8:按班级查出数学最高分
结果:显示 class max(math)
条件:group by class
方法1:select class,max(math) from student2 GROUP BY class;
题目9:查询出每班数学最低分
结果:class min(math)
条件:group by class
方法1:select class,min(math) from student2 GROUP BY class ;
题目10:查询每班数学总分
结果:class sum(math)
条件:group by class
方法1:select class,sum(math) from student2 GROUP BY class
方法2:select sum(math),class from student2 where class is not null group by class ;
题目11:查询每班数学平均分
结果:class avg(math)
条件:group by class
方法1:select class,avg(math) from student2 GROUP BY class
题目12:查询出每班学生总数
注意:统计人数可以(,字段名)
结果:class count(math)
条件:group by class
方法1:select class,count(id) from student2 GROUP BY class ;
方法2:select count(),class from student2 where class is not null group by class ;
方法3:select class,count(name) from student2 group by class ;
题目13:在表中插入一条小谢的成绩数据
结果:显示一条数据
条件 : name=小谢
方法1:INSERT INTO student2 VALUES(8,'小谢','女',20,'15915913615',1719,50,80,92);
方法2:INSERT INTO student2 (id ,name) VALUES (9,'小谢') ;
题目14:把英语分数小于60的同学分数改为60分
结果:显示所有信息
条件: update english =60
方法1:update student2 set english=60 where english<60;
==================================================================
数据库之单表练习:
建表语句:
建表:
部门表
create table dept(deptno int primary key,
dname varchar(14), -- 部门名称
loc varchar(13)-- 部门地址
)default charset=utf8 ;
insert into dept values (10,'会计部','纽约');
insert into dept values (20,'技术部','达拉斯');
insert into dept values (30,'销售部','芝加哥');
insert into dept values (40,'运营部','波士顿');
select * from dept
员工表
drop table emp;
create table emp(
empno int primary key, -- 员工编号
ename varchar(10), -- 员工姓名
job varchar(9), -- 员工工作
mgr int, -- 员工直属领导编号
hiredate date, -- 入职时间
sal double, -- 工资
comm double, -- 奖金
deptno int, -- 所在部门
foreign key (deptno) references dept(deptno)) default charset=utf8; -- 关联dept表
-- alter table emp add foreign key (deptno) references dept(deptno);
insert into emp values(7369,'smith','职员',7566,"1980-12-17",800,null,20);
insert into emp values(7499,'allen','销售员',7698,'1981-02-20',1600,300,30);
insert into emp values(7521,'ward','销售员',7698,'1981-02-22',1250,500,30);
insert into emp values(7566,'jones','经理',7839,'1981-04-02',2975,null,20);
insert into emp values(7654,'martin','销售员',7698,'1981-09-28',1250,1400,30);
insert into emp values(7698,'blake','经理',7839,'1981-05-01',2850,null,30);
insert into emp values(7782,'clark','经理',7839,'1981-06-09',2450,null,10);
insert into emp values(7788,'scott','职员',7566,'1987-07-03',3000,2000,20);
insert into emp values(7839,'king','董事长',null,'1981-11-17',5000,null,10);
insert into emp values(7844,'turners','销售员',7698,'1981-09-08',1500,50,30);
insert into emp values(7876,'adams','职员',7566,'1987-07-13',1100,null,20);
insert into emp values(7900,'james','职员',7698,'1981-12-03',1250,null,30);
insert into emp values(7902,'ford','销售员',7566,'1981-12-03',3000,null,20);
insert into emp values(7934,'miller','职员',7782,'1981-01-23',1300,null,10);
===========================
单表题目:
1、查找部门30中员工的详细信息。
结果: 详细信息 用* 表示;
条件:dept=30
方法1:select * from emp WHERE deptno=30;
2、找出从事职员工作的员工的编号、姓名、部门号。(clerk 职员,办事员)
结果:empno,ename,deptno
条件:job=“职员”
方法1:SELECT empno,ename,deptno FROM emp WHERE job="职员"
3、检索出奖金多于基本工资的员工信息。
结果:员工信息 用* 表示
条件:comm >sal
方法1:SELECT * from emp WHERE comm>sal;
4、检索出奖金多于基本工资60%的员工信息。
结果:员工信息 用* 表示
条件: sal0.6<comm
方法1:select * from emp where comm>sal0.6;
5、找出10部门的经理、20部门的职员 的员工信息。(manager经理;管理
结果:员工信息 用* 表示员工
条件: deptno=10 and job="经理" ,or , deptno=20 and job="职员"
方法1:select * from emp where (deptno=10 and job="经理") or (deptno=20 and job="职员") ;
6、找出10部门的经理、20部门的职员 或者既不是经理也不是职员但是工资高于2000元的员工信息。(sal 工资)
结果:员工信息 用* 表示员工
条件: deptno=10 and job="经理" ,deptno=20 and job="职员" , job !="经理" and job !="职员" and sal>2000
方法一:select * from emp where (deptno=10 and job='经理') or (deptno=20 and job='职员') or ( (job!='职员' and job!='理') and sal>2000 );
7、找出获得奖金的员工的工作。(comm奖金)
结果: job
条件:comm is not null 或 comm>0
方法1:SELECT DISTINCT(job) from emp WHERE comm>0;
方法2:select DISTINCT(job) from emp where comm is not null;
方法3:select job from emp where comm!="null";
8、找出奖金少于100或者没有获得奖金的员工的信息。
结果:员工信息 用* 表示员工
条件:comm<100 or is null
方法1:select * from emp where comm<100 or comm is null;
9、找出姓名以a、b、s开始的员工信息.
结果:员工信息 用* 表示员工
条件:like a% , b% , %s
方法:select * from emp where ename like "a%" or ename like "b%" or ename like "s%";
10、找到名字长度为6个字符的员工信息。
结果:员工信息 用* 表示员工
条件: like
方法1:select * from emp WHERE ename like "______" ;
方法2:select * from emp where length(ename)=6;
11、名字中不包含r字符的员工信息。
结果:员工信息 用* 表示员工
条件: like ,not like
方法:select * from emp WHERE ename not LIKE "%r%" ;
12、查找员工的详细信息并按姓名排序。
结果:员工信息 用* 表示员工
条件: order by ename
方法:select *from emp order by ename;
13、返回员工的信息并按工作降序工资升序排列。(升序的规则)
二次排序
结果:员工信息 用* 表示员工
条件:job desc ,sal asc
方法:SELECT * FROM emp ORDER BY job DESC , sal ASC;
14、计算员工的日薪(按30天)。(加奖金,不加奖金)
结果:ename ,sal/30
方法:SELECT sal/30,ename from emp;
15、找出姓名中包含a的员工信息。
结果:员工信息 用* 表示员工
条件:like %a%
方法:select * from emp where ename like '%a%';
SELECT * from emp where comm>60%sal;