๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿ’ป Computer Science/๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค

[๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค] JOIN ์ข…๋ฅ˜(FULL OUTER, SELF, NATURAL, CROSS) ์˜ˆ์ œ ํฌํ•จ

by Jay Din 2024. 8. 23.
728x90
๋ฐ˜์‘ํ˜•

FULL OUTER, SELF, NATURAL, CROSS ์š”์•ฝ

FULL OUTER JOIN ๋‘ ํ…Œ์ด๋ธ”์˜ ๋ชจ๋“  ํ–‰์„ ๊ฒฐํ•ฉํ•˜๋ฉฐ, ์ผ์น˜ํ•˜์ง€ ์•Š๋Š” ํ–‰์€ NULL๋กœ ์ฑ„์›Œ์ง‘๋‹ˆ๋‹ค.
SELF JOIN ํ…Œ์ด๋ธ”์„ ์ž๊ธฐ ์ž์‹ ๊ณผ ์กฐ์ธํ•˜์—ฌ ๊ฐ™์€ ํ…Œ์ด๋ธ” ๋‚ด์—์„œ ํ–‰ ๊ฐ„์˜ ๊ด€๊ณ„๋ฅผ ํ‘œํ˜„ํ•ฉ๋‹ˆ๋‹ค.
NATURAL JOIN ๋‘ ํ…Œ์ด๋ธ” ๊ฐ„์˜ ๋™์ผํ•œ ์ด๋ฆ„์„ ๊ฐ€์ง„ ๋ชจ๋“  ์—ด์„ ๊ธฐ์ค€์œผ๋กœ ์ž๋™์œผ๋กœ ์กฐ์ธํ•ฉ๋‹ˆ๋‹ค.
CROSS JOIN ๋‘ ํ…Œ์ด๋ธ” ๊ฐ„์˜ ๋ชจ๋“  ๊ฐ€๋Šฅํ•œ ์กฐํ•ฉ์„ ์ƒ์„ฑํ•˜์—ฌ ์นดํ‹ฐ์…˜ ๊ณฑ์„ ๋งŒ๋“ญ๋‹ˆ๋‹ค.

 

1. FULL OUTER JOIN

FULL OUTER JOIN์€ ๋‘ ํ…Œ์ด๋ธ” ๊ฐ„์˜ ๋ชจ๋“  ํ–‰์„ ๊ฒฐํ•ฉํ•ฉ๋‹ˆ๋‹ค. ์ฆ‰, ์–‘์ชฝ ํ…Œ์ด๋ธ”์—์„œ ์ผ์น˜ํ•˜๋Š” ํ–‰์€ ํ•˜๋‚˜๋กœ ๊ฒฐํ•ฉ๋˜๋ฉฐ, ์ผ์น˜ํ•˜์ง€ ์•Š๋Š” ํ–‰๋„ ํฌํ•จํ•˜์—ฌ NULL ๊ฐ’์œผ๋กœ ์ฑ„์›Œ์ค๋‹ˆ๋‹ค

 

์˜ˆ์‹œ

ํ…Œ์ด๋ธ” Customers:

CustomerID CustomerName
1 Alice
2 Bob
3 Charlie

 

ํ…Œ์ด๋ธ” Orders:

CustomerID OrderAmount OrderAmount
101 1 100
102 2 150
103 4 200

 

์ฟผ๋ฆฌ:

SELECT Customers.CustomerID, Customers.CustomerName, Orders.OrderID, Orders.OrderAmount
FROM Customers
FULL OUTER JOIN Orders ON Customers.CustomerID = Orders.CustomerID;

 

๊ฒฐ๊ณผ:

CustomerID Customer Name OrderID OrderAmount
1 Alice 101 100
2 Bob 102 150
3 Charlie NULL NULL
NULL NULL 103 200

 

์„ค๋ช…:

  • CustomerID = 1, 2: Alice์™€ Bob์€ Orders ํ…Œ์ด๋ธ”์˜ OrderID 101๊ณผ OrderID 102์— ๊ฐ๊ฐ ๋งค์นญ๋ฉ๋‹ˆ๋‹ค.
  • CustomerID = 3: Charlie๋Š” ์ฃผ๋ฌธ์ด ์—†์œผ๋ฏ€๋กœ OrderID์™€ OrderAmount๋Š” NULL์ž…๋‹ˆ๋‹ค.
  • CustomerID = 4: OrderID 103์€ Customers ํ…Œ์ด๋ธ”์— ๋งค์นญ๋˜๋Š” ํ–‰์ด ์—†์œผ๋ฏ€๋กœ CustomerID์™€ CustomerName์€ NULL์ž…๋‹ˆ๋‹ค.

 

2. SELF JOIN

SELF JOIN์€ ํ…Œ์ด๋ธ”์„ ์ž๊ธฐ ์ž์‹ ๊ณผ ์กฐ์ธํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค.

์ž๊ธฐ ์ž์‹ ๊ณผ์˜ ๊ด€๊ณ„๋ฅผ ํ‘œํ˜„ํ•ด์•ผ ํ•  ๋•Œ ์‚ฌ์šฉ๋˜๋ฉฐ, ํ…Œ์ด๋ธ”์˜ ๊ฐ ํ–‰์„ ๋‹ค๋ฅธ ํ–‰๊ณผ ๋น„๊ตํ•˜๋Š” ๋ฐ ์œ ์šฉํ•ฉ๋‹ˆ๋‹ค.

 

