๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
ORACLE/Tunning

[Oracle] ON์ ˆ๊ณผ WHERE์ ˆ ์กฐ๊ฑด ์ฐจ์ด, JOIN ๋Œ€์ƒ ์ฐจ์ด

by ๐ŸŒปโ™š 2020. 8. 9.

ON์ ˆ๊ณผ WHERE์ ˆ ์ฐจ์ด

์กฐ์ธ๋ฌธ์„ ์‚ฌ์šฉํ• ๋•Œ ON์ ˆ์„ ์ด์šฉํ•ด์„œ ํ•ด๋‹น ์กฐ๊ฑด์œผ๋กœ ํ…Œ์ด๋ธ” ์กฐ์ธ์„ ํ•˜๊ฒŒ๋ฉ๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ JOIN์„ ํ•˜๊ธฐ ์œ„ํ•ด ON์ ˆ์ด ์•„๋‹Œ WHERE์ ˆ์— ์‚ฌ์šฉํ• ๋•Œ๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ฐ™์€ ๋‚ด์šฉ๊ฐ™์ง€๋งŒ ํ™•์‹คํžˆ ๋‘๊ฐ€์ง€ ์ ˆ์˜ ์ฐจ์ด๋Š” ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋ฒˆ ๊ธ€์—์„œ๋Š” ON์ ˆ๊ณผ WHERE์ ˆ์˜ ์ฐจ์ด์™€ ์–ด๋–ค ์ƒํ™ฉ์—์„œ ๊ฐ๊ฐ ์‚ฌ์šฉํ•ด์•ผํ•˜๋Š”์ง€ ์•Œ์•„๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

 

 

SQL๋ฌธ ์ˆœ์„œ

ON JOIN ์ ˆ์ด ์ถ”๊ฐ€๋œ SQL๋ฌธ์˜ ์ˆœ์„œ๋Š” FROM ์ ˆ ๋‹ค์Œ์œผ๋กœ ์ง„ํ–‰ํ•ฉ๋‹ˆ๋‹ค. ์ฆ‰ ON์ ˆ์€ WHERE์ ˆ ๋ณด๋‹ค ์ˆœ์„œ์ƒ์œผ๋กœ ๋” ๋น ๋ฆ…๋‹ˆ๋‹ค. ๊ทธ๋Ÿผ ์ด๊ฒŒ ์‹ค์ œ ์ฟผ๋ฆฌ๋ฅผ ์‹คํ–‰ํ–ˆ์„ ๋•Œ ์–ด๋–ค ์ฐจ์ด๊ฐ€ ์žˆ๋Š”์ง€ ํ™•์ธํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

 

 

WHERE์ ˆ ON์ ˆ ์‹ค์Šต

OS : Red Hat Enterprise Linux Server release 6.3
DB : Oracle 12.1.0.2
user : scott

๊ธฐ๋ณธ์œผ๋กœ ์ฃผ์–ด์ง€๋Š” scott ๊ณ„์ •์˜ ํ…Œ์ด๋ธ”์„ ๊ฐ–๊ณ  ์‹ค์Šต์„ ์ง„ํ–‰ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

 

 

inner join

๊ธ‰์—ฌ๊ฐ€ 2000์ด ๋„˜๋Š” ์ง์›๋“ค์˜ ๋ถ€์„œ๋ฒˆํ˜ธ, ๋ถ€์„œ๋ช…, ์ด๋ฆ„, ๊ธ‰์—ฌ๋ฅผ ์ถœ๋ ฅํ•˜์‹œ์˜ค.

 

QUERY1

SELECT
	d.deptno
	,d.dname
	,e.ename
	,e.sal
FROM dept d,emp e
WHERE d.deptno = e.deptno
AND e.sal > 2000;

QUERY2

SELECT
	d.deptno
	,d.dname
	,e.ename
	,e.sal
FROM dept d INNER JOIN emp e
ON d.deptno = e.deptno
WHERE e.sal > 2000;

QUERY3

SELECT
	d.deptno
	,d.dname
	,e.ename
	,e.sal
FROM dept d INNER JOIN emp e
ON d.deptno = e.deptno AND e.sal > 2000;

๋™์ผํ•œ RESULT

