Account
Categories

FULL JOIN: Retrieve Data from Both Tables


FULL JOIN is an SQL operator used to retrieve all records from both the left and right tables.
It returns matching records where available and NULL for unmatched records in either table. In this process, the columns act as operands.

Syntax:

SELECT columns
FROM table1
LEFT JOIN table2
ON table1.common_column = table2.common_column

UNION
SELECT columns
FROM table1
RIGHT JOIN table2
ON table1.common_column = table2.common_column


Example:

Table1: Employees

Employee_id	  Name	    Department_id

1	         Mohan	          11
2	         Anaya	          12
3	         Babita	          13

Table2: Departments

Department_id	       Department_name

11	                HR
12                      IT
14	                Finance
   

SQL Query:


SELECT Employees.name, Departments.department_name
FROM Employees
LEFT JOIN Departments
ON Employees.department_id = Departments.department_id

UNION

SELECT Employees.name, Departments.department_name
FROM Employees
RIGHT JOIN Departments
ON Employees.department_id =Departments.department_id;


Output:

Name                	    Department_name

Mohan	                     HR
Anaya                        IT
NULL	                    Finance
Babita  	            NULL