The FULL OUTER JOIN is used in SQL to combine the results of both a LEFT JOIN and a RIGHT JOIN. It returns all records from both the left and the right table. If there is no match, NULL values are returned for the columns from the table that does not have the matching row. In other words, the FULL OUTER JOIN ensures that no rows from either table are excluded, even if there are no corresponding matches.
The basic syntax of a FULL OUTER JOIN is as follows:
SELECT column1, column2, ... FROM table1 FULL OUTER JOIN table2 ON table1.column = table2.column;
In this syntax:
Consider two tables: Employees and Departments. The Employees table contains employee details, and the Departments table contains department details. If you want to list all employees and departments, regardless of whether they have a match in the other table, you can use a FULL OUTER JOIN:
SELECT Employees.EmployeeID, Employees.Name, Departments.DepartmentName FROM Employees FULL OUTER JOIN Departments ON Employees.DepartmentID = Departments.DepartmentID;
This query will return all records from both the Employees and Departments tables. If an employee is not assigned to a department, or if a department has no employees, NULL values will appear for the missing data.
If there are departments in the Departments table that have no employees, or employees in the Employees table that are not assigned to any department, the FULL OUTER JOIN will still include these records. For example, if the "HR" department has employees, but the "IT" department has no employees, and some employees are not assigned to any department, the query might return this result:
EmployeeID | Name | DepartmentName ------------------------------------------ 101 | John Doe | HR 102 | Jane Smith | HR NULL | NULL | IT 103 | Bob Brown | NULL
Here:
You can use the FULL OUTER JOIN with multiple tables. For instance, if you want to list all employees, departments, and their respective orders, you can join three tables:
SELECT Employees.Name, Departments.DepartmentName, Orders.OrderID FROM Employees FULL OUTER JOIN Departments ON Employees.DepartmentID = Departments.DepartmentID FULL OUTER JOIN Orders ON Employees.EmployeeID = Orders.EmployeeID;
This query will return all employees, departments, and orders. If an employee has not placed an order, or if there is a department without employees, NULL values will be shown for the missing data.
You can also filter the results of a FULL OUTER JOIN by using the WHERE clause. For example, if you want to find all employees and their respective departments, and only show records where an employee is assigned to a department, you can use the following query:
SELECT Employees.Name, Departments.DepartmentName FROM Employees FULL OUTER JOIN Departments ON Employees.DepartmentID = Departments.DepartmentID WHERE Employees.DepartmentID IS NOT NULL;
This query will exclude employees who do not have a department assigned and departments that have no employees.
To make your queries cleaner, you can use table aliases. Here's an example of using aliases with a FULL OUTER JOIN:
SELECT e.Name, d.DepartmentName, o.OrderID FROM Employees AS e FULL OUTER JOIN Departments AS d ON e.DepartmentID = d.DepartmentID FULL OUTER JOIN Orders AS o ON e.EmployeeID = o.EmployeeID;
In this query, e is used as an alias for the Employees table, d for the Departments table, and o for the Orders table. This makes the query more readable.
The FULL OUTER JOIN is a powerful SQL operation that allows you to combine data from two tables, ensuring that all records from both tables are included in the result set. It is particularly useful when you need to ensure that no data from either table is left out, even if there are no matching records. Understanding how to use the FULL OUTER JOIN can help you perform more comprehensive queries and gain a fuller picture of your data.