inner join์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ ๋ฌธ์ œ์˜ ๊ฒฐ๊ณผ๋ฅผ ์–ป๊ธฐ์œ„ํ•ด ์œ„์˜ 3๊ฐ€์ง€ QUERY ์–ด๋Š๊ฒƒ์„ ์‚ฌ์šฉํ•˜๋“ ์ง€ ์ƒ๊ด€์ด ์—†์Šต๋‹ˆ๋‹ค. QUERY1๋ฒˆ์˜ ๊ฒฝ์šฐ FROM ์ ˆ์—์„œ Cartesian Product์— ์˜ํ•œ ์กฐ์ธ์—์„œ WHERE์ ˆ์—์„œ ์กฐ๊ฑด์„ ๋น„๊ตํ•˜๋Š” ๊ฒƒ์ด๊ณ , QUERT2์—์„œ๋Š” deptno์— ๋Œ€ํ•œ ์กฐ์ธ ์ž‘์—…์„ ํ•œ ํ›„ WHERE ์ ˆ์—์„œ sal์— ๋Œ€ํ•œ ๋น„๊ต๋ฅผ ํ•˜๋Š” ๊ฒƒ์ด๊ณ , QUERY3๋Š” ON์ ˆ์—์„œ ์กฐ์ธํ• ๋•Œ ๋ชจ๋“  ์กฐ๊ฑด์„ ๋น„๊ตํ•˜๋Š”๊ฒƒ์ž…๋‹ˆ๋‹ค.

 

inner join์€ ์กด์žฌํ•˜๋Š” ๊ฐ’์— ๋Œ€ํ•ด์„œ๋งŒ ์ถœ๋ ฅํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์กฐ๊ฑด์˜ ์œ„์น˜๋‚˜ ํ…Œ์ด๋ธ”์˜ ์ˆœ์„œ์— ๋ฌด๊ด€ํ•˜๊ฒŒ ๊ฐ™์€ ์‹คํ–‰๊ณ„ํš์œผ๋กœ ๊ฐ™์€ ๊ฐ’์ด ์ถœ๋ ฅ๋ฉ๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ outer join์„ ์‚ฌ์šฉํ•˜๊ฒŒ๋˜๋ฉด ์กฐ๊ฑด์˜ ์œ„์น˜์™€ ์ˆœ์„œ๊ฐ€ ๋งค์šฐ ์ค‘์š”ํ•ด์ง‘๋‹ˆ๋‹ค.

 

 

outer join

๊ธ‰์—ฌ๊ฐ€ 2000์ด ๋„˜๋Š” ์ง์›๋“ค์— ๋Œ€ํ•ด ๊ฐ ๋ถ€์„œ๋ณ„ ๊ธ‰์—ฌ์˜ ํ•ฉ์„ ๊ตฌํ•˜์‹œ์˜ค.

 

QUERY1

SELECT d.deptno, sum(e.sal)
FROM dept d LEFT OUTER JOIN emp e
ON d.deptno = e.deptno
WHERE e.sal > 2000
GROUP BY d.deptno
ORDER BY d.deptno;

์–ผํ•๋ณด๋ฉด ์ •๋‹ต๊ฐ™์ง€๋งŒ... SQL QUERY์˜ ์˜๋„์ƒ ์ž˜๋ชป๋œ ์ •๋ณด๋ฅผ ๊ฐ–๊ณ ์˜ค๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ๋ญ๊ฐ€ ๋ฌธ์ œ์ธ์ง€ ํ™•์ธํ•˜๊ธฐ ์œ„ํ•ด ์กฐ์ธ๋œ ํ…Œ์ด๋ธ”์˜ ๊ฒฐ๊ณผ๋งŒ ํ™•์ธํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

 

SELECT *
FROM dept d LEFT OUTER JOIN emp e
ON d.deptno = e.deptno;

