SQL full join
Created By: chatGPT
FULL JOIN in SQL, also known as FULL OUTER JOIN, is used to combine records from two tables, returning all records when there is a match in either left or right table records. If there are no matches, the result is filled with NULLs on the side that does not have a match. This is particularly useful when you want to retrieve the full data set from both tables, regardless of whether there is a corresponding row in the other table.
SELECT
a.*,
b.*
FROM
TableA a
FULL JOIN
TableB b
ON
a.common_column = b.common_column;
In this example, TableA and TableB are joined based on the column common_column. The resulting dataset will include:
- All rows from TableA regardless of whether there is a match in TableB.
- All rows from TableB even if there is no corresponding row in TableA.
SELECT
a.id,
a.name,
b.age,
b.city
FROM
Employees a
FULL JOIN
EmployeesDetails b
ON
a.id = b.employee_id;
Let’s say you have Employees and EmployeesDetails tables. If some employees do not have details and some details do not correspond to any employee, using a full join will help you to view all employees and all the details available along with corresponding values.
SELECT
e.name,
d.job_title
FROM
Employees e
FULL JOIN
JobDetails d
ON
e.id = d.employee_id;