๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿ—‚.๋Œ€์™ธํ™œ๋™/๐Ÿ“.์นด์นด์˜คํด๋ผ์šฐ๋“œ์Šค์ฟจ

[DB] ๋ถ€์† ์งˆ์˜(subquery)

by ๐Ÿ’พ๊ณ ๊ตฌ๋งˆ๋ง›ํƒ•๋จน๊ณ ์‹ถ๋‹ค 2023. 12. 29.
728x90

๐ŸŽˆ ๋ถ€์† ์งˆ์˜(subquery)

ํ•˜๋‚˜์˜ SQL๋ฌธ ์•ˆ์— ๋‹ค๋ฅธ SQL๋ฌธ์ด ์ค‘์ฒฉ ๋œ ์งˆ์˜๋ฅผ ๋งํ•œ๋‹ค.
 

๐ŸŽˆ ์œ„์น˜์— ๋”ฐ๋ฅธ ๋ถ€์† ์งˆ์˜์˜ ์ข…๋ฅ˜

์œ„์น˜์— ๋”ฐ๋ผ ๋ถ€์† ์งˆ์˜์˜ ํ˜•ํƒœ๊ฐ€ ๋‹ค๋ฅด๋‹ค. 

๋ช…์นญ ์œ„์น˜ ์˜๋ฌธ ๋ฐ ๋™์˜์–ด
์Šค์นผ๋ผ ๋ถ€์† ์งˆ์˜ SELECT scalar subquery
์ธ๋ผ์ธ ๋ทฐ FROM inline view / table subquery
์ค‘์ฒฉ ์งˆ์˜ WHERE nested subquery / predicate subquery

 

๐ŸŽˆ  ์ฃผ ์งˆ์˜(main query)์™€ ๋ถ€์† ์งˆ์˜(sub query) ๊ฐ„์˜ ์ƒ๊ด€๊ด€๊ณ„

๐Ÿ‘‰ ์ƒ๊ด€ ๋ถ€์† ์งˆ์˜

์ฃผ ์งˆ์˜์˜ ๋‚ด์šฉ์„ ์ฐธ์กฐํ•ด์„œ ๋ถ€์† ์งˆ์˜์— ์ ์šฉํ•  ๋•Œ ์ƒ๊ด€ ๋ถ€์† ์งˆ์˜์ด๋‹ค. 

์ฃผ ์งˆ์˜์—์„œ ๋จผ์ € ์ˆ˜ํ–‰๋˜์–ด์„œ ๊ฒฐ๊ณผ๋ฅผ ๊ฐ€์ง€๊ณ  ๋ถ€์† ์งˆ์˜์˜ ๋‚ด์šฉ์„ ์ ์šฉํ•˜๋Š” ๊ฒƒ์ด๋‹ค.

์™œ ์ด๋ ‡๊ฒŒ ํ•˜๋ƒ๋ฉด ๊ฒฐ๊ณผ๋ฅผ ์ €์žฅํ•  ์ˆ˜ ์žˆ๋Š” ๊ธฐ๋Šฅ์ด ์—†๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. 

 

๐Ÿ‘‰  ๋น„์ƒ๊ด€ ๋ถ€์† ์งˆ์˜

์„œ๋กœ ๋…๋ฆฝ์ ์ธ ๊ฒฝ์šฐ, ๋ถ€์† ์งˆ์˜ ๊ฒฐ๊ณผ๋ฅผ ๊ฐ€์ง€๊ณ  ์ฃผ ์งˆ์˜๋ฅผ ์ˆ˜ํ–‰ํ•˜๋Š” ๊ฒฝ์šฐ์ด๋‹ค.

 

๐ŸŽˆ  ์Šค์นผ๋ผ ๋ถ€์† ์งˆ์˜ (SELECT)

๋ถ€์†์งˆ์˜์˜ ๊ฒฐ๊ณผ ๊ฐ’์„ ๋‹จ์ผ ํ–‰(์Šค์นผ๋ผ), ๋‹จ์ผ ์—ด์˜ ๋ฒกํ„ฐ ๊ฐ’์œผ๋กœ ๋ฐ˜ํ™˜ํ•œ๋‹ค. ๋‹จ์ผํ–‰(์Šค์นผ๋ผ, ๋‹จ์ผ ๊ฐ’)์ด๊ฑฐ๋‚˜ ๋‹จ์ผ ์—ด(์—ฌ๋Ÿฌ ํ–‰)๋กœ ๊ฒฐ๊ณผ๊ฐ€ ๋‚˜์™€์•ผํ•œ๋‹ค.

์ƒ๊ด€๊ด€๊ณ„: ์ƒ๊ด€/๋น„์ƒ๊ด€ ๋ชจ๋‘ ๊ฐ€๋Šฅํ•˜๋‹ค.

 

