๐Ÿ’ป ๊ณต๋ถ€ ๊ธฐ๋ก/๐Ÿ˜ SQL

SQL/Postgresql | Sub Query(์„œ๋ธŒ์ฟผ๋ฆฌ)

  • -
ํ•ด๋‹น ๋‚ด์šฉ์€ ๊ณต๋ถ€์˜ ๋ชฉ์ ์œผ๋กœ ๊ธฐ๋ก๋˜์—ˆ์œผ๋ฉฐ,
์•„๋ž˜ ๋‚ด์šฉ์€ ๋ชจ๋‘ ๊ตฌ๊ธ€ ๊ฒ€์ƒ‰์œผ๋กœ ๊ณต๋ถ€ ๋ธ”๋กœ๊ทธ๋ฅผ ์ฐธ๊ณ ํ•˜์—ฌ ์ž‘์„ฑํ–ˆ์Šต๋‹ˆ๋‹ค.
์—ด์‹ฌํžˆ ๋ฐฐ์šฐ๋Š” ์ค‘์ž…๋‹ˆ๋‹ค. ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค.

 

์ถœ์ฒ˜ | Everly

 

SQL ์„œ๋ธŒ์ฟผ๋ฆฌ(Sub Query) ์˜ˆ์ œ - select์ ˆ, from์ ˆ, where์ ˆ

์ด๋ฒˆ ํฌ์ŠคํŒ…์€ ์‹ค๋ฌด SQL์—์„œ ์ •๋ง ๋งŽ์ด ์‚ฌ์šฉํ•˜๋Š” ์„œ๋ธŒ์ฟผ๋ฆฌ(Sub Query)์— ๋Œ€ํ•ด ์†Œ๊ฐœํ•œ๋‹ค. โ€ป ์„œ๋ธŒ์ฟผ๋ฆฌ๋ž€? ์•ž์„  ํฌ์ŠคํŒ…์—์„œ ๊ฐ€์žฅ ๊ธฐ๋ณธ์ ์ธ SQL ๋ฌธ๋ฒ•์€ [SELECT, FROM, WHERE] ๋ผ๊ณ  ํ–ˆ์—ˆ๋‹ค. ์„œ๋ธŒ์ฟผ๋ฆฌ๋ž€ ์œ„์น˜์—

suy379.tistory.com

 


 

 

๐Ÿ“Œ ์„œ๋ธŒ์ฟผ๋ฆฌ๋ž€?

SQL์˜ ๊ฐ€์žฅ ๊ธฐ๋ณธ์ ์ธ ๊ตฌ์„ฑ์ธ [SELECT, FROM, WHERE] ๊ตฌ๋ฌธ์œผ๋กœ SELECT์ ˆ, FROM์ ˆ, WHERE์ ˆ์˜ ๊ฐ๊ฐ ํ•„์š”ํ•œ ์ ˆ์— ๊ธฐ๋ณธ์ ์ธ ๊ตฌ๋ฌธ์ด ๋“ค์–ด๊ฐ€๋Š” ๊ฒƒ์„ ์˜๋ฏธํ•œ๋‹ค.
์˜ˆ์‹œ๋กœ ์•„๋ž˜์™€ ๊ฐ™๋‹ค.
 - SELECT์ ˆ ( SELECT ... FROM ... WHERE ... )
 - FROM์ ˆ ( SELECT ... FROM ... WHERE ... )
 - WHERE์ ˆ ( SELECT ... FROM ... WHERE ... )

 

๐Ÿ“Œ SELECT์ ˆ

SELECT์ ˆ์˜ ์„œ๋ธŒ์ฟผ๋ฆฌ๋Š” ํ•˜๋‚˜์˜ ์—ด์ฒ˜๋Ÿผ ์‚ฌ์šฉ๋œ๋‹ค.

 

์˜ˆ์‹œ users table

 

์˜ˆ์‹œ photos table

 

photos table ์—์„œ users id ์— ํ•ด๋‹นํ•˜๋Š” ์‚ฌ์ง„๋งŒ ์ถœ๋ ฅํ•˜๊ณ ์ž ํ•œ๋‹ค.

์•„๋ž˜์™€ ๊ฐ™์€ SELECT์ ˆ ์ฟผ๋ฆฌ๋ฌธ์€ photos table์˜ 'filename' ์ปฌ๋Ÿผ๋งŒ ๊ฐ€์ ธ์™€์„œ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ์‹์ด๋‹ค.

SELECT
    p.filename
    , (SELECT nickname FROM users u WHERE u.id = p.user_id)
FROM photos p ;

์งœ์ž”

 

SELECT์ ˆ ์„œ๋ธŒ์ฟผ๋ฆฌ๋Š” ๋ฐ์ดํ„ฐ์˜ ์–‘์ด ๋งŽ์„ ์ˆ˜๋ก ์‹คํ–‰์†๋„๊ฐ€ ๋Š๋ ค์ง€๊ธฐ ๋•Œ๋ฌธ์— ๊ฑฐ์˜ ์‚ฌ์šฉ๋˜์ง€ ์•Š๋Š” ๋ช…๋ น์–ด์ด๋‹ค.

 

 

๐Ÿ“Œ FROM์ ˆ

FROM์ ˆ์˜ ์„œ๋ธŒ์ฟผ๋ฆฌ๋Š” ํ•˜๋‚˜์˜ ํ…Œ์ด๋ธ”์ฒ˜๋Ÿผ ์‚ฌ์šฉ๋œ๋‹ค.

ํ…Œ์ด๋ธ”์ฒ˜๋Ÿผ ์‚ฌ์šฉ๋˜๊ธฐ ๋•Œ๋ฌธ์— ์—ด ์ด๋ฆ„๊ณผ ํ…Œ์ด๋ธ”๋ช…์„ ๊ผญ ๋ช…์‹œํ•ด์ค˜์•ผ ํ•œ๋‹ค. (์ค‘์š”)

