๐Ÿ’ป [SQL] 15. ํ…Œ์ด๋ธ”์˜ ์ง‘ํ•ฉ ์—ฐ์‚ฐ

thisk336ยท2023๋…„ 10์›” 12์ผ
0

SQL

๋ชฉ๋ก ๋ณด๊ธฐ
15/17
post-thumbnail

์ง‘ํ•ฉ ์—ฐ์‚ฐ

  • SQL์—์„œ๋Š” ๋‘ ๊ฐœ ์ด์ƒ์˜ ํ…Œ์ด๋ธ” ๋˜๋Š” SELECT๋ฌธ์„ ๊ฒฐํ•ฉํ•˜๊ฑฐ๋‚˜ ๋น„๊ตํ•˜๋„๋ก ํ•ด์ฃผ๋Š” ์ง‘ํ•ฉ ์—ฐ์‚ฐ์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.
  • SQL ์ง‘ํ•ฉ ์—ฐ์‚ฐ์—๋Š” ํ•ฉ์ง‘ํ•ฉ, ๊ต์ง‘ํ•ฉ, ์ฐจ์ง‘ํ•ฉ์ด ์žˆ๋‹ค.

UNION

  • UNION์€ ๋‘ ๊ฐœ ์ด์ƒ์˜ ํ…Œ์ด๋ธ” ๋˜๋Š” SELECT ๋ฌธ์„ ๊ฒฐํ•ฉํ•˜์—ฌ, ์ค‘๋ณต๋œ ํ–‰์„ ์ œ๊ฑฐํ•œ๋‹ค.
  • ๊ธฐ๋ณธ ๊ตฌ์กฐ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.
[query 1]
UNION
[query 2]
SELECT film_id FROM film
UNION
SELECT film_id FROM inventory;
  • UNION ALL์€ ์ค‘๋ณต๋œ ํ–‰์„ ์ œ๊ฑฐํ•˜๋Š” UNION๊ณผ ๋‹ฌ๋ฆฌ ์ค‘๋ณต๋œ ํ–‰๋„ ํฌํ•จํ•˜์—ฌ ๋ณด์—ฌ์ค€๋‹ค.
SELECT film_id FROM film
UNION ALL
SELECT film_id FROM inventory;
  • ์ด ๋•Œ, ๊ฐ SELECT ๋ฌธ์˜ ์—ด์€ ๊ฐ™์€ ์ˆœ์„œ๋ฅผ ๊ฐ€์ ธ์•ผ ํ•˜๋ฉฐ, ์œ ์‚ฌํ•œ ๋ฐ์ดํ„ฐ ์œ ํ˜•์„ ๊ฐ€์ง€๊ณ  ์žˆ์–ด์•ผ ํ•œ๋‹ค.

INTERSECT

  • INTERSECT๋Š” ๋‘ ๊ฐœ ์ด์ƒ์˜ SELECT ๋ฌธ์˜ ๊ฒฐ๊ณผ ์ง‘ํ•ฉ์˜ ๊ต์ง‘ํ•ฉ์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค. ์ฆ‰, ๋ชจ๋“  SELECT ๋ฌธ์— ๊ณตํ†ต์ ์œผ๋กœ ์žˆ๋Š” ํ–‰์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
  • UNION๊ณผ UNION ALL๊ณผ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ, ๊ฐ SELECT ๋ฌธ์˜ ์—ด์€ ๊ฐ™์€ ์ˆœ์„œ๋ฅผ ๊ฐ€์ ธ์•ผ ํ•˜๋ฉฐ, ์œ ์‚ฌํ•œ ๋ฐ์ดํ„ฐ ์œ ํ˜•์„ ๊ฐ€์ง€๊ณ  ์žˆ์–ด์•ผ ํ•œ๋‹ค.
  • ๊ธฐ๋ณธ ๊ตฌ์กฐ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.
[query 1]
INTERSECT
[query 2]
SELECT film_id FROM film
INTERSECT
SELECT film_id FROM inventory;

EXCEPT

  • EXCEPT๋Š” ๋‘ SELECT ๋ฌธ์˜ ๊ฒฐ๊ณผ ์ง‘ํ•ฉ์˜ ์ฐจ์ง‘ํ•ฉ์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค. ์ฆ‰, ์ฒซ ๋ฒˆ์งธ ๊ฒฐ๊ณผ ์ง‘ํ•ฉ์—๋Š” ์žˆ์ง€๋งŒ ๋‘ ๋ฒˆ์งธ ๊ฒฐ๊ณผ ์ง‘ํ•ฉ์—๋Š” ์—†๋Š” ํ–‰์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
  • EXCEPT๋„ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ, ๊ฐ SELECT ๋ฌธ์˜ ์—ด์€ ๊ฐ™์€ ์ˆœ์„œ๋ฅผ ๊ฐ€์ ธ์•ผ ํ•˜๋ฉฐ, ์œ ์‚ฌํ•œ ๋ฐ์ดํ„ฐ ์œ ํ˜•์„ ๊ฐ€์ง€๊ณ  ์žˆ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.
  • ๊ธฐ๋ณธ ๊ตฌ์กฐ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.
[query 1]
EXCEPT
[query 2]
SELECT film_id FROM film
EXCEPT
SELECT film_id FROM inventory;

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