oracle 查询

雇员emp表记录:
   	EMPNO	ENAME	JOB	MGR	HIREDATE	SAL	COMM	DEPTNO
14	7934	MILLER	CLERK	7782	1982-1-23	1300.00		10
7	7782	CLARK	MANAGER	7839	1981-6-9	2450.00		10
9	7839	KING	PRESIDENT		1981-11-17	5000.00		10
1	7369	SMITH	CLERK	7902	1980-12-17	800.00		20
11	7876	ADAMS	CLERK	7788	1987-5-23	1100.00		20
4	7566	JONES	MANAGER	7839	1981-4-2	2975.00		20
13	7902	FORD	ANALYST	7566	1981-12-3	3000.00		20
8	7788	SCOTT	ANALYST	7566	1987-4-19	3000.00		20
12	7900	JAMES	CLERK	7698	1981-12-3	950.00		30
5	7654	MARTIN	SALESMAN	7698	1981-9-28	1250.00	1400.00	30
3	7521	WARD	SALESMAN	7698	1981-2-22	1250.00	500.00	30
10	7844	TURNER	SALESMAN	7698	1981-9-8	1500.00	0.00	30
2	7499	ALLEN	SALESMAN	7698	1981-2-20	1600.00	300.00	30
6	7698	BLAKE	MANAGER	7839	1981-5-1	2850.00		30


部门dept表记录:
   	DEPTNO	DNAME	LOC
1	10	ACCOUNTING	NEW YORK
2	20	RESEARCH	DALLAS
3	30	SALES	CHICAGO
4	40	OPERATIONS	BOSTON


要求:即要查出部门信息全部记录而且还要查出部门在雇员表中工资最高的人员信息
例如:
   DEPTNO DNAME LOC EMPNO ENAME JOB MGR HIREDATE SAL COMM
10 ACCOUNTING NEW YORK 7839 KING PRESIDENT 1981-11-17 5000.00
SQL语句:
select t.*, c.* from dept t left join (select d.* from emp d,(select x.deptno, max(x.sal) sal from emp x group by x.deptno) e  where d.sal = e.sal) c on t.deptno = c.deptno


查询结果:
   	DEPTNO	DNAME	LOC	EMPNO	ENAME	JOB	MGR	HIREDATE	SAL	COMM	DEPTNO
1	10	ACCOUNTING	NEW YORK	7839	KING	PRESIDENT		1981-11-17	5000.00		10
2	20	RESEARCH	DALLAS	7902	FORD	ANALYST	7566	1981-12-3	3000.00		20
3	20	RESEARCH	DALLAS	7788	SCOTT	ANALYST	7566	1987-4-19	3000.00		20
4	30	SALES	CHICAGO	7698	BLAKE	MANAGER	7839	1981-5-1	2850.00		30
5	40	OPERATIONS	BOSTON								


不知道SQL写的对不对,是不是有其他更简单、更好的写法?

你可能感兴趣的:(oracle,sql,C++,c,C#)