CREATE TABLE dept (
deptno INT(2) NOT NULL COMMENT '部门编号',
dname VARCHAR (15) COMMENT '部门名称',
loc VARCHAR (20) COMMENT '地理位置'
);-- 添加主键
ALTER TABLE dept ADD PRIMARY KEY (deptno);-- 添加数据
INSERT INTO dept (deptno,dname,loc)VALUES (10,'财务部','高新四路');
INSERT INTO dept (deptno,dname,loc)VALUES (20,'人事部','科技二路');
INSERT INTO dept (deptno,dname,loc)VALUES (30,'销售部','长安区');
INSERT INTO dept (deptno,dname,loc)VALUES (40,'运输部','雁塔区');
CREATE TABLE emp(
empno INT(4) NOT NULL COMMENT '员工编号',
ename VARCHAR(10) COMMENT '员工名字',
job VARCHAR(10) COMMENT '职位',
mgr INT(4) COMMENT '上司',
hiredate DATE COMMENT '入职时间',
sal INT(7) COMMENT '基本工资',
comm INT(7) COMMENT '补贴',
deptno INT(2) COMMENT '所属部门编号'
);-- 添加主键
ALTER TABLE emp ADD PRIMARY KEY (empno);-- 添加外键约束
ALTER TABLE emp ADD CONSTRAINT f_ed_key
FOREIGN KEY (deptno)
REFERENCES dept(deptno);INSERT INTO `emp` VALUES('7369','张倩','办事员','7902','2002-12-17','820',NULL,'20');
INSERT INTO `emp` VALUES('7499','刘博','售货员','7698','1992-02-20','1900','300','30');
INSERT INTO `emp` VALUES('7521','李兴','售货员','7698','1995-07-22','1250','500','30');
INSERT INTO `emp` VALUES('7566','李雷','人事部长','7839','1991-04-02','975',NULL,'20');
INSERT INTO `emp` VALUES('7654','刘浩','售货员','7698','1991-09-28','1250','1400','30');
INSERT INTO `emp` VALUES('7698','刘涛','销售部长','7839','1997-05-01','2850',NULL,'30');
INSERT INTO `emp` VALUES('7782','华仔','人事部长','7839','1995-06-09','2450',NULL,'10');
INSERT INTO `emp` VALUES('7788','张飞','人事专员','7566','1998-04-19','3000',NULL,'20');
INSERT INTO `emp` VALUES('7839','马晓云','董事长',NULL,'1991-11-17','5000',NULL,'10');
INSERT INTO `emp` VALUES('7844','马琪','售货员','7698','1996-09-08','1500','0','30');
INSERT INTO `emp` VALUES('7876','李涵','办事员','7788','1997-05-23','1100',NULL,'20');
INSERT INTO `emp` VALUES('7900','李小涵','销售员','7698','1993-2-13','950',NULL,'30');
INSERT INTO `emp` VALUES('7902','张三','人事组长','7566','1992-10-08','3000',NULL,'20');
INSERT INTO `emp` VALUES('7934','张三丰','人事长','7782','1997-06-23','1300',NULL,'10');
CREATE TABLE salgrade(
grade INT (10) COMMENT '工资等级',
losal INT (10) COMMENT '最低限额',
hisal INT (10) COMMENT '最高限额'
)INSERT INTO salgrade (grade, losal, hisal)VALUES (1, 700, 1200);
INSERT INTO salgrade (grade, losal, hisal)VALUES (2, 1201, 1400);
INSERT INTO salgrade (grade, losal, hisal)VALUES (3, 1401, 2000);
INSERT INTO salgrade (grade, losal, hisal)VALUES (4, 2001, 3000);
INSERT INTO salgrade (grade, losal, hisal)VALUES (5, 3001, 9999);
单表查询
1.选择部门30中的所有员工2.列出所有办事员的姓名,编号和部门编号
3.找出佣金高于薪金的员工
4.找出没有佣金的员工
5.找出佣金高于薪金的60%的员工
6.找出部门10中所有人事部长和部门20中所有办事员的详细资料
7.找出收取佣金的员工
8.找出不收取佣金或收取的佣金低于100的员工
9.找出姓张的员工的信息
10.显示员工的姓名和受雇日期,新的员工排在最前面
11.按工作的降序排序,若工作相同则按薪金排序
12.查出不姓刘的员工的信息
13.取出姓李的员工
多表查询
1.查询出每一位雇员的编号、姓名、职位、部门名称、位置
2.要求查询出每一位雇员的姓名、职位、领导的姓名
3.查询出每个雇员的编号、姓名、基本工资、职位、领导的姓名、部门名称及位置
4.列出在部门"销售部"工作的员工的姓名,假定不知道销售部的部门编号
5.列出与"李兴"从事相同工作的所有员工。
6.列出薪金高于在部门30工作的所有员工的薪金的员工姓名和薪金
7.列出受雇日期早于其直接上级的所有员工。
分组函数查询
1.按照部门编号分组,求出每个部的人数,平均工资2.按照职位分组,求出每个职位的最高和最低工资
3.计算出每个各职位的平均工资
4.查询出每个部门的名称、部门的人数、平均工资
left outer join on
5.要求统计出每个部门的详细信息,并且要求这些部门的平均工资高于2000
6.要求查询出工资比华仔还要高的全部雇员信息
7.要求查询出高于公司平均工资的全部雇员信息
8.查询出每个部门的编号、名称、位置、部门人数、平均工资
9.列出至少有一个员工的所有部门
等级查询
1.查询每个工资等级各有多少员工2.查询部门中(所有人)的平均工资等级
①创建dept表并插入数据
②创建emp表并插入数据
③创建salgrade表并插入数据
①命令
mysql> select ename from emp where deptno = 30;
②结果
①命令
mysql> select ename,empno,deptno from emp where job = '办事员';
②结果
①命令
mysql> select ename from emp where comm > sal;
②结果
①命令
mysql> select ename from emp where comm is NULL;
②结果
①命令
mysql> select ename from emp where comm > 0.6*(sal);
②结果
①命令
mysql> select e.* from emp e where deptno = 10 and job = '人事部长' or deptno = 20 and job = '办事员';
②结果
①命令
mysql> select ename from emp where comm is not NULL;
②结果
①命令
mysql> select ename from emp where comm is NULL or comm < 100;
②结果
①命令
mysql> select * from emp where ename like '张%';
②结果
①命令
mysql> select ename,hiredate from emp where hiredate order by hiredate desc;
②结果
①命令
mysql> select *
-> from emp
-> order by job desc, sal asc;
②结果
①命令
mysql> select * from emp where ename not like '刘%';
②结果
①命令
mysql> select ename from emp where ename like '李%';
②结果
①命令
mysql> select e.empno,e.ename,e.job,d.dname,d.loc from emp e,dept d where e.deptno=d.deptno;
②结果
①命令
mysql> select e.ename as '员工姓名',e.job as '职位',l.ename as '领导姓名'
-> from emp e
-> left join emp l on e.mgr = l.empno;
②结果
①命令
mysql> select e.empno as 雇员编号, e.ename as 雇员姓名, e.sal as 基本工资, e.job as 职位, l.ename as 领导姓名, d.dname as 部门名称, d.loc as 位置
-> from emp e
-> left join dept d on e.deptno = d.deptno
-> left join emp l on e.mgr = l.empno;
②结果
①命令
mysql> select e.ename,d.dname from emp e inner join dept d where d.dname = '销售部';
②结果
①命令
#方法一
mysql> select * from emp where job = '售货员';
#方法二
mysql> select * from emp where job = (select job from emp where ename = '李兴');
②结果
①命令
mysql> select e.ename,e.sal from emp e where e.sal > (select max(sal) from emp where deptno = 30 );
②结果
①命令
mysql> select e1.empno, e1.ename, e1.hiredate as 'Employee Hire Date', e2.ename as 'Manager Name', e2.hiredate as 'Manager Hire Date'
-> from emp e1
-> left join emp e2 on e1.mgr = e2.empno
-> where e1.hiredate < e2.hiredate;
②结果
①命令
mysql> select deptno, count(deptno) as 人数, avg(sal) as 平均工资
-> from emp
-> group by deptno;
②结果
①命令
mysql> select job as 职位,max(sal) as 最高工资,min(sal) as 最低工资
-> from emp
-> group by job;
②结果
①命令
mysql> select job, count(*) as 人数, avg(sal) as 平均工资
-> from emp
-> group by job;
②结果
①命令
mysql> select d.dname, count(e.empno) as 人数, avg(e.sal) as 平均工资
-> from dept d
-> left join emp e on d.deptno = e.deptno
-> group by d.dname;
②结果
①命令
mysql> select d.dname, d.loc, avg(e.sal) as 平均工资
-> from dept d
-> left join emp e on d.deptno = e.deptno
-> group by d.dname, d.loc
-> having 平均工资 > 2000;
②结果
①命令
mysql> select *
-> from emp
-> where sal > (
-> select sal
-> from emp e
-> where e.ename = '华仔'
-> );
②结果
①命令
mysql> select * from emp where sal > (select avg(sal) from emp);
②结果
①命令
mysql> select d.deptno, d.dname, d.loc, COUNT(*) AS department_count, avg(e.sal) AS average_salary
-> from dept d
-> join emp e ON d.deptno = e.deptno
-> group by d.deptno, d.dname, d.loc;
②结果
①命令
mysql> select d.deptno, d.dname
-> from dept d
-> left join emp e on d.deptno = e.deptno
-> group by d.deptno, d.dname
-> having count(e.empno) > 0;
②结果
①命令
mysql> select grade, count(*) as num_employees
-> from emp
-> left join salgrade on emp.sal between salgrade.losal and salgrade.hisal
-> group by grade;
②结果
①命令
mysql> select deptno, avg(grade) as avg_grade
-> from emp
-> left join salgrade on emp.sal between salgrade.losal and salgrade.hisal
-> group by deptno;
②结果