创建表
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);
select * from emp where deptno=30;
select ename,empno,deptno from emp where job='销售员';
select ename from emp where comm>sai;
select ename from emp where comm>sai*0.6;
5.找出部门编号为10中所有经理,和部门编号为20中所有销售员的详细资料。
select * from emp where deptno=10 and job='经理' or deptno=20 and job='销售员';
6.找出部门编号为10中所有经理,部门编号为20中所有销售员,还有即不是经理又不是销售员但其工资大或等于20000的所有员工详细资料。
select * from emp where deptno=10 and job='经理' or deptno=20 and job='销售员'or job!='经理' and job!='销售员' and sai>=20000;
select * from emp where comm<1000 or comm is null;
– 注意:一个汉字占三个字节
select * from emp where char_length(ename)=3;
select * from emp where year(hiredate)=2000;
select * from emp order by empno;
11.查询所有员工详细信息,用工资降序排序,如果工资相同使用入职日期升序排序
select * from emp order by sai desc,hiredate;
select deptno,avg(sai) from emp group by deptno;
select deptno,count(1) from emp group by deptno;
select job,max(sai),min(sai),count(1) from emp group by job;
创建部门表
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 d.name,e.ename from dept3 d,emp3 e where d.deptno=e.dept_id;
select d.name,e.ename from dept3 d,emp3 e where d.deptno=e.dept_id and d.name='研发部';
select d.name,e.ename from dept3 d,emp3 e where d.deptno=e.dept_id and (d.name='研发部' or d.name='销售部');
4、查询每个部门的员工数,并升序排序
select d.name,count(e.ename) num from dept3 d,emp3 e where d.deptno=e.dept_id group by d.name order by num asc
5、查询人数大于等于3的部门,并按照人数降序排序
select * from (select d.name,count(e.ename) num from dept3 d,emp3 e where re d.deptno=e.dept_id group by d.name order by num asc) n where n.num>=3;