SELECT dname
FROM dept
WHERE deptno=(SELECT deptno
FROM emp
WHERE ename='SMITH');
![](https://velog.velcdn.com/images/smsee3/post/15cee682-b458-4583-a9e5-c8bdf7dde826/image.png)
SELECT ename, sal
FROM emp
WHERE sal > (SELECT AVG(sal)
FROM emp);
SELECT ename, sal
FROM emp
WHERE sal >=(SELECT sal
FROM emp
WHERE ename='ALLEN')
AND ename!= 'ALLEN';
SELECT E.ename,E.deptno "부서번호"
FROM emp E
WHERE E.deptno=(SELECT D.deptno
FROM dept D
WHERE D.loc='DALLAS');
SELECT E.ename,E.sal
FROM emp E
WHERE e.deptno=(SELECT D.deptno
FROM dept D
WHERE DNAME='SALES');
SELECT E.ename, E.sal
FROM emp E
WHERE MGR=(SELECT E.empno
FROM emp E
WHERE ename='KING');
SELECT E.ename
FROM emp E
WHERE E.sal =(SElECT MAX(sal)
FROM emp);
SELECT deptno, MIN(sal)
FROM emp
GROUP BY deptno
HAVING MIN(sal) > (SELECT MIN(sal)
FROM emp
WHERE deptno=20);
서브쿼리에서 반환되는 결과가 하나 이상일 때 사용하는 서브쿼리 입니다
다중행 서브쿼리는 반드시 다중행 연산자와 함께 사용해야 합니다.
IN, ANY, ALL, EXISTS
메인 쿼리의 비교 조건이 서브쿼리 결과 중에서 하나라도 일치하면 참 입니다
SELECT ename, sal, deptno
FROM emp
WHERE deptno IN (SELECT DISTINCT deptno
FROM emp
WHERE sal>=3000);
ANY : 검색 결과에 대해서 하나라도 크면 참
< ANY : 검색 결과에 대해서 하나라도 작으면 참
SELECT empno,ename sal,deptno
FROM emp
WHERE sal > ANY(SELECT sal
FROM emp
WHERE deptno=30);
![](https://velog.velcdn.com/images/smsee3/post/1379b890-e300-445a-b2b7-5b2ba5a343a1/image.png)
SELECT ename, sal
FROM emp
WHERE sal > All(SELECT sal
FROM emp
WHERE deptno=30);
SELECT ename, deptno, sal
FROM emp E
WHERE EXISTS (SELECT 1
FROM dept D
WHERE D.deptno=D.
deptno
AND E.deptno IN (10,20));
SELECT empno,ename, sal,deptno
FROM emp
WHERE sal IN (SELECT MAX(sal)
FROM emp
GROUP BY deptno);
SELECT E.deptno, D.DNAME, D.loc
FROM emp E, dept D
WHERE D.deptno IN(SELECT E.deptno
FROM emp E
WHERE job = 'MANAGER');
SELECT ename, sal, job
FROM emp
WHERE sal > ANY(SELECT MIN(sal)
FROM emp
WHERE job = 'SALESMAN');
SELECT ename, sal, job
FROM emp
WHERE sal > ALL(SELECT MAX(sal)
FROM emp
WHERE job = 'SALESMAN');
SELECT empno, ename, job, hiredate, sal
FROM emp E
WHERE EXISTS(SElECT *
FROM emp
WHERE E.empno=mgr)
ORDER BY empno ;