Search

์„œ๋ธŒ์ฟผ๋ฆฌ

์„œ๋ธŒ์ฟผ๋ฆฌ

: SQL๋ฌธ ๋‚ด๋ถ€์— ์‚ฌ์šฉํ•˜๋Š” SELECT ๋ฌธ
โ€ข
๋ฉ”์ธ์ฟผ๋ฆฌ : ์„œ๋ธŒ์ฟผ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ์ตœ์ข…์ ์ธ SELECT ๋ฌธ

์‹คํ–‰ ๊ฒฐ๊ณผ์— ๋”ฐ๋ฅธ ๋ถ„๋ฅ˜

โ€ข
๋‹จ์ผํ–‰ ์„œ๋ธŒ์ฟผ๋ฆฌ : ๊ฒฐ๊ณผ ๋ฐ์ดํ„ฐ๊ฐ€ ํ•˜๋‚˜์ธ ์„œ๋ธŒ์ฟผ๋ฆฌ
โ€ข
๋‹ค์ค‘ํ–‰ ์„œ๋ธŒ์ฟผ๋ฆฌ : ๊ฒฐ๊ณผ ๋ฐ์ดํ„ฐ๊ฐ€ ์—ฌ๋Ÿฌ ํ–‰์ธ ์„œ๋ธŒ์ฟผ๋ฆฌ

๋‹ค์ค‘ํ–‰ ์—ฐ์‚ฐ์ž

โ€ข
IN : ํ•ด๋‹น ์ปฌ๋Ÿผ๊ณผ ์—ฌ๋Ÿฌ ๊ฐœ์˜ ๊ฐ’์ด ์ผ์น˜ํ•˜๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ๊ตฌํ•˜๋Š” ์—ฐ์‚ฐ
โ€ข
ANY : A ๋น„๊ต์—ฐ์‚ฐ ANY B ๋น„๊ตํ•œ ๊ฒฐ๊ณผ ์ค‘, ํ•˜๋‚˜ ๋งŒ์ด๋ผ๋„ ์กฐ๊ฑด์„ ๋งŒ์กฑํ•˜๋Š” A ๋ฅผ ๊ตฌํ•˜๋Š” ์—ฐ์‚ฐ
โ€ข
ALL : A ๋น„๊ต์—ฐ์‚ฐ ALL B ๋น„๊ตํ•œ ๊ฒฐ๊ณผ ์ค‘, ๋ชจ๋“  ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•ด ์กฐ๊ฑด์„ ๋งŒ์กฑํ•˜๋Š” A ๋ฅผ ๊ตฌํ•˜๋Š” ์—ฐ์‚ฐ
โ€ข
EXISTS : ์„œ๋ธŒ์ฟผ๋ฆฌ์— ์กด์žฌํ•˜๋Š” ๋ฐ์ดํ„ฐ๋งŒ ์กฐํšŒํ•˜๋Š” ์—ฐ์‚ฐ

์‚ฌ์šฉ ์œ„์น˜์— ๋”ฐ๋ฅธ ๋ถ„๋ฅ˜

โ€ข
์Šค์นผ๋ผ ์„œ๋ธŒ์ฟผ๋ฆฌ : SELECT ์ ˆ์—์„œ ์‚ฌ์šฉํ•˜๋Š” ์„œ๋ธŒ์ฟผ๋ฆฌ
โ€ข
์ธ๋ผ์ธ ๋ทฐ : FROM ์ ˆ์—์„œ ์‚ฌ์šฉํ•˜๋Š” ์„œ๋ธŒ์ฟผ๋ฆฌ
โ€ข
์„œ๋ธŒ ์ฟผ๋ฆฌ : WHERE ์ ˆ ๋“ฑ์—์„œ ์‚ฌ์šฉํ•˜๋Š” ์„œ๋ธŒ์ฟผ๋ฆฌ

์Šค์นผ๋ผ ์„œ๋ธŒ์ฟผ๋ฆฌ

SELECT ์ ˆ์—์„œ ์‚ฌ์šฉํ•˜๋ฉฐ ๋‹จ์ผ ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•˜๋Š” ์„œ๋ธŒ์ฟผ๋ฆฌ
SELECT name, (SELECT COUNT(*) FROM orders WHERE customer_id = id) AS order_count FROM customers;
SQL
๋ณต์‚ฌ

์ธ๋ผ์ธ ๋ทฐ

FROM ์ ˆ์—์„œ ์‚ฌ์šฉํ•˜๋Š” ์„œ๋ธŒ์ฟผ๋ฆฌ
SELECT name, order_count FROM ( SELECT customer_id, COUNT(*) AS order_count FROM orders GROUP BY customer_id ) AS orders_by_customer;
SQL
๋ณต์‚ฌ

์„œ๋ธŒ ์ฟผ๋ฆฌ

WHERE ์ ˆ ๋“ฑ์—์„œ ์‚ฌ์šฉํ•˜๋Š” ์„œ๋ธŒ์ฟผ๋ฆฌ
SELECT name FROM customers WHERE id IN (SELECT customer_id FROM orders);
SQL
๋ณต์‚ฌ

WITH ์ ˆ

: ๊ณผ๋„ํ•œ ์„œ๋ธŒ์ฟผ๋ฆฌ ์‚ฌ์šฉ์œผ๋กœ ์ธํ•ด ์ฟผ๋ฆฌ์˜ ๊ฐ€๋…์„ฑ์ด ๋–จ์–ด์ง€๋Š” ๊ฒƒ์„ ๋ฐฉ์ง€ํ•˜๊ธฐ ์œ„ํ•ด์„œ, ์„œ๋ธŒ์ฟผ๋ฆฌ๋กœ ์‚ฌ์šฉํ•  SELECT ๋ฌธ์˜ ์กฐํšŒ๊ฒฐ๊ณผ๋ฅผ ์ž„์‹œํ…Œ์ด๋ธ”๋กœ ์ •์˜ํ•œ ๊ตฌ๋ฌธ
โ€ข
ํ˜•ํƒœ
WITH ( ๋ณ„์นญ1 AS (SELECT * FROM ํ…Œ์ด๋ธ”), ๋ณ„์นญ2 AS (SELECT * FROM ํ…Œ์ด๋ธ”), ... ) SELECT * FROM ๋ณ„์นญ1, ๋ณ„์นญ2, ...
SQL
๋ณต์‚ฌ