๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
DB/Oracle

[Oracle] IN, NOT IN ํŠน์ • ๋ฐ์ดํ„ฐ ํฌํ•จ/์ œ์™ธ ํ•˜๊ธฐ

by yunamom 2022. 4. 15.
๋ฐ˜์‘ํ˜•

์•ˆ๋…•ํ•˜์„ธ์š” yunamom ์ž…๋‹ˆ๋‹ค. :D

 

ํŠน์ • ๋ฐ์ดํ„ฐ ๊ฐ’์„ ํฌํ•จํ•˜๋Š” ํ˜น์€ ์ œ์™ธํ•œ ๋ฐ์ดํ„ฐ๋ฅผ ์ถ”์ถœํ•˜๊ณ  ์‹ถ์„ ๋•Œ ์‚ฌ์šฉํ•˜๋Š” IN, NOT IN ๋ฌธ ์‚ฌ์šฉ๋ฒ•์— ๋Œ€ํ•˜์—ฌ ํฌ์ŠคํŒ…ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.๐Ÿ˜Š


โœจIN ๋ฌธ ์‚ฌ์šฉ๋ฒ•

SELECT * 
    FROM TABLE
  WHERE [์ปฌ๋Ÿผ๋ช…] IN ('A','B'); -- ์—ฌ๋Ÿฌ ๋ฐ์ดํ„ฐ ์ œ์™ธ ๊ฐ€๋Šฅ
  
  
  -- IN ๋ฌธ์„ ํ’€์ดํ•œ ์ฟผ๋ฆฌ๋ฌธ
SELECT *
    FROM TABLE
  WHERE [์ปฌ๋Ÿผ๋ช…] = 'A'
     OR [์ปฌ๋Ÿผ๋ช…] = 'B'

โœจNOT IN ๋ฌธ ์‚ฌ์šฉ๋ฒ•

SELECT * 
    FROM TABLE
  WHERE [์ปฌ๋Ÿผ๋ช…] NOT IN ('A','B'); -- ์—ฌ๋Ÿฌ ๋ฐ์ดํ„ฐ ์ œ์™ธ ๊ฐ€๋Šฅ
  
  
  -- NOT IN ๋ฌธ์„ ํ’€์ดํ•œ ์ฟผ๋ฆฌ๋ฌธ
SELECT *
    FROM TABLE
  WHERE [์ปฌ๋Ÿผ๋ช…] <> 'A'
    AND [์ปฌ๋Ÿผ๋ช…] <> 'B'

โœจSUBQUERY IN

SELECT * 
    FROM TABLE
  WHERE [์ปฌ๋Ÿผ๋ช…] IN (SELECT [์ปฌ๋Ÿผ๋ช…]
                     FROM [TABLE2])

โœจSUBQUERY NOT IN

SELECT * 
    FROM TABLE
  WHERE [์ปฌ๋Ÿผ๋ช…] NOT IN (SELECT [์ปฌ๋Ÿผ๋ช…]
                         FROM [TABLE2])

 

๐Ÿ’ก IN,NOT IN ๋ฌธ ์•ˆ์— ์„œ๋ธŒ์ฟผ๋ฆฌ ์‚ฌ์šฉ์‹œ ์ฃผ์˜์‚ฌํ•ญ

NOT IN๋ฌธ ์„œ๋ธŒ์ฟผ๋ฆฌ์˜ ๊ฒฐ๊ณผ ์ค‘์— NULL์ด ํฌํ•จ๋˜๋Š” ๊ฒฝ์šฐ ๋ฐ์ดํ„ฐ๊ฐ€ ์ถœ๋ ฅ๋˜์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์—

์กฐํšŒ ์ปฌ๋Ÿผ์— IS NOT NULL ์กฐ๊ฑด์„ ์ž‘์„ฑํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

 

NULL์€ ๋…ผ๋ฆฌ์ ์œผ๋กœ ๋น„๊ตํ•  ์ˆ˜ ์—†๋Š” ์—ฐ์‚ฐ์ด๊ธฐ ๋•Œ๋ฌธ์— ๊ฐ’์ด ์—†๊ฒŒ ๋˜๋Š” ๊ฒƒ์ด์ฃ .

๊ทธ๋Ÿฌ๋ฏ€๋กœ, ์„œ๋ธŒ์ฟผ๋ฆฌ ๋‚ด์— ํ•ด๋‹น์ปฌ๋Ÿผ์ด IS NOT NULL์ธ ์กฐ๊ฑด์„ ์ฃผ์–ด NULL ์ธ ๋ฐ์ดํ„ฐ๋ฅผ  ์ œ์™ธํ•˜๊ณ  ์กฐํšŒํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

SELECT * 
    FROM TABLE
  WHERE [์ปฌ๋Ÿผ๋ช…] NOT IN (SELECT [์ปฌ๋Ÿผ๋ช…2]
                         FROM [TABLE2]
                       WHERE [์ปฌ๋Ÿผ๋ช…2] IS NOT NULL)

oracle

300x250

์ฝ”๋“œ