- ์ด๊ฑฐ ์•ˆ์ง€์ผฐ๋‹ค๊ฐ€ ์™œ SELECT ์ ์šฉ ์•ˆ๋˜๋Š”๊ฐ€๋ฅผ ์—‰๋šฑํ•œ ๊ณณ์—์„œ ์ˆ˜ ์‹ญ๋ฒˆ ํ™•์ธํ•ด์•ผ๋งŒ ํ–ˆ๋‹ค.

SELECT
    u.nickname AS ๋‹‰๋„ค์ž„
    , count(*) AS ๋ ˆ์ฝ”๋“œ๊ธฐ๋ก
FROM users u
    , (SELECT * FROM photos (WHERE ๊ตฌ๋ฌธ์œผ๋กœ ์—ฌ๋Ÿฌ ์กฐ๊ฑด์„ ๋„ฃ์„ ์ˆ˜ ์žˆ๋‹ค.)) p
WHERE u.id = p.user_id
GROUP BY u.nickname ;

์งœ์ž”

 FROM์ ˆ ์„œ๋ธŒ์ฟผ๋ฆฌ๋Š” ์‹ค์ „์—์„œ ์—ฌ๋Ÿฌ ๊ฐ€์ง€ ํ…Œ์ด๋ธ”์„ ์กฐ์ธ์‹œ์ผœ ๊ฐ€๊ณตํ•˜๊ณ , 1์ฐจ ๊ฐ€๊ณตํ•œ ๊ฒฐ๊ณผ๋ฌผ์„ ๋˜ ๋‹ค์‹œ 2์ฐจ ๊ฐ€๊ณต ๊ทธ ์ด์ƒ์œผ๋กœ ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ๋งŽ๋‹ค. 1์ฐจ ๊ฐ€๊ณตํ•œ ๊ฒฐ๊ณผ๋ฅผ ํ…Œ์ด๋ธ” ํ˜•ํƒœ๋กœ ๋‹ค์‹œ ์ €์žฅํ•œ๋‹ค๋ฉด ํŽธํ•˜๊ฒ ์ง€๋งŒ, ๋ถˆ๊ฐ€๋Šฅํ•  ๊ฒฝ์šฐ 1์ฐจ ๊ฐ€๊ณต๋ฌผ์„ FROM์ ˆ ์„œ๋ธŒ์ฟผ๋ฆฌ์— ๋„ฃ์–ด ํ•˜๋‚˜์˜ ํ…Œ์ด๋ธ”๋กœ ์ด์šฉํ•œ๋‹ค.

 

์ง์ ‘ ๋งŒ๋“  ์˜ˆ์‹œ ์ค‘ ๋น„์Šทํ•œ๊ฒŒ ์—†์–ด ํฌ์ŠคํŒ…ํ•œ ๋ธ”๋กœ๊ทธ์— ์‚ฌ์šฉ๋œ ์˜ˆ์‹œ๋ฅผ ๊ฐ€์ ธ์™”๋‹ค.

SELECT *
FROM (SELECT mem_no, SUM(sales_amt) AS tot_amt
		FROM [ORDER]
	GROUP BY mem_no) A 
LEFT JOIN [MEMBER] B
ON A.mem_no = B.mem_no

 ์œ„ ์ฟผ๋ฆฌ๋ฌธ์ฒ˜๋Ÿผ ์‚ฌ์šฉ๋˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ๋งŽ๋‹ค.

 

 

๐Ÿ“Œ WHERE์ ˆ

WHERE์ ˆ์˜ ์„œ๋ธŒ์ฟผ๋ฆฌ๋Š” ๊ฐ€์žฅ ๋Œ€ํ‘œ์ ์ธ ํ˜•ํƒœ๋ฅผ ๊ฐ€์ง„ ์ผ๋ฐ˜ ์„œ๋ธŒ์ฟผ๋ฆฌ์ด๋‹ค.

 

photos ์˜ user ๋ ˆ์ฝ”๋“œ ๊ธฐ๋ก์ด ์žˆ๋Š” ์œ ์ €๋งŒ ๋ถˆ๋Ÿฌ์˜ค๋„๋ก ์กฐ๊ฑด๋ฌธ์„ ์‚ฌ์šฉํ–ˆ๋‹ค.

SELECT 
    * 
FROM users u
WHERE
    u.id IN (
        SELECT p.user_id 
        FROM photos p
        WHERE p.user_id = u.id
        ) ;

user_id๋ฅผ ์ฐธ์กฐํ•œ๋‹ค.

 

์งœ์ž”

 

'๐Ÿ’ป ๊ณต๋ถ€ ๊ธฐ๋ก > ๐Ÿ˜ SQL' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

SQL | CONCAT  (0) 2023.01.09
SQL | LPAD, RPAD  (0) 2023.01.09
SQL/PostgreSQL | Sequence  (0) 2023.01.09
SQL | ์กฐ๊ฑด ๋ฐ์ดํ„ฐ ๊ฒ€์ƒ‰  (0) 2023.01.03
SQL | ๊ฐœ๋…  (0) 2023.01.03
Contents

ํฌ์ŠคํŒ… ์ฃผ์†Œ๋ฅผ ๋ณต์‚ฌํ–ˆ์Šต๋‹ˆ๋‹ค

์ด ๊ธ€์ด ๋„์›€์ด ๋˜์—ˆ๋‹ค๋ฉด ๊ณต๊ฐ ๋ถ€ํƒ๋“œ๋ฆฝ๋‹ˆ๋‹ค.