์˜ˆ์ œ: ๋งˆ๋‹น์„œ์ ์˜ ๊ณ ๊ฐ๋ณ„ ํŒ๋งค์•ก์„ ๋ณด์ด์‹œ์˜ค. (๊ฒฐ๊ณผ๋Š” ๊ณ ๊ฐ์ด๋ฆ„๊ณผ ๊ณ ๊ฐ๋ณ„ ํŒ๋งค์•ก์„ ์ถœ๋ ฅ) = ์ƒ๊ด€ ์งˆ์˜ ํ˜•ํƒœ

1๋ฒˆ๊ณผ ๊ฐ™์ด ๋ถ€์† ์งˆ์˜๋ฅผ ์ด์šฉํ•˜๊ฑฐ๋‚˜ 2๋ฒˆ๊ณผ ๊ฐ™์ด ์กฐ์ธ์„ ์‚ฌ์šฉํ•˜๋ฉด ์ •๋‹ต์„ ์ถœ๋ ฅํ•  ์ˆ˜ ์žˆ๋‹ค.ํ•˜์ง€๋งŒ 1๋ฒˆ์ฒ˜๋Ÿผ ๋ถ€์† ์งˆ์˜๋ฅผ ์ด์šฉํ•˜๋ฉด 2๋ฒˆ๋ณด๋‹ค ์†๋„๊ฐ€ ๋น ๋ฅด๊ฒŒ ๊ฒฐ๊ณผ๋ฅผ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.

-- 1.
SELECT (SELECT NAME 
        FROM CUSTOMER CS 
        WHERE CS.CUSTID = OD.CUSTID) "NAME", SUM(SALEPRICE) "TOTAL"
FROM ORDERS OD
GROUP BY OD.CUSTID;

-- 2.
SELECT NAME, SUM(SALEPRICE)
FROM ORDERS OD, CUSTOMER CS
WHERE OD.CUSTID = CS.CUSTID
GROUP BY NAME;

 

๐ŸŽˆ  ์ธ๋ผ์ธ ๋ทฐ (FROM)

FROM์ ˆ์—๋Š” ํ…Œ์ด๋ธ”๋งŒ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค. ๊ทธ๋ ‡๊ธฐ ๋•Œ๋ฌธ์— ๋ฐ์ดํ„ฐ๋Š” ๋‹ค์ค‘ ํ–‰, ๋‹ค์ค‘ ์—ด์ด์–ด๋„ ์ƒ๊ด€์—†๋‹ค. 2์ฐจ์› ํ…Œ์ด๋ธ”์ด๋ฉด ๋œ๋‹ค. ๋‹จ์ผํ–‰, ๋‹จ์ผ ์—ด์€ ๋ถˆ๊ฐ€๋Šฅํ•˜๋‹ค. ๋ถ€์† ์งˆ์˜์˜ ๊ฒฐ๊ณผ๋ฅผ ์‚ฌ์šฉํ•ด ์ฃผ ์งˆ์˜๋ฅผ ์ˆ˜ํ–‰ํ•œ๋‹ค.

์ƒ๊ด€๊ด€๊ณ„: ๋น„์ƒ๊ด€๋งŒ ๊ฐ€๋Šฅํ•˜๋‹ค. ๊ฐ€์ƒ์˜ ํ…Œ์ด๋ธ”์ธ ๋ทฐ ํ˜•ํƒœ๋กœ ์ œ๊ณต๋˜๋‹ˆ ์ƒ๊ด€ ๋ถ€์† ์งˆ์˜๋Š” ์‚ฌ์šฉํ•  ์ˆ˜ ์—†๋‹ค.

 

์˜ˆ์ œ: ๊ณ ๊ฐ๋ฒˆํ˜ธ๊ฐ€ 2 ์ดํ•˜์ธ ๊ณ ๊ฐ์˜ ํŒ๋งค์•ก์„ ๋ณด์ด์‹œ์˜ค. (๊ฒฐ๊ณผ๋Š” ๊ณ ๊ฐ์ด๋ฆ„๊ณผ ๊ณ ๊ฐ๋ณ„ ํŒ๋งค์•ก ์ถœ๋ ฅ)

SELECT CS.NAME, SUM(OD.SALEPRICE) "TOTAL"
FROM (SELECT CUSTID, NAME
      FROM CUSTOMER
      WHERE CUSTID <=2) CS, ORDERS OD
WHERE CS.CUSTID = OD.CUSTID
GROUP BY CS.NAME;

