一、单表查询练习
/* 素材
CREATE TABLE emp (
empno int(4) NOT NULL,
ename varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
job varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
mgr int(4) NULL DEFAULT NULL,
hiredate date NOT NULL,
sai int(255) NOT NULL,
comm int(255) NULL DEFAULT NULL,
deptno int(2) NOT NULL,
PRIMARY KEY ( empno ) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;
INSERT INTO emp VALUES (1001, ' 甘宁 ', ' 文员 ', 1013, '2000-12-17', 8000, NULL, 20);
INSERT INTO emp VALUES (1002, ' 黛绮丝 ', ' 销售员 ', 1006, '2001-02-20', 16000, 3000, 30);
INSERT INTO emp VALUES (1003, ' 殷天正 ', ' 销售员 ', 1006, '2001-02-22', 12500, 5000, 30);
INSERT INTO emp VALUES (1004, ' 刘备 ', ' 经理 ', 1009, '2001-04-02', 29750, NULL, 20);
INSERT INTO emp VALUES (1005, ' 谢逊 ', ' 销售员 ', 1006, '2001-09-28', 12500, 14000, 30);
INSERT INTO emp VALUES (1006, ' 关羽 ', ' 经理 ', 1009, '2001-05-01', 28500, NULL, 30);
INSERT INTO emp VALUES (1007, ' 张飞 ', ' 经理 ', 1009, '2001-09-01', 24500, NULL, 10);
INSERT INTO emp VALUES (1008, ' 诸葛亮 ', ' 分析师 ', 1004, '2007-04-19', 30000, NULL, 20);
INSERT INTO emp VALUES (1009, ' 曾阿牛 ', ' 董事长 ', NULL, '2001-11-17', 50000, NULL, 10);
INSERT INTO emp VALUES (1010, ' 韦一笑 ', ' 销售员 ', 1006, '2001-09-08', 15000, 0, 30);
INSERT INTO emp VALUES (1011, ' 周泰 ', ' 文员 ', 1006, '2007-05-23', 11000, NULL, 20);
INSERT INTO emp VALUES (1012, ' 程普 ', ' 文员 ', 1006, '2001-12-03', 9500, NULL, 30);
INSERT INTO emp VALUES (1013, ' 庞统 ', ' 分析师 ', 1004, '2001-12-03', 30000, NULL, 20);
INSERT INTO emp VALUES (1014, ' 黄盖 ', ' 文员 ', 1007, '2002-01-23', 13000, NULL, 10);
INSERT INTO emp VALUES (1015, ' 张三 ', ' 保洁员 ', 1001, '2013-05-01', 80000, 50000, 50);
1. 查询出部门编号为30的所有员工
select * from emp where deptno = 30;
2. 所有销售员的姓名、编号和部门编号。
select ename,empno,mgr from emp;
3. 找出奖金高于工资的员工。
select * from emp where sai < comm;
4. 找出奖金高于工资60%的员工。
select * from emp where sai < (comm * 0.6);
5. 找出部门编号为10中所有经理,和部门编号为20中所有销售员的详细资料。
select * from emp where deptno in (10,20) and job in ('经理','销售员');
6. 找出部门编号为10中所有经理,部门编号为20中所有销售员,还有即不是经理又不是销售员但其工资大或等于20000的所有员工详细资料。
select * from emp where (deptno = 10 and job = '经理') or (deptno = 20 and job = '销售员') or (sai >= 20000 and job != '经理' and job !='销售员');
7. 无奖金或奖金低于1000的员工。
select * from emp where comm is null or comm < 1000;
8. 查询名字由三个字组成的员工。
select * from emp where length(ename) = 9;
9.查询2000年入职的员工。
select * from emp where year(hiredate)=2000;
10. 查询所有员工详细信息,用编号升序排序
select * from emp order by empno;
11. 查询所有员工详细信息,用工资降序排序,如果工资相同使用入职日期升序排序
select * from emp order by sai desc,hiredate;
12.查询每个部门的平均工资
select deptno,avg(sai) as total from emp group by deptno;
13.查询每个部门的雇员数量
select deptno,count(job) as total from emp group by deptno;
14.查询每种工作的最高工资、最低工资、人数
select job,max(sai),min(sai),count(job) from emp group by job;
二、多表连接查询
use mydb3;
-- 创建部门表
create table if not exists dept3(
deptno varchar(20) primary key , -- 部门号
name varchar(20) -- 部门名字
);
-- 创建员工表
create table if not exists emp3(
eid varchar(20) primary key , -- 员工编号
ename varchar(20), -- 员工名字
age int, -- 员工年龄
dept_id varchar(20) -- 员工所属部门
);
-- 给 dept3 表添加数据
insert into dept3 values('1001',' 研发部 ');
insert into dept3 values('1002',' 销售部 ');
insert into dept3 values('1003',' 财务部 ');
insert into dept3 values('1004',' 人事部 ');
-- 给 emp3 表添加数据
insert into emp3 values('1',' 乔峰 ',20, '1001');
insert into emp3 values('2',' 段誉 ',21, '1001');
insert into emp3 values('3',' 虚竹 ',23, '1001');
insert into emp3 values('4',' 阿紫 ',18, '1001');
insert into emp3 values('5',' 扫地僧 ',85, '1002');
insert into emp3 values('6',' 李秋水 ',33, '1002');
insert into emp3 values('7',' 鸠摩智 ',50, '1002');
insert into emp3 values('8',' 天山童姥 ',60, '1003');
insert into emp3 values('9',' 慕容博 ',58, '1003');
insert into emp3 values('10',' 丁春秋 ',71, '1005');
1 查询每个部门的所属员工
select name,GROUP_CONCAT(ename)from emp3 e RIGHT JOIN dept3 d on e.dept_id = d.deptno GROUP BY name ;
2 查询研发部门的所属员工
select name,GROUP_CONCAT(ename) from emp3 e RIGHT JOIN dept3 d on e.dept_id = d.deptno and name='研发部';
3、查询研发部和销售部的所属员工
select name, GROUP_CONCAT(ename) from emp3 e RIGHT JOIN dept3 d on e.dept_id = d.deptno group by name and d.name='研发部' or d.name='销售部' ;
4、查询每个部门的员工数,并升序排序
select name,count(deptno) as total from emp3 e left JOIN dept3 d on e.dept_id = d.deptno GROUP BY d.name HAVING NAME is not null ORDER BY total;
5、查询人数大于等于3的部门,并按照人数降序排
select name,count(deptno) as total from emp3 e left JOIN dept3 d on e.dept_id = d.deptno GROUP BY d.name HAVING NAME is not null and total > 1 ORDER BY total desc
新增员工表emp和部门表dept
create table dept (dept1 int ,dept_name varchar(11));
create table emp (sid int ,name varchar(11),age int,worktime_start date,incoming int,dept2 int);
insert into dept values
(101,'财务'),
(102,'销售'),
(103,'IT技术'),
(104,'行政');
insert into emp values
(1789,'张三',35,'1980/1/1',4000,101),
(1674,'李四',32,'1983/4/1',3500,101),
(1776,'王五',24,'1990/7/1',2000,101),
(1568,'赵六',57,'1970/10/11',7500,102),
(1564,'荣七',64,'1963/10/11',8500,102),
(1879,'牛八',55,'1971/10/20',7300,103);
1.找出销售部门中年纪最大的员工的姓名
select name from emp e ,dept d where e.dept2=d.dept1 and dept_name='销售' and age=(select max(age) from emp where dept2=(select dept1 from dept where dept_name='销售'));
2.求财务部门最低工资的员工姓名
select name from emp e ,dept d where e.dept2=d.dept1 and dept_name='财务' and incoming=(
select min(incoming) from emp where dept2=(select dept1 from dept where dept_name='务'));
3.列出每个部门收入总和高于9000的部门名称
select dept_name from dept d,(select dept2 from emp group by dept2 having sum(incoming)> 9000 ) e where d.dept1=e.dept2;
4.求工资在7500到8500元之间,年龄最大的人的姓名及部门
select dept_name,`name` from emp e RIGHT JOIN dept d on e.dept2 = d.dept1 WHERE incoming >= 7500 AND incoming <= 8500 and d.dept_name = '销售' ORDER BY age desc LIMIT 1;
5.找出销售部门收入最低的员工入职时间
SELECT worktime_start FROM emp e RIGHT JOIN dept d ON e.dept2 = d.dept1 where d.dept_name = '销售' ORDER BY incoming ASC LIMIT 1;
6.财务部门收入超过2000元的员工姓名
select name from emp e RIGHT JOIN dept d on e.dept2 = d.dept1 where d.dept_name = '财务' and incoming > 2000 ;
7.列出每个部门的平均收入及部门名称
select dept_name,AVg(incoming) from emp e RIGHT JOIN dept d on e.dept2 = d.dept1 group by dept_name;
8.IT技术部入职员工的员工号
select sid from emp e RIGHT JOIN dept d on e.dept2 = d.dept1 where d.dept_name = 'IT技术'
9.财务部门的收入总和;
select dept_name,sum(incoming) from emp e RIGHT JOIN dept d on e.dept2 = d.dept1 WHERE d.dept_name = '财务' ;
10.先按部门号大小排序,再依据入职时间由早到晚排序员工信息表
select * from emp e RIGHT JOIN dept d on e.dept2 = d.dept1 ORDER BY dept2,worktime_start;
11.找出哪个部门还没有员工入职;
select dept_name from emp e RIGHT JOIN dept d on e.dept2 = d.dept1 WHERE worktime_start is NULL;
12.列出部门员工收入大于7000的部门编号,部门名称;
select dept1,dept_name from emp e RIGHT JOIN dept d on e.dept2 = d.dept1 WHERE incoming > 7000;
13.列出每一个部门的员工总收入及部门名称;
select dept_name,sum(incoming) from emp e RIGHT JOIN dept d on e.dept2 = d.dept1 group by dept_name
14.列出每一个部门中年纪最大的员工姓名,部门名称;
select MAX(age),dept_name from emp e RIGHT JOIN dept d on e.dept2 = d.dept1 group by dept_name
15.求李四的收入及部门名称
select `name`,incoming,dept_name from emp e RIGHT JOIN dept d on e.dept2 = d.dept1 where name = '李四'
16.列出每个部门中收入最高的员工姓名,部门名称,收入,并按照收入降序
select name,MAX(incoming),dept_name from emp e RIGHT JOIN dept d on e.dept2 = d.dept1 group by dept_name ;
17.列出部门员工数大于1个的部门名称
select dept_name,dept2 from emp e RIGHT JOIN dept d on e.dept2 = d.dept1 group by dept_name HAVING COUNT(dept2) > 1;
19.查找张三所在的部门名称
select dept_name from emp e RIGHT JOIN dept d on e.dept2 = d.dept1 where name = '张三'