1. 哪些部門的人數比20 號部門的人數多。
SELECT DEPTNO,COUNT(*) FROM EMP
GROUP BY DEPTNO
HAVING COUNT(*) >
(SELECT COUNT(*) FROM EMP
WHERE DEPTNO = 20
);
2. SMITH的領導是誰(非關聯子查詢)。
SELECT ENAME
FROM EMP
WHERE EMPNO =
(SELECT MGR FROM EMP
WHERE ENAME='SMITH'
);
3.FORD 領導誰(非關聯子查詢)。
SELECT ENAME
FROM EMP
WHERE MGR IN
(SELECT EMPNO FROM EMP
WHERE ENAME='FORD'
);
4. SMITH 的領導是誰(關聯子查詢)。
SELECT ENAME
FROM EMP E1
WHERE EXISTS (
SELECT 1 FROM EMP E2
WHERE ENAME='SMITH'
AND E2.MGR = E1.EMPNO);
5. FORD 領導誰(關聯子查詢)。
SELECT ENAME
FROM EMP E1
WHERE EXISTS (
SELECT 1 FROM EMP E2
WHERE ENAME='FORD'
AND E2.EMPNO = E1.MGR);
6. 列出在同一部門共事,入職日期晚但工資高於其他同事的員工:名字、工資、入職日期
(關聯子查詢)。
SELECT ename 姓名,
SAL AS 工資,HIREDATE AS 入職日期
FROM EMP E1
WHERE EXISTS (
SELECT 1 FROM EMP E2
WHERE E2.DEPTNO = E1.DEPTNO
AND E1.HIREDATE > E2.HIREDATE
AND E1.SAL > E2.SAL
);
7. 哪些員工跟SMITH不在同一個部門(非關聯子查詢)。
SELECT ENAME
FROM EMP
WHERE DEPTNO <>
(SELECT DEPTNO FROM EMP
WHERE ENAME='SMITH'
);
8. 哪些員工跟SMITH不在同一個部門(關聯子查詢)。
SELECT ENAME
FROM EMP E1
WHERE NOT EXISTS (
SELECT 1 FROM EMP E2
WHERE E1.DEPTNO = E2.DEPTNO
AND E2.ENAME='SMITH'
);
9. RESEARCH部門有哪些職位(非關聯子查詢)。
SELECT DISTINCT JOB FROM EMP
WHERE DEPTNO = (
SELECT DEPTNO FROM DEPT
WHERE DNAME = ' RESEARCH');
10. RESEARCH部門有哪些職位(關聯子查詢)。
SELECT DISTINCT JOB FROM EMP
WHERE EXISTS(
SELECT 1 FROM DEPT
WHERE EMP.DEPTNO = DEPT.DEPTNO
AND DEPT.DNAME = ' RESEARCH');