//查询每个部门里面最高工资的人
这里可以用连接查询,思路:
(1)得到每个部门的最高薪水:
select max(sal),deptno from emp group by deptno;
select ename,sal from emp join (select max(sal) as maxsal,deptno from emp g roup by deptno)t on (emp.sal=t.maxsal and emp.deptno=t.deptno);
select ename,sal from emp,(select max(sal) as maxsal,deptno from emp group by deptno)t where emp.deptno=t.deptno and emp.sal=t.maxsal;
select t1.deptno,t1.maxsal,t2.grade from((select deptno,avg(sal) maxsal fro m emp group by deptno)t1 join salgrade t2 on t1.maxsal between t2.losal and t2.h isal);
SELECT T1.ENAME,T2.ENAME FROM EMP T1 JOIN (SELECT ENAME,EMPNO FROM EMP)T2 ON T1.MGR=T2.EMPNO;
select e1.ename,e2.ename from emp e1,emp e2 where e1.mgr=e2.empno;
SELECT T1.ENAME,T2.ENAME FROM EMP T1 LEFT JOIN (SELECT ENAME,EMPNO FROM EMP )T2 ON T1.MGR=T2.EMPNO; SELECT T1.ENAME,T2.ENAME FROM EMP T1 RIGHT JOIN (SELECT ENAME,MGR FROM EMP) T2 ON T2.MGR=T1.EMPNO;
(A)select ename,dname from emp,dept where emp.deptno=dept.deptno;
(B)select ename,dname from emp join dept on(emp.deptno=dept.deptno);
select ename,dname from emp join dept using (deptno);
select ename,sal,grade from emp e join salgrade s on(e.sal between s.losal and s.hisal);
select dname, ename,sal,grade from emp e join dept d on(d.deptno=e.deptno) join salgrade s on(e.sal between s.losal and s.hisal) where sal>2000;
select e1.ename,e2.ename from emp e1 (left) join emp e2 on(e1.mgr = e2.empno);
select dname,ename from emp e right join dept d on(e.deptno=d.deptno);
select dname,ename from emp e full join dept d on(e.deptno=d.deptno);
select deptno,avg(grade) from (select deptno,ename,grade from emp join salg rade s on (emp.sal between s.losal and s.hisal))t group by deptno;
select ename from emp where empno in (select distinct mgr from emp);
select distinct e1.sal from emp e1 join emp e2 on (e1.sal<e2.sal);
select avg(sal) avg_sal,deptno from emp group by deptno;
select max(avg_sal) from (select avg(sal) avg_sal,deptno from emp group by deptno);
select deptno,avg_sal from (select avg(sal) avg_sal,deptno from emp group by deptno) where avg_sal=(select max(avg_sal) from (select avg(sal) avg_sal,deptno from emp group by deptno);