๐Ÿ’ป [SQL] 10. ์„œ๋ธŒ์ฟผ๋ฆฌ(Subquery)

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

SQL

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

์„œ๋ธŒ์ฟผ๋ฆฌ(Subquery)

  • ์„œ๋ธŒ์ฟผ๋ฆฌ๋ž€ SQL๋ฌธ ์•ˆ์— ํฌํ•จ๋˜์–ด ์žˆ๋Š” SQL๋ฌธ์„ ๋งํ•˜๋ฉฐ, SQL๋ฌธ ์•ˆ์—์„œ ๊ด„ํ˜ธ() ๋ฅผ ์‚ฌ์šฉํ•ด์„œ, ์„œ๋ธŒ์ฟผ๋ฆฌ๋ฌธ์„ ์ถ”๊ฐ€ํ•  ์ˆ˜ ์žˆ๋‹ค.
  • ์ฃผ๋กœ ํ…Œ์ด๋ธ”๊ณผ ํ…Œ์ด๋ธ”๊ฐ„์˜ ๊ฒ€์ƒ‰์‹œ, ๊ฒ€์ƒ‰ ๋ฒ”์œ„(ํ…Œ์ด๋ธ” ์ค‘ ํ•„์š”ํ•œ ๋ถ€๋ถ„๋งŒ ๋จผ์ € ๊ฐ€์ ธ์˜ค๋„๋ก)๋ฅผ ์ขํžˆ๋Š” ๊ธฐ๋Šฅ์— ์‚ฌ์šฉํ•œ๋‹ค.
  • SELECT, FROM, WHERE, HAVING, ORDER BY์ ˆ์ด๋‚˜ INSERT, UPDATE, DELETE ๋ฌธ์—๋„ ์‚ฌ์šฉ๊ฐ€๋Šฅํ•˜๋‹ค.
-- WHERE ์ ˆ์— ์‚ฌ์šฉ
SELECT title
FROM items
WHERE item_code IN
(SELECT item_code FROM ranking WHERE sub_category = '์—ฌ์„ฑ์‹ ๋ฐœ')

-- FROM ์ ˆ์— ์‚ฌ์šฉ
SELECT
a, b, c
FROM
(SELECT * FROM atoz_table)

-- SELETE ์ ˆ์— ์‚ฌ์šฉ
SELECT number, name, 
	(SELECT height FROM a where number = 25) AS height
FROM b
WHERE name = 'John'

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