Full join return rows when there are matching rows in any one of the tables. This means it returns all the rows from the left-hand side table and all the rows from the right-hand side table.
In SQL, there is no specific “FULL JOIN” keyword. However, you can achieve a full join using a combination of LEFT JOIN, RIGHT JOIN, and UNION clauses. A full join combines the results of both LEFT JOIN and RIGHT JOIN, including unmatched rows from both tables.
Here’s an example using the following two tables:
Table A: employees
employee_id | employee_name
------------|---------------
1 | Alice
2 | Bob
3 | Charlie
Table B: departments
department_id | department_name
--------------|-----------------
1 | Sales
4 | Marketing
A full join of these two tables would include all rows from both tables, filling in NULL values for columns where there are no matches. The SQL query for a full join would be:
SELECT *
FROM employees
FULL JOIN departments ON employees.employee_id = departments.department_id;
The result would look something like this:
employee_id | employee_name | department_id | department_name
------------|---------------|---------------|-----------------
1 | Alice | 1 | Sales
2 | Bob | NULL | NULL
3 | Charlie | NULL | NULL
NULL | NULL | 4 | Marketing
In this example, all rows from both tables are included, and NULL values are filled in where there are no matches. Note that the syntax for a full join may vary slightly depending on the specific database system you are using.