day6

์ƒ์€๐Ÿ‘ธยท2023๋…„ 12์›” 5์ผ
0

๋šœ๋ฒ…๋šœ๋ฒ… ์„ธ๋ฒˆ์งธ

๋ชฉ๋ก ๋ณด๊ธฐ
6/11
post-thumbnail

๐Ÿ“– ์„œ๋ธŒ์ฟผ๋ฆฌ

์ฟผ๋ฆฌ ์•ˆ์— ์ฟผ๋ฆฌ๊ฐ€ ์กด์žฌํ•˜๋Š” ๊ฒƒ!

SELECT ์กฐํšŒํ•  ์ปฌ๋Ÿผ๋ช…, (SELECT ์ปฌ๋Ÿผ๋ช… FROM ํ…Œ์ด๋ธ”๋ช… WHERE ์กฐ๊ฑด) --์Šค์นผ๋ผ ์„œ๋ธŒ์ฟผ๋ฆฌ
FROM ํ…Œ์ด๋ธ”๋ช…, (SELECT ์ปฌ๋Ÿผ๋ช… FROM ํ…Œ์ด๋ธ”๋ช… WHERE ์กฐ๊ฑด) --์ธ๋ผ์ธ ๋ทฐ
WHERE ์กฐ๊ฑด AND/OR (SELECT ์ปฌ๋Ÿผ๋ช… FROM ํ…Œ์ด๋ธ”๋ช… WHERE ์กฐ๊ฑด) --์„œ๋ธŒ์ฟผ๋ฆฌ

๐ŸŽˆ์—ฐ์Šต๋ฌธ์ œ --SALES ์ด๋ฆ„์˜ ๋ถ€์„œ์— ๋‹ค๋‹ˆ๋Š” ์ง์›๋“ค์˜ ์ •๋ณด

--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');

๐Ÿ’ป

๐ŸŽˆ์—ฐ์Šต๋ฌธ์ œ --์ด๋ฆ„์ด ALLEN ์ธ ์‚ฌ๋žŒ์˜ ๊ธ‰์—ฌ๋ณด๋‹ค ์ ๊ฒŒ ๋ฐ›๋Š” ์ง์›๋“ค์˜ ์ •๋ณด๋ฅผ ์ถœ๋ ฅํ•˜์„ธ์š”.

SELECT *
FROM emp
--WHERE sal < 1600;
WHERE sal < (SELECT sal
            FROM emp
            WHERE ename = 'ALLEN'); --์ด๋ฆ„์ด ALLEN

๐Ÿ’ป

๐ŸŽˆ์—ฐ์Šต๋ฌธ์ œ --1. professor, department ํ…Œ์ด๋ธ” ํ™œ์šฉ. --์ด๋ฆ„์ด 'Meg Ryan' ์ธ ๊ต์ˆ˜๋ณด๋‹ค ๋‚˜์ค‘์— ์ž…์‚ฌํ•œ ์‚ฌ๋žŒ์˜ ์ด๋ฆ„, ์ž…์‚ฌ์ผ, ํ•™๊ณผ๋ช… ์„ ์ถœ๋ ฅํ•˜์„ธ์š”.

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' ์ธ ๊ต์ˆ˜๋ฅผ ๋จผ์ € ์ฐพ๊ณ  (์„œ๋ธŒ์ฟผ๋ฆฌ)
==> ๊ทธ ๊ต์ˆ˜๋ณด๋‹ค ๋‚˜์ค‘์— ์ž…์‚ฌํ•œ ๋‚ ์งœ๋ฅผ ์ฐพ๊ณ 
==> ํ•™๊ณผ๋ช…๋„ ๋‚˜์™€์•ผํ•˜๋‹ˆ๊นŒ ๋‘˜์„ ์กฐ์ธ!

๐Ÿ’ป