outer join์„ ์‚ฌ์šฉํ•˜๋Š” ์˜๋„๋Š” ํŠน์ • ํ…Œ์ด๋ธ”์˜ ๋ชจ๋“  ๊ฐ’์ด ํฌํ•จ๋œ ๊ฒฐ๊ณผ๋ฅผ ๋ณด๊ธฐ ์œ„ํ•ด์„œ ์ž…๋‹ˆ๋‹ค. dept ํ…Œ์ด๋ธ”์—๋Š” 40๋ฒˆ ๋ถ€์„œ๊ฐ€ ์กด์žฌํ•˜๋Š”๋ฐ empํ…Œ์ด๋ธ”์ƒ์œผ๋กœ ํ˜„์žฌ 40๋ฒˆ ๋ถ€์„œ์— ์†Œ์†๋˜์–ด์žˆ๋Š” ์ง์›์ด ์—†์Šต๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ, ๊ฒฐ๊ณผ๋กœ 40๋ฒˆ๋ถ€์„œ๋„ ํ•จ๊ป˜ ํ‘œ์‹œ๋˜๋„๋กํ•˜๋Š” ๊ฒƒ์ด ๋ฌธ์ œ์˜ ๋ชฉ์ ์ž…๋‹ˆ๋‹ค.

 

QUERY1์˜ ๊ฒฝ์šฐ, deptno๋กœ ์กฐ์ธ์„ ํ•˜๊ณ  WHERE์ ˆ์—์„œ 2000์ด ์ดˆ๊ณผ๋˜๋Š” ์ง์›๋“ค์„ ํ•„ํ„ฐ๋งํ•ฉ๋‹ˆ๋‹ค. ์ด๋•Œ 40๋ฒˆ ๋ถ€์„œ์˜ ํ–‰์€ sal๊ฐ’์ด null์ด๊ธฐ ๋•Œ๋ฌธ์— ๋น„๊ต์กฐ๊ฑด์—์„œ ํ•ด๋‹น ํ–‰์„ ์ง€์›Œ๋ฒ„๋ฆฝ๋‹ˆ๋‹ค. ๊ทธ๋ž˜์„œ ์ตœ์ข…์ ์œผ๋กœ 40๋ฒˆ ๋ถ€์„œ๊ฐ€ ์—†๋Š” ๊ฒฐ๊ณผ๋ฅผ ์ถœ๋ ฅํ•˜๊ฒŒ๋ฉ๋‹ˆ๋‹ค.

40๋ฒˆ ๋ถ€์„œ๋„ ํ•จ๊ป˜ ๋ณด์ด๊ธฐ ์œ„ํ•ด ์•„๋ž˜์˜ SQL QUERY์ฒ˜๋Ÿผ ์ถœ๋ ฅํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค.

 

QUERY2

SELECT d.deptno, sum(e.sal)
FROM dept d LEFT OUTER JOIN emp e
ON d.deptno = e.deptno AND e.sal > 2000
GROUP BY d.deptno
ORDER BY d.deptno;

QUERY2๋Š” ON์ ˆ์— ํ•ด๋‹น 2000 ์ดˆ๊ณผ ์กฐ๊ฑด์„ ๋ช…์‹œํ•ฉ๋‹ˆ๋‹ค. ์ฆ‰ JOINํ• ๋•Œ ํ•ด๋‹น ์กฐ๊ฑด์„ ์ฒ˜๋ฆฌํ•˜๊ณ  outer join์ด๊ธฐ ๋•Œ๋ฌธ์— dept์— ์กด์žฌํ•˜๊ณ  emp์—๋Š” ์กด์žฌํ•˜์ง€ ์•Š๋Š” ๊ฒฐ๊ณผ๋„ ํ•จ๊ป˜ ์ถœ๋ ฅํ•˜๊ฒŒ๋ฉ๋‹ˆ๋‹ค.

 

ON์ ˆ๊ณผ WHERE์ ˆ์˜ ์ฐจ์ด๋Š” outer join์„ ์‚ฌ์šฉํ–ˆ์„๋•Œ ํ™•์‹คํžˆ ํ™•์ธ์ด ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. ON์ ˆ์˜ ์กฐ๊ฑด์œผ๋กœ ์กฐ์ธ๋˜๋Š” ๊ฒฐ๊ณผ๊ฐ€ ๋‹ฌ๋ผ์ง€๊ณ  ์กฐ๊ฑด์„ ๋น„๊ตํ•˜๋Š” ๋Œ€์ƒ์ด ๋‹ฌ๋ผ์ง€๋‹ˆ ํ™•์‹คํ•œ ์ฐจ์ด๋ฅผ ์•Œ์•„๋‘๊ณ  QUERY ์ž‘์„ฑ์ด ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค.

 

๋Œ“๊ธ€