SQL OUTER JOIN
Outer Join in SQL is a method used to merge rows from multiple tables, accommodating instances where there might not be corresponding rows in one or more of those tables. This operation ensures that every row from at least one table (referred to as the "outer" table) is represented in the output, together with any matching rows from the other table(s) involved.
Syntax
The syntax of the outer join in SQL is as follows:
We select columns from the tableA that are to be part of the outer join. Next, we mention FULL OUTER JOIN and tableB. ON defines common criteria for the join; say, one column is common in both the relations, and join is performed on it. Also, we can mention the WHERE condition if we wish to filter some records.
The outer join of two tables tableA and tableB can be represented as follows:
Note: The values common in tableA and tableB appear only once in the outer join.
Demo Database
Employees Table This table will hold information on employees, including their unique ID, name, and the department they're assigned to.
EmployeeID | EmployeeName | DepartmentID |
---|---|---|
1 | Lara Quent | 101 |
2 | Kiven Morat | 102 |
3 | Jasper Elto | 103 |
4 | Nila Devar | NULL |
Departments Table This table lists the departments within the organization, including their unique ID and name.
DepartmentID | DepartmentName |
---|---|
101 | Tech Support |
102 | Logistics |
103 | Public Relations |
104 | Research & Development |
Types of OUTER JOIN in SQL
LEFT OUTER JOIN
In SQL, a left outer join delivers every row from the initial, or left, table, alongside corresponding rows from the subsequent, or right, table. If there exists a row in the left table without a counterpart in the right table, the resulting values for the right table's rows are defaulted to zero. These placeholders can subsequently be adjusted as needed.
Syntax
The syntax of left join is the same as full join, except there will be a LEFT keyword instead of FULL.
Left join keywords are used to perform left outer join. ON is used to identify the columns in each table to be linked.
RIGHT OUTER JOIN
The right outer join operation in SQL returns all the rows from the right, i.e., the second relation and only matching rows from the left relation. If some records from the right table don't match rows from the left table, the null value is inserted in the columns of the left table. The records that don't match in the left table are discarded.
Syntax
FULL OUTER JOIN in SQL
The full outer join operation returns all the records from both the relations, irrespective of the match from tableA or tableB.
Syntax
Here we use the FULL keyword to perform a full outer join of two tables.
OUTER JOIN SQL Example
To demonstrate the OUTER JOIN in SQL using the tables we've created (Employees and Departments), Let's illustrate how to perform a LEFT JOIN, RIGHT JOIN, and FULL OUTER JOIN.
LEFT OUTER JOIN Example
This query retrieves all employees and their respective department names, including those without a department.
Query:
Output:
EmployeeName | DepartmentName |
---|---|
Lara Quent | Tech Support |
Kiven Morat | Logistics |
Jasper Elto | Public Relations |
Nila Devar | NULL |
RIGHT OUTER JOIN Example
This query shows all departments, including those without any employees, and lists any employees that are part of those departments.
Query:
Output:
EmployeeName | DepartmentName |
---|---|
Lara Quent | Tech Support |
Kiven Morat | Logistics |
Jasper Elto | Public Relations |
NULL | Research & Development |
FULL OUTER JOIN Example
A FULL OUTER JOIN combines the results of both LEFT JOIN and RIGHT JOIN, showing all records from both tables with matching records from either side where available.
Query:
Expected Output:
EmployeeName | DepartmentName |
---|---|
Lara Quent | Tech Support |
Kiven Morat | Logistics |
Jasper Elto | Public Relations |
Nila Devar | NULL |
NULL | Research & Development |
Conclusion
- Outer join in SQL are essential for combining rows from two or more tables, filling gaps where no direct matches exist, ensuring a comprehensive dataset is always retrieved.
- Grasping the syntax for Left, Right, and Full Outer join in SQL enables users to apply the correct type of join for their specific data retrieval needs, enhancing query flexibility.
- Familiarity with different types of Outer Joins (LEFT, RIGHT, FULL) allows for tailored queries that precisely meet reporting and analysis requirements.
- Mastery of Outer Joins enriches a user's SQL toolkit, opening doors to advanced data manipulation and insights that would be challenging to obtain otherwise.