Have you ever found yourself lost in the vast world of databases, wondering how to seamlessly combine information from different tables? Don't worry! A special kind of join that's really flexible is the SQL Full Outer JOIN. Let's explore it together to learn how it can improve the way you handle and manipulate data.
Before delving into the full outer join, it's crucial to grasp the fundamentals of SQL joins. In SQL, a join is a method for combining rows from two or more tables based on related columns. A full outer join takes this concept a step further, incorporating both left and right outer joins, ensuring that all records from both tables find a place in the result set.
Let's break down the syntax for performing a Full outer Join in SQL:
SELECT *
FROM ExampleTableOne
FULL OUTER JOIN ExampleTableTwo
ON ExampleTableOne.column_name = ExampleTableTwo.column_name;
Here, ExampleTableOne and ExampleTableTwo are the participating tables, and column_name is the linking column. The beauty of the full outer join lies in its inclusivity – it returns all rows, whether matched or unmatched, from both table
They're the same!
The complete name for this type of join is actually FULL OUTER JOIN. You might have come across SQL code examples where tables are joined using only FULL JOIN. This is because SQL recognizes both FULL OUTER JOIN and FULL JOIN. FULL JOIN is more commonly used simply because it's shorter. When you encounter it, it essentially means FULL OUTER JOIN. The term 'FULL' is implied because there's no other full join than a full outer join.
SELECT *
FROM ExampleTableOne
FULL JOIN ExampleTableTwo
ON ExampleTableOne.column_name = ExampleTableTwo.column_name;
Table 1: employee_detail
CREATE TABLE employee_detail (
emp_id INT NOT NULL,
emp_name VARCHAR (20) NOT NULL,
emp_salary INT NOT NULL,
dept_id CHAR (25),
PRIMARY KEY (emp_id)
);
emp_id | emp_name | emp_salary | dept_id |
---|---|---|---|
001 | John Doe | 60000 | 001 |
002 | Jane Smith | 55000 | 002 |
003 | Mike Johnson | 62000 | 001 |
004 | Emily Davis | 58000 | NULL |
Table 2: department_detail
CREATE TABLE department_detail (
dept_id INT NOT NULL,
dept_name VARCHAR (20) NOT NULL,
PRIMARY KEY (dept_id)
);
dept_id | dept_name |
---|---|
001 | HR |
002 | IT |
003 | Finance |
Imagine we have two tables, employee_details and department_details. By performing an Full Join on the common column EmpID, we can obtain a consolidated table with information such as Employee ID, Name, Salary, dept_name, and dept_id Status. Here's the SQL query:
-- Perform a FULL JOIN
SELECT
e.emp_id,
e.emp_name,
e.dept_id,
d.dept_name
FROM
employee_details e
FULL JOIN
department_details d ON e.dept_id = d.dept_id;
emp_id | emp_name | emp_salary | dept_id | dept_name |
---|---|---|---|---|
001 | John Doe | 60000 | 001 | HR |
002 | Jane Smith | 55000 | 002 | IT |
003 | Mike Johnson | 62000 | 001 | HR |
004 | Emily Davis | 58000 | NULL | NULL |
NULL | NULL | NULL | 003 | Finance |
In this example, all rows from the employee_detail and department_details tables are included in the result, and matching rows from the department_detail table are included based on the department id. If there is no match, the columns from the department_detail and employee_detail table will contain NULL values.