Full Join - SQL

In SQL, a full join, also known as a full outer join, is a type of join operation used to combine rows from two tables based on a related column, returning all rows from both tables, regardless of whether there is a match between them. If a row in one table has no corresponding match in the other table, NULL values are included for the columns from the non-matching table.

Syntax
SELECT columns
FROM table1
FULL JOIN table2
ON table1.column = table2.column;

Where

  • columns: Represents the columns to be retrieved from the tables.
  • table1 and table2: Specifies the tables to be joined.
  • FULL JOIN: Indicates that all rows from both tables will be returned, regardless of whether there is a match between them.
  • ON: Specifies the join condition, which establishes the relationship between the tables based on the specified columns.

Example

Consider two tables, "employees" and "departments," where you want to retrieve all employees and their corresponding departments. Some employees may not be assigned to any department, and some departments may not have any employees.

Example
SELECT employees.name, departments.department_name
FROM employees
FULL JOIN departments
ON employees.department_id = departments.department_id;

In this query, the FULL JOIN ensures that all rows from both the "employees" and "departments" tables are included in the result set. If there is no matching row in one table for a row in the other table, NULL values are included for the columns from the non-matching table.

Conclusion

In SQL, a full join is a powerful tool for combining data from multiple tables based on a related column while ensuring that all rows from both tables are included in the result set. By understanding its syntax and behavior, SQL developers can effectively utilize full joins to handle various data retrieval and analysis tasks.