๐ŸŽˆ์—ฐ์Šต๋ฌธ์ œ --2. student ํ…Œ์ด๋ธ”, --1์ „๊ณต์ด 201๋ฒˆ ํ•™๊ณผ์˜ ํ‰๊ท  ๋ชธ๋ฌด๊ฒŒ๋ณด๋‹ค ๋ชธ๋ฌด๊ฒŒ๊ฐ€ ๋งŽ์ด ๋‚˜๊ฐ€๋Š” ํ•™์ƒ๋“ค์˜ ์ด๋ฆ„๊ณผ ๋ชธ๋ฌด๊ฒŒ ์ถœ๋ ฅํ•˜์„ธ์š”.

SELECT name, weight
FROM student 
WHERE weight > (SELECT AVG(weight)
                FROM student
                WHERE deptno1 = 201) --67
ORDER BY weight;

==> 201๋ฒˆ ํ•™๊ณผ์˜ ํ‰๊ท ๋ชธ๋ฌด๊ฒŒ๋ฅผ ๋จผ์ € ์ฐพ๊ณ  (์„œ๋ธŒ์ฟผ๋ฆฌ)
==> ๊ทธ ํ‰๊ท ๋ชธ๋ฌด๊ฒŒ๋ณด๋‹ค ํฐ ๋ชธ๋ฌด๊ฒŒ๋ฅผ ์ฐพ๊ณ 

๐Ÿ’ป

๐ŸŸ ์„œ๋ธŒ์ฟผ๋ฆฌ ์ฟผ๋ฆฌ -> ๊ฒฐ๊ณผ๊ฐ€ ๋‹จ์ผํ–‰, ๋‹ค์ค‘ํ–‰์ธ์ง€ ์ฒดํฌํ•ด์•ผํ•จ! => ๋‹ค์ค‘ํ–‰์„ ์“ฐ๊ณ  ์‹ถ์„๋•Œ๋Š” IN์„ ์จ์ค˜์„œ ์“ธ ์ˆ˜ ์žˆ๊ฒŒ ํ•œ๋‹ค!

๐ŸŽˆ์—ฐ์Šต๋ฌธ์ œ -- ๊ทผ๋ฌด์ง€์—ญ์ด ํฌํ•ญ์ธ ๋ชจ๋“  ์‚ฌ์›๋“ค์˜ ์‚ฌ๋ฒˆ, ์ด๋ฆ„, ๋ถ€์„œ๋ฒˆํ˜ธ ์ถœ๋ ฅํ•˜๊ธฐ!

SELECT empno, name, deptno
FROM emp2
WHERE deptno IN (SELECT dcode
                FROM dept2
                WHERE area = 'Pohang Main Office'); --์ง€์—ญ์ด ํฌํ•ญ์ธ dcode (0001,1003,1006,1007)

==> ์„œ๋ธŒ์ฟผ๋ฆฌ์— ๋‹ค์ค‘ํ–‰์ด ๋‚˜์˜ฌ๋•Œ๋Š” IN์„ ์จ์ค˜์„œ ๋‚˜์˜ฌ ์ˆ˜ ์žˆ๊ฒŒ ํ•œ๋‹ค!!!

๐Ÿ’ป

๐ŸŽˆ์—ฐ์Šต๋ฌธ์ œ --3. professor, department ํ…Œ์ด๋ธ” ํ™œ์šฉ, --๊ฐ ํ•™๊ณผ๋ณ„๋กœ ๊ฐ€์žฅ ๋จผ์ € ์ž…์‚ฌํ•œ ๊ต์ˆ˜์˜ ๊ต์ˆ˜๋ฒˆํ˜ธ์™€ ์ด๋ฆ„, ํ•™๊ณผ๋ช…์„ ์ถœ๋ ฅํ•˜์„ธ์š” --(์ž…์‚ฌ์ผ์ด ์˜›๋‚ ์ธ ์‚ฌ๋žŒ๋ถ€ํ„ฐ ์ •๋ ฌ)

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 ์•ž์—๋„ ์กฐ๊ฑด ๋‘๊ฐ€์ง€ ๋‹ค ๋ช…์‹œํ•ด์ค˜์•ผ ํ•œ๋‹ค!

๐Ÿ’ป

profile
๋’ค์ฃฝ๋ฐ•์ฃฝ ๋ฒจ๋กœ๊ทธ

0๊ฐœ์˜ ๋Œ“๊ธ€