๋ณ€์ˆ˜๋ฅผ ์ €์žฅํ•˜๋Š” ๊ธฐ๋Šฅ์ด ์—†๊ธฐ ๋•Œ๋ฌธ์— ์•ž์— ์ˆ˜ํ–‰ํ•œ ๊ฒฐ๊ณผ๋ฅผ ์ €์žฅํ•  ์ˆ˜ ์—†์œผ๋‹ˆ ๋ถ€์† ์งˆ์˜๋ฅผ ์‚ฌ์šฉํ•ด ์ˆœ์ฐจ์  ์‹คํ–‰์„ ์ˆ˜ํ–‰ํ•˜๊ฒŒ ๋˜๋Š” ๊ฒƒ์ด๋‹ค. 

 

๐ŸŽˆ  ์ค‘์ฒฉ ์งˆ์˜ (WHERE)

WHERE์ ˆ์€ ์กฐ๊ฑด์„ ๋ช…์‹œํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉํ•œ๋‹ค. ๋”ฐ๋ผ์„œ ๋ณดํ†ต ๋ฐ์ดํ„ฐ๋ฅผ ์„ ํƒํ•˜๋Š” ์กฐ๊ฑด ์ˆ ์–ด์™€ ํ•จ๊ป˜ ์‚ฌ์šฉ๋œ๋‹ค. ๊ทธ๋ž˜์„œ ์ˆ ์–ด ๋ถ€์† ์งˆ์˜๋ผ๊ณ  ๋ถ€๋ฅด๊ธฐ๋„ ํ•œ๋‹ค.

์ˆ ์–ด ์—ฐ์‚ฐ์ž ๋ฐ˜ํ™˜ ํ–‰ ๋ฐ˜ํ™˜ ์—ด ์ƒ๊ด€
๋น„๊ต =, ๏ผž๏ผœ๏ผž=, ๏ผœ=, ๏ผœ๏ผž ๋‹จ์ผ ๋‹จ์ผ ๊ฐ€๋Šฅ
์ง‘ํ•ฉ IN, NOT IN ๋‹ค์ค‘ ๋‹จ์ผ ๊ฐ€๋Šฅ
ํ•œ์ • ALL, SOME(ANY) ๋‹ค์ค‘ ๋‹จ์ผ ๊ฐ€๋Šฅ
์กด์žฌ EXISTS, NOT EXISTS ๋‹ค์ค‘ ๋‹ค์ค‘ ํ•„์ˆ˜

 

1) ๋น„๊ต 

๋ถ€์† ์งˆ์˜๊ฐ€ ๋ฐ˜๋“œ์‹œ ๋‹จ์ผํ–‰, ๋‹จ์ผ ์—ด์„ ๋ฐ˜ํ™˜ํ•ด์•ผํ•œ๋‹ค. ๊ทธ๋ž˜์•ผ ๋น„๊ต๋ฅผ ํ•  ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.

 

์˜ˆ์ œ: ํ‰๊ท  ์ฃผ๋ฌธ๊ธˆ์•ก ์ดํ•˜์˜ ์ฃผ๋ฌธ์— ๋Œ€ํ•ด์„œ ์ฃผ๋ฌธ๋ฒˆํ˜ธ์™€ ๊ธˆ์•ก์„ ๋ณด์ด์‹œ์˜ค.

์˜ˆ์ œ: ๊ฐ ๊ณ ๊ฐ์˜ ํ‰๊ท  ์ฃผ๋ฌธ๊ธˆ์•ก๋ณด๋‹ค ํฐ ๊ธˆ์•ก์˜ ์ฃผ๋ฌธ ๋‚ด์—ญ์— ๋Œ€ํ•ด์„œ ์ฃผ๋ฌธ๋ฒˆํ˜ธ, ๊ณ ๊ฐ๋ฒˆํ˜ธ, ๊ธˆ์•ก์„ ๋ณด์ด์‹œ์˜ค.

-- 1. ๋น„์ƒ๊ด€ ์„œ๋ธŒ ์งˆ์˜
SELECT ORDERID, SALEPRICE
FROM ORDERS
WHERE SALEPRICE <= (SELECT AVG(SALEPRICE)
                    FROM ORDERS);
-- 2. ์ƒ๊ด€ ์„œ๋ธŒ ์งˆ์˜
SELECT ORDERID, CUSTID, SALEPRICE
FROM ORDERS OD
WHERE SALEPRICE > (SELECT AVG(SALEPRICE)
                   FROM ORDERS SOD
                   WHERE OD.CUSTID = SOD.CUSTID);

 

2)  ์ง‘ํ•ฉ: IN, NOT IN