์˜ˆ์‹œ:

ํ…Œ์ด๋ธ” Employees:

EmployeeID Name ManagerID
1 John 2
2 Jane NULL
3 Alice 2
4 Bob 1

 

์ฟผ๋ฆฌ:

SELECT e1.Name AS Employee, e2.Name AS Manager
FROM Employees e1
LEFT JOIN Employees e2 ON e1.ManagerID = e2.EmployeeID;

 

๊ฒฐ๊ณผ:

Employee Manager
John Jane
Jane NULL
Alice Jane
Bob John

 

์„ค๋ช…:

  • SELF JOIN์€ ๊ฐ™์€ ํ…Œ์ด๋ธ”์—์„œ ์ง์›๊ณผ ๊ทธ๋“ค์˜ ๊ด€๋ฆฌ์ž๋ฅผ ์กฐ์ธํ•˜์—ฌ, ๊ฐ ์ง์›์˜ ๊ด€๋ฆฌ์ž๋ฅผ ์ฐพ๋Š” ๋ฐ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค.
  • Jane์€ ์ตœ๊ณ  ๊ด€๋ฆฌ์ž์ด๋ฏ€๋กœ, Manager ์—ด์ด NULL์ž…๋‹ˆ๋‹ค.

 

3. NATURAL JOIN

NATURAL JOIN์€ ๋‘ ํ…Œ์ด๋ธ” ๊ฐ„์˜ ๋™์ผํ•œ ์ด๋ฆ„์„ ๊ฐ€์ง„ ๋ชจ๋“  ์—ด์„ ๊ธฐ์ค€์œผ๋กœ ์ž๋™์œผ๋กœ ์กฐ์ธํ•ฉ๋‹ˆ๋‹ค.

์—ด ์ด๋ฆ„๊ณผ ๊ฐ’์ด ์ผ์น˜ํ•˜๋Š” ํ–‰๋“ค๋งŒ ๊ฒฐํ•ฉ๋ฉ๋‹ˆ๋‹ค.

 

์˜ˆ์‹œ:

ํ…Œ์ด๋ธ” Departments:

DepartmentID DepartmentName
10 HR
20 Finance
30 IT

 

ํ…Œ์ด๋ธ” Employees:

EmployeeID DepartmentID EmployeeName
1 10 John
2 20 Jane
3 10 Jack

 

์ฟผ๋ฆฌ:

SELECT EmployeeID, EmployeeName, DepartmentName
FROM Employees
NATURAL JOIN Departments;

 

๊ฒฐ๊ณผ:

EmployeeID EmployeeName DepartmentName
1 John HR
2 Jane Finance
3 Jack HR

 

์„ค๋ช…:

  • NATURAL JOIN์€ ๋‘ ํ…Œ์ด๋ธ” ๊ฐ„์— ๊ณตํ†ต ์—ด์ธ DepartmentID๋ฅผ ์ž๋™์œผ๋กœ ์กฐ์ธํ•ฉ๋‹ˆ๋‹ค.
  • DepartmentID๊ฐ€ ์ผ์น˜ํ•˜๋Š” ํ–‰๋“ค๋งŒ ๊ฒฐํ•ฉ๋˜์–ด ๊ฒฐ๊ณผ๋กœ ๋ฐ˜ํ™˜๋ฉ๋‹ˆ๋‹ค.

 

4. CROSS JOIN

CROSS JOIN์€ ๋‘ ํ…Œ์ด๋ธ” ๊ฐ„์˜ ๋ชจ๋“  ๊ฐ€๋Šฅํ•œ ์กฐํ•ฉ์„ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.

๋‘ ํ…Œ์ด๋ธ”์˜ ๊ฐ ํ–‰์ด ์„œ๋กœ ๊ฒฐํ•ฉ๋˜์–ด ์นดํ‹ฐ์…˜ ๊ณฑ(Cartesian Product)์„ ๋งŒ๋“ญ๋‹ˆ๋‹ค.

 

์˜ˆ์‹œ

ํ…Œ์ด๋ธ” Products:

ProductID ProductName
1 Laptop
2 Phone

 

ํ…Œ์ด๋ธ” Suppliers:

SupplierID SupplierName
101 SupplierA
102 SupplierB

 

์ฟผ๋ฆฌ:

SELECT Products.ProductName, Suppliers.SupplierName
FROM Products
CROSS JOIN Suppliers;

 

๊ฒฐ๊ณผ:

ProductName SupplierName
Laptop SupplierA
Laptop SupplierB
Phone SupplierA
Phone SupplierB

 

์„ค๋ช…:

  • CROSS JOIN์€ ๋ชจ๋“  ์ œํ’ˆ์ด ๋ชจ๋“  ๊ณต๊ธ‰์ž์™€ ์กฐํ•ฉ๋˜๋ฏ€๋กœ, ์ด 4๊ฐœ์˜ ๊ฒฐ๊ณผ ํ–‰์ด ์ƒ์„ฑ๋ฉ๋‹ˆ๋‹ค.
  • ๊ฒฐ๊ณผ๋Š” ๋‘ ํ…Œ์ด๋ธ” ๊ฐ„์˜ ๋ชจ๋“  ๊ฐ€๋Šฅํ•œ ์กฐํ•ฉ์„ ํฌํ•จํ•ฉ๋‹ˆ๋‹ค.
728x90
๋ฐ˜์‘ํ˜•