EEALL@ONCE

๐Ÿ“Š ์ฟผ๋ฆฌ ์Šคํ„ฐ๋””(2) - ์‚ฌ์šฉ์ž ๋ณ„ ๊ฒŒ์‹œ๋ฌผ์„ ์ž‘์„ฑํ•œ ์ˆ˜์— ๋”ฐ๋ผ ์‚ฌ์šฉ์ž๋ฅผ ์„ ํƒ (HAVING / SUBQUERY / IN, EXISTS, ALL, ANY, SOME ) ๋ณธ๋ฌธ

Data๐Ÿ“Š/SQL ์ฟผ๋ฆฌ ์Šคํ„ฐ๋””

๐Ÿ“Š ์ฟผ๋ฆฌ ์Šคํ„ฐ๋””(2) - ์‚ฌ์šฉ์ž ๋ณ„ ๊ฒŒ์‹œ๋ฌผ์„ ์ž‘์„ฑํ•œ ์ˆ˜์— ๋”ฐ๋ผ ์‚ฌ์šฉ์ž๋ฅผ ์„ ํƒ (HAVING / SUBQUERY / IN, EXISTS, ALL, ANY, SOME )

์˜ฌ์—ฃ์›์Šค 2023. 7. 27. 18:41
728x90

1. ๐Ÿ“Šํ…Œ์ด๋ธ”

 

 

2. ๐Ÿง ๋ฌธ์ œ 

2๊ฐœ ์ด์ƒ์˜ ๊ฒŒ์‹œ๋ฌผ์„ ์ž‘์„ฑํ•œ ์‚ฌ์šฉ์ž๋งŒ ์„ ํƒํ•˜๊ณ  ์‹ถ๋‹ค. 

 

3. ๐Ÿ’ก ํ•ด๊ฒฐ

SELECT 
    u.id,
    u.username
FROM 
    Users u
WHERE 
    u.id IN (
        SELECT 
            p.user_id
        FROM 
            Posts p
        GROUP BY 
            p.user_id
        HAVING 
            COUNT(*) >= 2
    );

 

๐Ÿ”ด HAVING

HAVING ์ ˆ์€ SQL์—์„œ ๊ทธ๋ฃนํ™”๋œ ๊ฒฐ๊ณผ์— ๋Œ€ํ•œ ํ•„ํ„ฐ๋ฅผ ์ œ๊ณต. ์ฆ‰, GROUP BY ์ ˆ๋กœ ๊ทธ๋ฃนํ™”๋œ ๊ฒฐ๊ณผ ์ค‘ ํŠน์ • ์กฐ๊ฑด์„ ๋งŒ์กฑํ•˜๋Š” ๊ทธ๋ฃน๋งŒ์„ ์„ ํƒํ•˜๊ณ  ์‹ถ์„ ๋•Œ ์‚ฌ์šฉ

HAVING ์ ˆ์€ GROUP BY ์ ˆ๊ณผ ํ•จ๊ป˜ ์‚ฌ์šฉ๋˜๋ฉฐ, WHERE ์ ˆ๊ณผ ๋น„์Šทํ•˜๊ฒŒ ๋™์ž‘ํ•˜์ง€๋งŒ ์ค‘์š”ํ•œ ์ฐจ์ด์ ์ด ์žˆ์Œ!

WHERE ์ ˆ์€ ๊ฐœ๋ณ„ ํ–‰์— ๋Œ€ํ•œ ํ•„ํ„ฐ๋ฅผ ์ œ๊ณตํ•˜๋Š” ๋ฐ˜๋ฉด, HAVING ์ ˆ์€ ๊ทธ๋ฃน์— ๋Œ€ํ•œ ํ•„ํ„ฐ๋ฅผ ์ œ๊ณต! 

 

๐Ÿ”ดWHERE ๋’ค SUBQUERY 

์„œ๋ธŒ์ฟผ๋ฆฌ๋Š” ์ฃผ๋กœ IN, EXISTS, ALL, ANY, SOME ๋“ฑ์˜ ์—ฐ์‚ฐ์ž์™€ ํ•จ๊ป˜ ์‚ฌ์šฉ

      ๐ŸŸ  IN : IN ์—ฐ์‚ฐ์ž๋Š” ์„œ๋ธŒ์ฟผ๋ฆฌ๊ฐ€ ๋ฐ˜ํ™˜ํ•˜๋Š” ๊ฐ’ ๋ชฉ๋ก ์ค‘์—์„œ ์ผ์น˜ํ•˜๋Š” ๊ฐ’์„ ์ฐพ์Œ

'USA'์—์„œ ๊ฑฐ๋ž˜๊ฐ€ ์ด๋ฃจ์–ด์ง„ ๊ณ ๊ฐ์˜ ๋ชจ๋“  ์ฃผ๋ฌธ์„ ์ฐพ์Œ

SELECT * 
FROM Orders
WHERE CustomerID IN (SELECT CustomerID FROM Customers WHERE Country = 'USA');

      ๐ŸŸ  EXISTS : EXISTS ์—ฐ์‚ฐ์ž๋Š” ์„œ๋ธŒ์ฟผ๋ฆฌ๊ฐ€ ์ตœ์†Œํ•œ ํ•˜๋‚˜์˜ ํ–‰์„ ๋ฐ˜ํ™˜ํ•˜๋ฉด ์ฐธ(TRUE)์„ ๋ฐ˜ํ™˜

