Updated May 12, 2023
Introduction to PostgreSQL FULL OUTER JOIN
PostgreSQL Full Outer Join is a join that gives the data of both the left table and the right table. This means that when we join two tables and tend to take data from them, this join takes records from both tables. The system sets non-matching values to null values for every column of the table that does not have a matching row. This holds true for the left table and right table. The system marks values in the left table as null if they do not match the right table, and similarly, it marks values in the right table as null if they do not match the left table.
Syntax:
SELECT table1.columns, table2.columns
FROM table1
FULL OUTER JOIN table2
ON table1.common_filed = table2.common_field;
The syntax takes both tables that need to be matched. The system selects the required columns from both tables. The system uses a full outer join to join the first table with the other table. The keyword ‘OUTER’ is optional here. The system specifies the condition on which to match these rows after this. The system performs the join based on matching rows. The rows that match are taken first. Then the rows from the first table which do not match the condition have null values. The system takes null values if there are no matching rows for the second table.
How PostgreSQL FULL OUTER JOIN works?
With the help of the above Venn diagram, we can get an idea of how the full outer join works. The circle A signifies the left table. The circle B signifies the right table. The area where both circles intersect is the one that signifies rows that are matching in both tables; whenever the below statement is run, the compiler processes as below.
Code:
SELECT A.columns, B.columns
FROM A
FULL OUTER JOIN B
ON A.common_filed = B.common_field;
When this query is fired, then first, all columns of tables A and B are selected. The left outer join condition is checked, and the rows that match this condition are chosen. This signifies the intersection area of the Venn diagram. These become a part of the result set. Then it will take all records from the Left table, A.
The corresponding rows from the right table, that is, B, will take null values as they will not be matching. Similarly, after the left table, all rows from the right table, B, will be taken, and corresponding rows from the left table, A, will take null values as they will not be matching. The final result of the query will be all rows from both tables. The matching records are all records from the left table and all from the right table with null values for rows that do not match.
Examples of PostgreSQL FULL OUTER JOIN
Given below are the examples:
Example #1
We have two tables, ODS_CLICC_IPTV.REGIONS_LIVE and ODS_CLICC_IPTV.REGIONS_LIVE_STG. The table has columns like REGION_ID, REGION_NAME, STATUS, etc.
Code:
select * from ODS_CLICC_IPTV.REGIONS_LIVE_STG;
Output:
The above table shows the different columns in the specified stage table – ODS_CLICC_IPTV.REGIONS_LIVE_STG.
Code:
select * from ODS_CLICC_IPTV.REGIONS_LIVE;
Output:
The above table shows the different columns in the specified main table – ODS_CLICC_IPTV.REGIONS_LIVE.
We now apply FULL outer join on the above two tables. The rows of the stage table are 2 rows, and that of the main table is 34 rows.
Code:
select A.REGION_ID,B.REGION_PROXY_CODE from ODS_CLICC_IPTV.REGIONS_LIVE A
FULL OUTER JOIN ODS_CLICC_IPTV.REGIONS_LIVE_STG B
ON A.REGION_ID=B.REGION_ID;
The above code gets all rows as the results. If you check the output of the above query, the number of rows here is 35 rows. The system first takes all rows from the left table A and then selects the common row matching row 34. In the end, the row from the right table is taken.
The system marks all rows from the left table with no corresponding value in the right table as null. Similarly, the last row from the right table does not have a corresponding REGION_ID. Hence it is marked as null here. All rows from table A, common row from tables A and B, and one row from Table B make the count as 35 rows.
Output:
Example #2
Code:
CREATE TABLE COMPANY1(
ID INT PRIMARY KEY NOT NULL,
NAME TEXT NOT NULL,
AGE INT NOT NULL,
ADDRESS CHAR(50),
SALARY REAL
);
SELECT * FROM company1;
Output:
We will create another table department.
Code:
CREATE TABLE DEPARTMENT(
ID INT PRIMARY KEY NOT NULL,
DEPT CHAR(50) NOT NULL,
EMP_ID INT NOT NULL
);
SELECT * FROM department;
Output:
Above are the structures of the two tables created.
If you check in this example, an ‘id’ column is common between these two tables. Let us join this two table and check the result that we get.
Code:
select C.id, EMP_ID, NAME, AGE, DEPT from company1 C
FULL OUTER JOIN department D
ON C.ID=D.ID;
Output:
Here all columns are matching for the two tables. The common column has similar values in both tables. Hence there are no null values in the result. The result is as good as the inner join result. Though we have used FULL OUTER JOIN, the result is similar to Inner Join. The number of rows here is hence similar to the number of rows in the tables.
Let us now insert another row in the department table and run the query again.
Code:
INSERT INTO DEPARTMENT (ID, DEPT, EMP_ID)
VALUES (4, 'ADMIN', 8 );
After inserting this row in the department table, the result will change.
Code:
select C.id, EMP_ID, NAME, AGE, DEPT from company1 C
FULL OUTER JOIN department D
ON C.ID=D.ID;
Output:
If you see the above snippet, you will see that there are null values present for all the company table values. On the EMPID and Dept are populated. The system populates the resulting value as null because the select statement takes the ID column from the company table, and there is no ID of 4.
Conclusion
The left outer join hence helps in combining the results of two tables. If there are no matching records, even then, it will take records from both tables and populate null values wherever applicable.
Recommended Articles
We hope that this EDUCBA information on “PostgreSQL FULL OUTER JOIN” was beneficial to you. You can view EDUCBA’s recommended articles for more information.