1. 列出至少有一个雇员的所有部门
SELECT DISTINCT dname FROM dept WHERE deptnoIN (SELECT DISTINCT deptno FROM emp)
2. 列出薪金比"SMITH"多的所有雇员
SELECT ename,sal FROM emp WHERE sal >(SELECT sal FROM emp WHERE ename = ‘SMITH’)
3. 列出入职日期早于其直接上级的所有雇员SELECT ename FROM emp e WHERE hiredate <(SELECT hiredate FROM emp WHERE empno= e.mgr)
4. 找员工姓名和直接上级的名字SELECT EMP.ENAME AS 员工姓名,MGR.ENAME AS 经理姓名
FROM EMP,(SELECT ENAME,EMPNO FROM EMP ) MGR
WHERE EMP.MGR = MGR.EMPNO(+)
SELECT DNAME 部门名称 ,cou 人数
FROM DEPT D ,(SELECT DEPTNO,COUNT(EMPNO) COU FROM EMP GROUP BY DEPTNO) E
WHERE D.DEPTNO = E.DEPTNO
SELECT *
FROM EMP
WHERE (DEPTNO,SAL) IN (SELECT DEPTNO,MAX(SAL) FROM EMP GROUP BY DEPTNO)
SELECT ENAME,SAL,DEPTNO
FROM EMP E
WHERE (SELECT COUNT(EMPNO) FROM EMP WHERE DEPTNO = E.DEPTNO AND SAL >E.SAL ) <=1
SELECT last_name, salary FROM employees WHERE department_id=(SELECT department_id FROM employees WHERE employee_id=200 )
9. 显示出和姓名中包含"W"的员工相同部门的员工姓名
FROM employees WHERE department_id IN( SELECT department_id FROM employees WHERE last_name LIKE '%a%' )
10. 显示出工资大于平均工资的员工姓名,工资
SELECT last_name, salary FROM employees WHERE salary>(SELECT AVG(salary) FROM employees )
11. 显示出工资大于本部门平均工资的员工姓名,工资
SELECT last_name, salary FROM employees WHERE salary> ANY(SELECT AVG(salary) FROM employees GROUP BY department_id )
12. 显示员工"KING"所管理的员工姓名
SELECT first_name||last_name NAME, salary, department_id FROM employees WHERE manager_id IN(SELECT employee_id FROM employees WHERE last_name = 'King' )
13. 显示每位经理管理员工的最低工资,及最低工资者的姓名
SELECT emp.employee_id, emp.salary, emp.department_id, emp.manager_id FROM employees emp WHERE NOT EXISTS(SELECT man.salary FROM employees man WHERE man.manager_id=emp.manager_id AND man.salary(SELECT hire_date FROM employees WHERE salary=(SELECT MAX(salary) FROM employees))
14. 显示比工资最高的员工参加工作时间晚的员工姓名,参加工作时间
15. 显示出平均工资最高的的部门平均工资及部门名称
SELECT ROWNUM, department_name, avg_sal FROM( SELECT department_name, AVG(salary) AS avg_sal FROM employees e, departments d WHERE e.department_id=d.department_id GROUP BY department_name ORDER BY avg_sal DESC ) WHERE ROWNUM=1 SELECT avg_sal, department_name FROM ( SELECT AVG(salary) AS avg_sal, department_name FROM departments dep JOIN employees emp ON dep.department_id = emp.department_id GROUP BY department_name ) WHERE avg_sal = ( SELECT MAX(avg_sal) FROM ( SELECT AVG(salary) AS avg_sal, department_name FROM departments dep JOIN employees emp ON dep.department_id = emp.department_id GROUP BY department_name ) )