SELECT SupplierName
FROM Suppliers
WHERE EXISTS (SELECT ProductName FROM Products WHERE Products.SupplierID = Suppliers.supplierID AND Price < 20);

๊ฐ€๊ฒฉ์ด 20 ๋ฏธ๋งŒ์ธ ์ œํ’ˆ์„ ๊ณต๊ธ‰ํ•˜๋Š” ๊ณต๊ธ‰์—…์ฒด๋ฅผ ์ฐพ์Œ

       ๐ŸŸ ALL : ALL ์—ฐ์‚ฐ์ž๋Š” ๋ชจ๋“  ์„œ๋ธŒ์ฟผ๋ฆฌ ๊ฒฐ๊ณผ์— ๋Œ€ํ•ด ๋น„๊ต ์กฐ๊ฑด์„ ๋งŒ์กฑํ•˜๋Š”์ง€ ํ™•์ธ

SELECT ProductName 
FROM Products 
WHERE Price > ALL (SELECT Price FROM Products WHERE Category = 'Drink');

๋ชจ๋“  ์Œ๋ฃŒ๋ณด๋‹ค ๋น„์‹ผ ๋ชจ๋“  ์ œํ’ˆ์„ ์ฐพ์Œ

       ๐ŸŸ ANY or SOME : ANY๋‚˜ SOME ์—ฐ์‚ฐ์ž๋Š” ์„œ๋ธŒ์ฟผ๋ฆฌ ๊ฒฐ๊ณผ ์ค‘ ํ•˜๋‚˜๋ผ๋„ ๋น„๊ต ์กฐ๊ฑด์„ ๋งŒ์กฑํ•˜๋ฉด ์ฐธ(TRUE)์„ ๋ฐ˜ํ™˜

SELECT ProductName 
FROM Products 
WHERE Price > ANY (SELECT Price FROM Products WHERE Category = 'Drink');

๊ฐ€๊ฒฉ์ด ์Œ๋ฃŒ ์ค‘ ํ•˜๋‚˜๋ณด๋‹ค ๋น„์‹ผ ์ œํ’ˆ์„ ์ฐพ์Œ

 

๋‚˜๋Š” ์ด์ค‘์— EXIST์™€ ANY๊ฐ€ ๋ฉ”์ปค๋‹ˆ์ฆ˜์ด ์–ด๋–ป๊ฒŒ ๋˜๋Š”๊ฑด์ง€ ์ข€ ๋” ์•Œ์•„๋ณด๊ธฐ๋กœ ํ–ˆ๋Œœ..  ์ฐธ์œผ๋กœ ๋ฐ˜ํ™˜ํ•˜๋Š”๋ฐ ๊ทธ๊ฒŒ ๊ทธ๋Ÿผ ์„ ํƒ ๋œ๋‹ค๋Š” ๊ฒƒ๊ณผ ๊ฐ™์€ ์˜๋ฏธ ์ธ๊ฑด๊ฐ€ ?? 

SELECT *
FROM Customers c
WHERE EXISTS (SELECT 1 FROM Orders o WHERE o.CustomerID = c.CustomerID);

๊ณ ๊ฐ ์ค‘์—์„œ ์ฃผ๋ฌธ์„ ํ•œ ์ ์ด ์žˆ๋Š” ์‚ฌ๋žŒ๋“ค์˜ ์ •๋ณด๋ฅผ ์•Œ๊ณ  ์‹ถ๋‹ค๋ฉด ์œ„์˜ ์ฟผ๋ฆฌ๋กœ ์งœ๋ฉด ๋œ๋‹ค. 

๋ฉ”์ปค๋‹ˆ์ฆ˜์€ ..

(1) ๊ฐ ๊ณ ๊ฐ์— ๋Œ€ํ•ด, ํ•ด๋‹น ๊ณ ๊ฐ ID๋ฅผ ๊ฐ€์ง„ ์ฃผ๋ฌธ์ด Orders ํ…Œ์ด๋ธ”์— ์žˆ๋Š”์ง€ ํ™•์ธ.

(2) ๋งŒ์•ฝ ๊ทธ๋Ÿฌํ•œ ์ฃผ๋ฌธ์ด ์กด์žฌํ•˜๋ฉด, EXISTS๋Š” ์ฐธ(TRUE)์„ ๋ฐ˜ํ™˜ํ•˜๊ณ , ํ•ด๋‹น ๊ณ ๊ฐ์˜ ์ •๋ณด๊ฐ€ ๊ฒฐ๊ณผ์— ํฌํ•จ ์ฆ‰ select ๋จ

 

๋”ฐ๋ผ์„œ, ...

์ฐธ์œผ๋กœ ๋ฐ˜ํ™˜ํ•˜๋Š”๋ฐ ๊ทธ๊ฒŒ ๊ทธ๋Ÿผ ์„ ํƒ ๋œ๋‹ค๋Š” ๊ฒƒ๊ณผ ๊ฐ™์€ ์˜๋ฏธ ์ธ๊ฑด๊ฐ€ ?? ----> ๊ทธ๋ ‡๋‹ค.! 

728x90