Updated May 6, 2023
Introduction to PostgreSQL Full join
PostgreSQL full outer join returns all rows from the left table as well as a right table; it will put null values when full outer join condition was not satisfied while joining table using FULL OUTER JOIN first it will join using inner join, the combination of left and right join is known as full outer join, the full join is also known as full outer join in PostgreSQL, It is used to join data from multiple tables, we have used this join whenever the need to retrieve data from one or more table.
Syntax
Select column1 …. columnN from table_name1 FULL JOIN table_name2 on table_name1.column = table_name2.column;
SELECT (*) FROM table_name1 FULL JOIN table_name2 on table_name1.column = table_name2.column;
Below is the parameter description of the above syntax are as follows.
- Column1 to columnN: Column used in full outer join to retrieve data from tables.
- Table name: Table name used in full join to retrieve data from the table.
- Full join: Full join is used to join two tables.
- Asterisk (*): Asterisk (*) defines all table columns to retrieve data from the table.
How Full Join works?
A full outside join is another name for it. It is crucial to the PostgreSQL join type.Both the left table’s and the right table’s whole rows will be returned.It will put null in the specified row when the full outer join condition was not satisfied. While joining the table using FULL OUTER JOIN or FULL JOIN first, it will join be using an inner join. The combination of left and right join is known as the full outer join.
Below is the image representation of the full outer join.
Using PostgreSQL full join, we have possible to combine the select and join statement into one statement. We can join a single table with multiple names as an alias in PostgreSQL. It will retrieve data according to the condition.
Full Join or full outer join is very useful in this to retrieve data from multiple tables.
Below are the types of join in PostgreSQL.
- CROSS JOIN
- INNER JOIN
- LEFT OUTER JOIN OR LEFT JOIN
- RIGHT OUTER JOIN OR RIGHT JOIN
- FULL OUTER JOIN OR FULL JOIN
Full outer join in PostgreSQL will return all rows from the left table and all rows from the right table.
PostgreSQL full join, also known as full outer join, returns all rows from both the left and right tables. If the full join condition is not met, null values are used. The result set contains all matching rows as well as any non-matching rows, with columns populated from both joined tables. This type of join is useful for selecting data from multiple tables in a single query.
Retrieving data from multiple tables using PostgreSQL full join, we don’t need to have any constraints on the first table and the second table. This will work without any constraints to display the output from the table.
If suppose ABC table has full join with PQR table with a join condition. In this case, full join or full outer join will show below outputs values from ABC and PQR table. It will take all values from ABC as well as the PQR table.
Examples
We have using employee and department tables to describe the example of full join.
1. Employee Table
Below is the example of an employee table is as follows.
CREATE TABLE Employee ( emp_id INT NOT NULL, emp_name character(10) NOT NULL, emp_address character(20) NOT NULL, emp_phone character(14), emp_salary INT NOT NULL, PRIMARY KEY (emp_name));
INSERT INTO Employee (emp_id, emp_name, emp_address, emp_phone, emp_salary) VALUES (1, 'ABC', 'Pune', '1234567890', 20000);
INSERT INTO Employee (emp_id, emp_name, emp_address, emp_phone, emp_salary) VALUES (1, 'PQR', 'Pune', '1234567890', 20000);
INSERT INTO Employee (emp_id, emp_name, emp_address, emp_phone, emp_salary) VALUES (1, 'XYZ', 'Mumbai', '1234567890', 35000);
INSERT INTO Employee (emp_id, emp_name, emp_address, emp_phone, emp_salary) VALUES (2, 'BBS', 'Mumbai', '1234567890', 45000);
INSERT INTO Employee (emp_id, emp_name, emp_address, emp_phone, emp_salary) VALUES (2, 'RBS', 'Delhi', '1234567890', 50000);
2. Department Table
Below is the example of the department table is as follows.
CREATE TABLE department ( dept_name character(10) NOT NULL, dept_id int NOT NULL, dept_code varchar(10));
INSERT INTO department (dept_name, dept_id, dept_code) VALUES ('IT', 101, 'IT101');
INSERT INTO department (dept_name, dept_id, dept_code) VALUES ('IT', 101, 'IT101');
INSERT INTO department (dept_name, dept_id, dept_code) VALUES ('HR', 102, 'HR102');
INSERT INTO department (dept_name, dept_id, dept_code) VALUES ('HR', 102, 'HR102');
INSERT INTO department (dept_name, dept_id, dept_code) VALUES ('Sales', 103, 'HR103');
3. PostgreSQL Full Join or Full Outer Join
The below example shows full join in PostgreSQL is as follows.
select * from employee FULL JOIN department ON employee.emp_id = department.dept_id;
select emp_id, emp_name from employee FULL JOIN department ON employee.emp_id = department.dept_id;
We must use a full join to join the employee and department tables in the example above. In the first illustration, every record from the right table and every record from the employee table will be shown.
In the second example, we have to retrieve data from the emp_id and emp_name column. The full outer join condition will not satisfied in the second example because it will return a null value. The above example retrieves different outputs from the same table using PostgreSQL full outer join. In our example, the employee table is the first table, and the department table is the second table.
We are joining the table using the employee table as the emp_id column and department tables as the dept_id column. The second example of a full outer join is useful when we select data from one table and don’t have a match from the second table, and also, we are retrieving null values from the second table.
Conclusion
PostgreSQL also refers to this type of join as a full outer join. In PostgreSQL, you can use a full join or a full outer join to retrieve all rows from both the left and right tables. It is essential in PostgreSQL.
Recommended Articles
We hope that this EDUCBA information on “PostgreSQL Full Join” was beneficial to you. You can view EDUCBA’s recommended articles for more information.