IN(OR์—ฐ์‚ฐ)์€ ๋ถ€์† ์งˆ์˜์˜ ๊ฒฐ๊ณผ ์ง‘ํ•ฉ์— ์ฃผ ์งˆ์˜ ๊ฐ’์ด ์žˆ๋Š”์ง€ ํ™•์ธํ•˜๋Š” ์—ญํ• ์ด๋‹ค(์ง‘ํ•ฉ์—ฐ์‚ฐ). ๋‹ค์ค‘ ํ–‰์„ ๊ฐ€์งˆ ์ˆ˜ ์žˆ๋‹ค. NOT IN์€ ๊ฐ’์ด ์กด์žฌํ•˜์ง€ ์•Š์œผ๋ฉด ์ฐธ์ด๋œ๋‹ค.

 

์˜ˆ์ œ: ๋Œ€ํ•œ๋ฏผ๊ตญ์— ๊ฑฐ์ฃผํ•˜๋Š” ๊ณ ๊ฐ์—๊ฒŒ ํŒ๋งคํ•œ ๋„์„œ์˜ ์ดํŒ๋งค์•ก์„ ๊ตฌํ•˜์‹œ์˜ค.

SELECT SUM(SALEPRICE) "TOTAL"
FROM ORDERS
WHERE CUSTID IN (SELECT CUSTID
                 FROM CUSTOMER
                 WHERE ADDRESS LIKE '%๋Œ€ํ•œ๋ฏผ๊ตญ%');

 

3) ํ•œ์ •: ALL, SOME(ANY)

ALL์€ ๋ชจ๋‘(AND ์—ฐ์‚ฐ์„ ์ด์–ด์„œ ์“ธ ๋•Œ ์œ ์šฉํ•˜๋‹ค.), SOME์€ ํ•˜๋‚˜๋ผ๋„ ์ฐธ์ด๋ฉด ๋œ๋‹ค.

 

์˜ˆ์ œ: 3๋ฒˆ ๊ณ ๊ฐ์ด ์ฃผ๋ฌธํ•œ ๋„์„œ์˜ ์ตœ๊ณ  ๊ธˆ์•ก๋ณด๋‹ค ๋” ๋น„์‹ผ ๋„์„œ๋ฅผ ๊ตฌ์ž…ํ•œ ์ฃผ๋ฌธ์˜ ์ฃผ๋ฌธ๋ฒˆํ˜ธ์™€ ๊ธˆ์•ก์„ ๋ณด์ด์‹œ์˜ค.

SELECT ORDERID, SALEPRICE
FROM ORDERS
WHERE SALEPRICE > ALL (SELECT SALEPRICE 
			FROM ORDERS 
			WHERE CUSTID = 3);

 

4) ์กด์žฌ: EXISTS, NOT EXISTS

๋ฐ์ดํ„ฐ์˜ ์กด์žฌ ์œ ๋ฌด ํ™•์ธํ•œ๋‹ค. IN๊ณผ ํ—ท๊ฐˆ๋ฆด ์ˆ˜ ์žˆ์ง€๋งŒ EXISTS๋Š” ์•ž์— ๋ณ€๋Ÿ‰, ๋Œ€์ƒ์ž๊ฐ€ ์—†๋‹ค(๋ฌธ๋ฒ•์ ์œผ๋กœ). ๊ทธ๋ฆฌ๊ณ  ๊ฐ’์„ ํ™•์ธํ•˜์ง€์•Š๊ณ  ์กด์žฌ ์œ ๋ฌด๋งŒ ํ™•์ธํ•œ๋‹ค. FROM์ ˆ์˜ ์ „์ฒด ๋Œ€์ƒ์ž๊ฐ€ ๋ถ€์† ์งˆ์˜์™€ ๋น„๊ตํ•ด์„œ ์กด์žฌํ•˜๋Š”์ง€ ํ™•์ธํ•˜๋Š” ๊ฒƒ์ด๊ธฐ ๋•Œ๋ฌธ์— 100% ์ƒ๊ด€ ์งˆ์˜๋งŒ ๊ฐ€๋Šฅํ•˜๋‹ค.

 

์˜ˆ์ œ: EXISTS ์—ฐ์‚ฐ์ž๋กœ ๋Œ€ํ•œ๋ฏผ๊ตญ์— ๊ฑฐ์ฃผํ•˜๋Š” ๊ณ ๊ฐ์—๊ฒŒ ํŒ๋งคํ•œ ๋„์„œ์˜ ์ด ํŒ๋งค์•ก์„ ๊ตฌํ•˜์‹œ์˜ค.

SELECT SUM(SALEPRICE) "TOTAL"
FROM ORDERS OD
WHERE EXISTS (SELECT *
              FROM CUSTOMER CS
              WHERE ADDRESS LIKE '%๋Œ€ํ•œ๋ฏผ๊ตญ%' AND CS.CUSTID = OD.CUSTID);

 

728x90