1,建表语句
DROP TABLE IF EXISTS `dept`;
CREATE TABLE `dept` (
`DEPTNO` int(2) NOT NULL COMMENT '部门编号',
`DNAME` varchar(14) DEFAULT NULL COMMENT '部门名称',
`LOC` varchar(13) DEFAULT NULL COMMENT '位置',
PRIMARY KEY (`DEPTNO`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='部门信息表';
2,数据插入语句
INSERT INTO `dept` VALUES ('10', 'ACCOUNTING', 'NEW YORK');
INSERT INTO `dept` VALUES ('20', 'RESEARCH', 'DALLAS');
INSERT INTO `dept` VALUES ('30', 'SALES', 'CHICAGO');
INSERT INTO `dept` VALUES ('40', 'OPERATIONS', 'BOSTON');
1,建表语句
DROP TABLE IF EXISTS `emp`;
CREATE TABLE `emp` (
`EMPNO` int(4) NOT NULL COMMENT '员工编号',
`ENAME` varchar(10) DEFAULT NULL COMMENT '员工姓名',
`JOB` varchar(9) DEFAULT NULL COMMENT '工作岗位',
`MGR` int(4) DEFAULT NULL COMMENT '上级经理',
`HIREDATE` date DEFAULT NULL,
`SAL` double(7,2) DEFAULT NULL,
`COMM` double(7,2) DEFAULT NULL,
`DEPTNO` int(2) DEFAULT NULL,
PRIMARY KEY (`EMPNO`),
KEY `DEPTNO` (`DEPTNO`),
KEY `SAL` (`SAL`),
CONSTRAINT `emp_ibfk_1` FOREIGN KEY (`DEPTNO`) REFERENCES `dept` (`DEPTNO`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='员工编号';
2,数据插入语句
INSERT INTO `emp` VALUES ('7369', 'SMITH', 'CLERK', '7902', '1980-12-17', '800.00', null, '20');
INSERT INTO `emp` VALUES ('7499', 'ALLEN', 'SALESMAN', '7698', '1981-02-20', '1600.00', '300.00', '30');
INSERT INTO `emp` VALUES ('7521', 'WARD', 'SALESMAN', '7698', '1981-02-22', '1250.00', '500.00', '30');
INSERT INTO `emp` VALUES ('7566', 'JONES', 'MANAGER', '7839', '1981-04-02', '2975.00', null, '20');
INSERT INTO `emp` VALUES ('7654', 'MARTIN', 'SALESMAN', '7698', '1981-09-28', '1250.00', '1400.00', '30');
INSERT INTO `emp` VALUES ('7698', 'BLAKE', 'MANAGER', '7839', '1981-05-01', '2850.00', null, '30');
INSERT INTO `emp` VALUES ('7782', 'CLARK', 'MANAGER', '7839', '1981-06-09', '2450.00', null, '10');
INSERT INTO `emp` VALUES ('7788', 'SCOTT', 'ANALYST', '7566', '1987-04-19', '3000.00', null, '20');
INSERT INTO `emp` VALUES ('7839', 'KING', 'PRESIDENT', null, '1981-11-17', '5000.00', null, '10');
INSERT INTO `emp` VALUES ('7844', 'TURNER', 'SALESMAN', '7698', '1981-09-08', '1500.00', '0.00', '30');
INSERT INTO `emp` VALUES ('7876', 'ADAMS', 'CLERK', '7788', '1981-05-23', '1100.00', null, '20');
INSERT INTO `emp` VALUES ('7900', 'JAMES', 'CLERK', '7698', '1981-12-03', '950.00', null, '30');
INSERT INTO `emp` VALUES ('7902', 'FORD', 'ANALYST', '7566', '1981-12-03', '3000.00', null, '20');
INSERT INTO `emp` VALUES ('7934', 'MILLER', 'CLERK', '7782', '1982-01-23', '1300.00', null, '10');
1,建表语句
DROP TABLE IF EXISTS `salgrade`;
CREATE TABLE `salgrade` (
`GRADE` int(11) DEFAULT NULL,
`LOSAL` int(11) DEFAULT NULL,
`HISAL` int(11) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
2,数据插入语句
INSERT INTO `salgrade` VALUES ('1', '700', '1200');
INSERT INTO `salgrade` VALUES ('2', '1201', '1400');
INSERT INTO `salgrade` VALUES ('3', '1401', '2000');
INSERT INTO `salgrade` VALUES ('4', '2001', '3000');
INSERT INTO `salgrade` VALUES ('5', '3001', '9999');
mysql> select
-> deptno, max(sal) maxsal
-> from
-> emp
-> group by
-> deptno;
+--------+---------+
| deptno | maxsal |
+--------+---------+
| 10 | 5000.00 |
| 20 | 3000.00 |
| 30 | 2850.00 |
+--------+---------+
第二步:把上面获得的结果作为临时表t,然后与emp表进行内连接中的等值连接,连接条件为emp表的deptno,sal和t中的deptno,maxsal相等。
mysql> select
-> e.deptno, e.ename, e.sal, t.maxsal
-> from
-> emp e
-> join
-> (select deptno, max(sal) maxsal from emp group by deptno) t
-> on
-> e.deptno = t.deptno
-> and
-> e.sal = t.maxsal;
+--------+-------+---------+---------+
| deptno | ename | sal | maxsal |
+--------+-------+---------+---------+
| 10 | KING | 5000.00 | 5000.00 |
| 20 | SCOTT | 3000.00 | 3000.00 |
| 20 | FORD | 3000.00 | 3000.00 |
| 30 | BLAKE | 2850.00 | 2850.00 |
+--------+-------+---------+---------+
2,哪些人的薪水在部门的平均薪水之上。mysql> select
-> deptno, avg(sal) as avgsal
-> from
-> emp
-> group by
-> deptno;
+--------+-------------+
| deptno | avgsal |
+--------+-------------+
| 10 | 2916.666667 |
| 20 | 2175.000000 |
| 30 | 1566.666667 |
+--------+-------------+
第二步:
把上面获得的结果作为临时表t,与emp表进行内连接中的等值连接,
连接的条件为emp中的deptno和t中的deptno相等,并且emp中的sal大于t中的avgsal。
mysql> select
-> e.deptno, e.ename, e.sal, t.avgsal
-> from
-> emp e
-> join
-> (select deptno, avg(sal) as avgsal from emp group by deptno) t
-> on
-> e.deptno = t.deptno
-> and
-> e.sal > t.avgsal;
+--------+-------+---------+-------------+
| deptno | ename | sal | avgsal |
+--------+-------+---------+-------------+
| 10 | KING | 5000.00 | 2916.666667 |
| 20 | JONES | 2975.00 | 2175.000000 |
| 20 | SCOTT | 3000.00 | 2175.000000 |
| 20 | FORD | 3000.00 | 2175.000000 |
| 30 | ALLEN | 1600.00 | 1566.666667 |
| 30 | BLAKE | 2850.00 | 1566.666667 |
+--------+-------+---------+-------------+
3,3.1取得部门(所有人的)平均薪水的等级。
mysql> select
-> deptno, avg(sal) as avgsal
-> from
-> emp
-> group by
-> deptno;
+--------+-------------+
| deptno | avgsal |
+--------+-------------+
| 10 | 2916.666667 |
| 20 | 2175.000000 |
| 30 | 1566.666667 |
+--------+-------------+
第二步:把上面的结果作为临时表t与salgrade表进行内连接中的非等值连接,条件为临时表中的avgsal在salgrade表中losal和hisal之间。
mysql> select
-> t.deptno, s.grade
-> from
-> salgrade s
-> join
-> (select deptno, avg(sal) as avgsal from emp group by deptno) t
-> on
-> t.avgsal between s.losal and hisal;
+--------+-------+
| deptno | grade |
+--------+-------+
| 30 | 3 |
| 10 | 4 |
| 20 | 4 |
+--------+-------+
3.2取得每个部门中所有人的薪水等级的平均等级。
mysql> select
-> e.deptno, e.ename, s.grade
-> from
-> emp e
-> join
-> salgrade s
-> on
-> e.sal between s.losal and hisal;
+--------+--------+-------+
| deptno | ename | grade |
+--------+--------+-------+
| 20 | SMITH | 1 |
| 30 | JAMES | 1 |
| 20 | ADAMS | 1 |
| 30 | WARD | 2 |
| 30 | MARTIN | 2 |
| 10 | MILLER | 2 |
| 30 | TURNER | 3 |
| 30 | ALLEN | 3 |
| 20 | JONES | 4 |
| 30 | BLAKE | 4 |
| 10 | CLARK | 4 |
| 20 | SCOTT | 4 |
| 20 | FORD | 4 |
| 10 | KING | 5 |
+--------+--------+-------+
第二步:将上面的结果作为临时表t,将deptno作为分组字段,用avg对grade求平均值。
mysql> select
-> t.deptno, avg(t.grade) as avggrade
-> from
-> (select e.deptno, e.ename, s.grade from emp e join salgrade s on e.sal between s.losal and hisal) t
-> group by
-> t.deptno;
+--------+----------+
| deptno | avggrade |
+--------+----------+
| 10 | 3.6667 |
| 20 | 2.8000 |
| 30 | 2.5000 |
+--------+----------+
4,不准用分组函数(Max),取得最高薪水(给出两种解决方案)。
mysql> select
-> sal
-> from
-> emp
-> order by
-> sal desc
-> limit
-> 1;
+---------+
| sal |
+---------+
| 5000.00 |
+---------+
第二中方法:
mysql> select
-> sal
-> from
-> emp;
+---------+
| sal |
+---------+
| 800.00 |
| 950.00 |
| 1100.00 |
| 1250.00 |
| 1250.00 |
| 1300.00 |
| 1500.00 |
| 1600.00 |
| 2450.00 |
| 2850.00 |
| 2975.00 |
| 3000.00 |
| 3000.00 |
| 5000.00 |
+---------+
第二步:把上面的结果作为临时表t,再把这张临时表t看成两张表,然后作内连接中的非等值连接,条件为前表中的sal小于后表中的sal,这样作为最大值的5000就被过滤掉了;
mysql> select
-> distinct t.sal as sal
-> from
-> (select sal from emp) t
-> join
-> (select sal from emp) t2
-> on
-> t.sal < t2.sal;
+---------+
| sal |
+---------+
| 800.00 |
| 950.00 |
| 1100.00 |
| 1250.00 |
| 1300.00 |
| 1500.00 |
| 1600.00 |
| 2450.00 |
| 2850.00 |
| 2975.00 |
| 3000.00 |
+---------+
第三步:选择薪水不在上面表中的薪水即为最高薪水。
mysql> select
-> sal
-> from
-> emp
-> where
-> sal not in (select distinct t.sal as sal from (select sal from emp) t join (select sal from emp) t2 on t
.sal < t2.sal);
+---------+
| sal |
+---------+
| 5000.00 |
+---------+