Introduction to PostgreSQL FULL OUTER JOIN
PostgreSQL Full Outer Join is a join that gives the data of both the left table and right table. This means that when we join two tables and tend to take data from them, this join takes records from both the tables. The values which are not matching are set to null values for every column of the table which does not have a matching row. This holds true for the left table and right table. If the Left table values do not match with the right table, they will be marked as null, and similarly, if they do not match the right table, they will be null in the right table.
Syntax:
SELECT table1.columns, table2.columns
FROM table1
FULL OUTER JOIN table2
ON table1.common_filed = table2.common_field;
This syntax takes both tables, which are to be matched. It selects the columns which are required from both tables. The first table here is joined with the other table by making use of the full outer join. The keyword ‘OUTER’ is optional here. After this, the condition on which these rows are to be matched is given. The join is done 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. Similarly, if there are no matching rows for the second table, then null values are taken for the same.
How PostgreSQL FULL OUTER JOIN works?
With the help of the above Venn diagram, we can see 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, that is, 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 that is B will be taken, and corresponding rows from the left table that is 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, all records from the left table and all records from the right table with nulls 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 is showing 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 is showing 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. Here all rows from the left table that is A are taken first. Then the common row, which is matching at row number 34, is taken. In the end, the row from the right table is taken.
All rows which are from the left table and do not have any corresponding value in the right table are marked 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, there is an ‘id’ column that 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. As the select statement is taking the ID column from the company table and there is no ID as 4 in the company table, the value is populated as null.
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
This is a guide to PostgreSQL FULL OUTER JOIN. Here we discuss the introduction, how PostgreSQL FULL OUTER JOIN works and examples. You may also have a look at the following articles to learn more –