์ฟผ๋ฆฌ ์์ ์ฟผ๋ฆฌ๊ฐ ์กด์ฌํ๋ ๊ฒ!
SELECT ์กฐํํ ์ปฌ๋ผ๋ช
, (SELECT ์ปฌ๋ผ๋ช
FROM ํ
์ด๋ธ๋ช
WHERE ์กฐ๊ฑด) --์ค์นผ๋ผ ์๋ธ์ฟผ๋ฆฌ
FROM ํ
์ด๋ธ๋ช
, (SELECT ์ปฌ๋ผ๋ช
FROM ํ
์ด๋ธ๋ช
WHERE ์กฐ๊ฑด) --์ธ๋ผ์ธ ๋ทฐ
WHERE ์กฐ๊ฑด AND/OR (SELECT ์ปฌ๋ผ๋ช
FROM ํ
์ด๋ธ๋ช
WHERE ์กฐ๊ฑด) --์๋ธ์ฟผ๋ฆฌ
--JOIN ์ด์ฉ
SELECT *
FROM emp e, dept d
WHERE e.deptno = d.deptno
AND d.dname = 'SALES';
--์๋ธ์ฟผ๋ฆฌ ์ด์ฉ
SELECT *
FROM emp
--WHERE deptno = 30;
WHERE deptno = (SELECT deptno
FROM dept
WHERE dname = 'SALES');
๐ป
SELECT *
FROM emp
--WHERE sal < 1600;
WHERE sal < (SELECT sal
FROM emp
WHERE ename = 'ALLEN'); --์ด๋ฆ์ด ALLEN
๐ป
SELECT p.name, p.hiredate, d.dname
FROM professor p, department d
WHERE hiredate > (SELECT hiredate
FROM professor
WHERE name = 'Meg Ryan') --85/09/18
AND p.deptno = d.deptno
ORDER BY hiredate;
==> ์ด๋ฆ์ด 'Meg Ryan' ์ธ ๊ต์๋ฅผ ๋จผ์ ์ฐพ๊ณ (์๋ธ์ฟผ๋ฆฌ)
==> ๊ทธ ๊ต์๋ณด๋ค ๋์ค์ ์
์ฌํ ๋ ์ง๋ฅผ ์ฐพ๊ณ
==> ํ๊ณผ๋ช
๋ ๋์์ผํ๋๊น ๋์ ์กฐ์ธ!
๐ป
SELECT name, weight
FROM student
WHERE weight > (SELECT AVG(weight)
FROM student
WHERE deptno1 = 201) --67
ORDER BY weight;
==> 201๋ฒ ํ๊ณผ์ ํ๊ท ๋ชธ๋ฌด๊ฒ๋ฅผ ๋จผ์ ์ฐพ๊ณ (์๋ธ์ฟผ๋ฆฌ)
==> ๊ทธ ํ๊ท ๋ชธ๋ฌด๊ฒ๋ณด๋ค ํฐ ๋ชธ๋ฌด๊ฒ๋ฅผ ์ฐพ๊ณ
๐ป
SELECT empno, name, deptno
FROM emp2
WHERE deptno IN (SELECT dcode
FROM dept2
WHERE area = 'Pohang Main Office'); --์ง์ญ์ด ํฌํญ์ธ dcode (0001,1003,1006,1007)
==> ์๋ธ์ฟผ๋ฆฌ์ ๋ค์คํ์ด ๋์ฌ๋๋ IN์ ์จ์ค์ ๋์ฌ ์ ์๊ฒ ํ๋ค!!!
๐ป
SELECT deptno, MIN(hiredate) --๊ฐ์ฅ ๋จผ์ ์
์ฌ
FROM professor
GROUP BY deptno; --๊ฐ ํ๊ณผ๋ณ๋ก
------------------
SELECT p.profno, p.name, d.dname, p.hiredate
FROM professor p, department d
WHERE p.deptno = d.deptno
AND (p.deptno, p.hiredate) IN(SELECT deptno, MIN(hiredate) --๊ฐ์ฅ ๋จผ์ ์
์ฌ
FROM professor
GROUP BY deptno)
ORDER BY hiredate;
==> ๊ฐ ํ๊ณผ๋ณ๋ก, ๊ฐ์ฅ ๋จผ์ ์
์ฌ๋ฅผ ๋จผ์ ์ฐพ๊ณ ! (์๋ธ์ฟผ๋ฆฌ)
==> ํ๊ณผ๋ช
์ ์จ์ค์ผ ํ๊ธฐ ๋๋ฌธ์ department ํ
์ด๋ธ๊ณผ ์กฐ์ธ์์ผ์ฃผ๊ณ ๊ฒน์น๋ ์ปฌ๋ผ ์ฐพ๊ณ !
==> ์๋ธ์ฟผ๋ฆฌ์์ ์ํ๋ ์กฐ๊ฑด ๋๊ฐ์ง deptno, hiredate ๊ฐ ๋ค ๋์์ผํ๊ธฐ ๋๋ฌธ์ IN ์์๋ ์กฐ๊ฑด ๋๊ฐ์ง ๋ค ๋ช
์ํด์ค์ผ ํ๋ค!
๐ป