select t1.ename,t1.deptno,t1.sal,t2.myavg from emp t1,(select deptno,avg(sal) myavg from emp group by deptno) t2 where t1.deptno=t2.deptno and t1.ssal > t2.myavg;
+-------+--------+---------+-------------+| ename | deptno | sal | myavg |+-------+--------+---------+-------------+| ALLEN | 30 | 1600.00 | 1566.666667 || JONES | 20 | 2975.00 | 2175.000000 || BLAKE | 30 | 2850.00 | 1566.666667 || SCOTT | 20 | 3000.00 | 2175.000000 || KING | 10 | 5000.00 | 2916.666667 || FORD | 20 | 3000.00 | 2175.000000 |+-------+--------+---------+-------------+6 rows in set (0.00 sec)
复制代码
查找每个部门工资最高的人的姓名、工资、部门、最高工资
答案:
select t1.ename,t1.sal,t1.deptno,t2.mymax from emp t1,(select deptno, max(sal) mymax from emp group by deptno) t2 where t1.deptno=t2.deptno and t1..sal=t2.mymax;
复制代码
步骤:
// 1.得到分组之后的部门号和最高工资mysql> select deptno, max(sal) from emp group by deptno;+--------+----------+| deptno | max(sal) |+--------+----------+| 10 | 5000.00 || 20 | 3000.00 || 30 | 2850.00 |+--------+----------+3 rows in set (0.01 sec)// 2.与emp表进行笛卡尔积并进行t1.sal=t2.mymax的筛选(工资等于最高工资)mysql> select * from emp t1,(select deptno, max(sal) mymax from emp group by deptno) t2 where t1.deptno=t2.deptno and t1.sal=t2.mymax;+--------+-------+-----------+------+---------------------+---------+------+--------+--------+---------+| empno | ename | job | mgr | hiredate | sal | comm | deptno | deptno | mymax |+--------+-------+-----------+------+---------------------+---------+------+--------+--------+---------+| 007698 | BLAKE | MANAGER | 7839 | 1981-05-01 00:00:00 | 2850.00 | NULL | 30 | 30 | 2850.00 || 007788 | SCOTT | ANALYST | 7566 | 1987-04-19 00:00:00 | 3000.00 | NULL | 20 | 20 | 3000.00 || 007839 | KING | PRESIDENT | NULL | 1981-11-17 00:00:00 | 5000.00 | NULL | 10 | 10 | 5000.00 || 007902 | FORD | ANALYST | 7566 | 1981-12-03 00:00:00 | 3000.00 | NULL | 20 | 20 | 3000.00 |+--------+-------+-----------+------+---------------------+---------+------+--------+--------+---------+4 rows in set (0.00 sec)// 3.根据题目要求选择必要筛选的内容mysql> select t1.ename,t1.sal,t1.deptno,t2.mymax from emp t1,(select deptno, max(sal) mymax from emp group by deptno) t2 where t1.deptno=t2.deptno and t1..sal=t2.mymax;
+-------+---------+--------+---------+| ename | sal | deptno | mymax |+-------+---------+--------+---------+| BLAKE | 2850.00 | 30 | 2850.00 || SCOTT | 3000.00 | 20 | 3000.00 || KING | 5000.00 | 10 | 5000.00 || FORD | 3000.00 | 20 | 3000.00 |+-------+---------+--------+---------+4 rows in set (0.00 sec
复制代码
显示每个部门的信息(部门名,编号,地点)和职员数量
答案:
select t1.deptno,t1.dname,t1.loc,t2.num from dept t1,(select deptno,count(*) num from emp group by deptno) t2 where t1.deptno=t2.deptno;
复制代码
步骤:
// 1.分组得到每一组的人数mysql> select deptno,count(*) num from emp group by deptno;+--------+-----+| deptno | num |+--------+-----+| 10 | 3 || 20 | 5 || 30 | 6 |+--------+-----+3 rows in set (0.00 sec)// 2.和部门表进行笛卡尔积,然后进行条件筛选mysql> select * from dept t1,(select deptno,count(*) num from emp group by deptno) t2 where t1.deptno=t2.deptno;+--------+------------+----------+--------+-----+| deptno | dname | loc | deptno | num |+--------+------------+----------+--------+-----+| 10 | ACCOUNTING | NEW YORK | 10 | 3 || 20 | RESEARCH | DALLAS | 20 | 5 || 30 | SALES | CHICAGO | 30 | 6 |+--------+------------+----------+--------+-----+3 rows in set (0.01 sec)mysql> select t1.deptno,t1.dname,t1.loc,t2.num from dept t1,(select deptno,count(*) num from emp group by deptno) t2 where t1.deptno=t2.deptno;
+--------+------------+----------+-----+| deptno | dname | loc | num |+--------+------------+----------+-----+| 10 | ACCOUNTING | NEW YORK | 3 || 20 | RESEARCH | DALLAS | 5 || 30 | SALES | CHICAGO | 6 |+--------+------------+----------+-----+3 rows in set (0.00 sec)
复制代码
暴力解法:
mysql> select dept.dname,dept.deptno,dept.loc,count(*) from emp,dept where emp.deptno=dept.deptno group by dept.deptno,dept.dname,dept.loc;