EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 360+ Courses All in One Bundle
  • Login

PostgreSQL FULL OUTER JOIN

Home » Data Science » Data Science Tutorials » PostgreSQL Tutorial » PostgreSQL FULL OUTER JOIN

PostgreSQL FULL OUTER JOIN

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:

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

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?

postgreSQL FULL OUTER JOIN

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:

PostgreSQL FULL OUTER JOIN 2

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:

different columns in the specified main table

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:

PostgreSQL FULL OUTER JOIN 4JPG

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:

select from company 1

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:

another table department

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:

‘id’ column which is common between these two table

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:

inserting row in the department table

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 –

  1. PostgreSQL Boolean
  2. PostgreSQL RANK()
  3. PostgreSQL NOT NULL
  4. PostgreSQL Notify

All in One Data Science Bundle (360+ Courses, 50+ projects)

360+ Online Courses

50+ projects

1500+ Hours

Verifiable Certificates

Lifetime Access

Learn More

0 Shares
Share
Tweet
Share
Primary Sidebar
PostgreSQL Tutorial
  • Joins
    • Joins in PostgreSQL
    • PostgreSQL Inner Join
    • PostgreSQL Outer Join
    • LEFT OUTER JOIN in PostgreSQL
    • PostgreSQL FULL OUTER JOIN
    • PostgreSQL LEFT JOIN
    • PostgreSQL Full Join
    • PostgreSQL Cross Join
    • PostgreSQL NATURAL JOIN
    • PostgreSQL UPDATE JOIN
  • Basic
    • What is PostgreSQL
    • PostgreSQL Features
    • How to Install PostgreSQL
    • PostgreSQL Versions
    • PostgreSQL Architecture
    • PostgreSQL GUI
    • PostgreSQL Variables
    • PostgreSQL Data Types
    • PostgreSQL NOT NULL
    • PostgreSQL Integer
    • PostgreSQL Boolean
    • PostgreSQL NULLIF
    • PostgreSQL Administration
    • PostgreSQL Commands
    • PostgreSQL Operators
    • PostgreSQL IN Operator
  • Control Statement
    • PostgreSQL IF Statement
    • PostgreSQL if else
    • PostgreSQL CASE Statement
    • PostgreSQL LOOP
    • PostgreSQL For Loop
    • PostgreSQL While Loop
  • Queries
    • PostgreSQL Queries
    • PostgreSQL WHERE Clause
    • PostgreSQL WITH Clause
    • PostgreSQL ORDER BY
    • PostgreSQL ORDER BY Random
    • PostgreSQL GROUP BY
    • PostgreSQL group_concat
    • PostgreSQL HAVING
    • PostgreSQL Recursive Query
  • Advanced
    • PostgreSQL Schema
    • Postgres List Schemas
    • PostgreSQL VARCHAR
    • Array in PostgreSQL
    • PostgreSQL DDL
    • PostgreSQL List Users
    • Postgres Default User
    • Postgres add user
    • PostgreSQL log_statement
    • PostgreSQL String Functions
    • PostgreSQL Compare Strings
    • PostgreSQL Text Search
    • PostgreSQL TEXT
    • PostgreSQL String Array
    • PostgreSQL Constraints
    • PostgreSQL UNIQUE Constraint
    • PostgreSQL INTERSECT
    • PostgreSQL Like
    • Cursors in PostgreSQL
    • PostgreSQL UNION ALL
    • Indexes in PostgreSQL
    • PostgreSQL Index Types
    • PostgreSQL REINDEX
    • PostgreSQL UNIQUE Index
    • PostgreSQL Clustered Index
    • PostgreSQL DROP INDEX
    • PostgreSQL DISTINCT
    • PostgreSQL FETCH
    • PostgreSQL RAISE EXCEPTION
    • PostgreSQL Auto Increment
    • Sequence in PostgreSQL
    • Wildcards in PostgreSQL
    • PostgreSQL Subquery
    • PostgreSQL Alias
    • PostgreSQL LIMIT
    • PostgreSQL Limit Offset
    • PostgreSQL LAG()
    • PostgreSQL Table
    • Postgres Show Tables
    • PostgreSQL Describe Table
    • PostgreSQL Lock Table
    • PostgreSQL ALTER TABLE
    • Postgres Rename Table
    • Postgres DROP Table
    • PostgreSQL Functions
    • PostgreSQL Math Functions
    • PostgreSQL Window Functions
    • Aggregate Functions in PostgreSQL
    • PostgreSQL Primary Key
    • Foreign Key in PostgreSQL
    • PostgreSQL Procedures
    • PostgreSQL Stored Procedures
    • PostgreSQL Views
    • PostgreSQL Materialized Views
    • Postgres Create View
    • PostgreSQL Triggers
    • PostgreSQL DROP TRIGGER
    • PostgreSQL Date Functions
    • PostgreSQL TO_DATE()
    • PostgreSQL Timestamp
    • PostgreSQL CURRENT_TIMESTAMP()
    • PostgreSQL Notify
    • PostgreSQL RANK()
    • PostgreSQL Select
    • PostgreSQL Average
    • PostgreSQL DATE_PART()
    • PostgreSQL EXECUTE
    • PostgreSQL COALESCE
    • PostgreSQL EXTRACT()
    • PostgreSQL Sort
    • PostgreSQL TO_CHAR
    • PostgreSQL Interval
    • PostgreSQL Number Types
    • PostgreSQL ROW_NUMBER
    • Alter Column in PostgreSQL
    • PostgreSQL Identity Column
    • PostgreSQL SPLIT_PART()
    • PostgreSQL CONCAT()
    • PostgreSQL replace
    • PostgreSQL TRIM()
    • PostgreSQL MAX
    • PostgreSQL DELETE
    • PostgreSQL Float
    • PostgreSQL OID
    • PostgreSQL log
    • PostgreSQL REGEXP_MATCHES()
    • PostgreSQL MD5 
    • PostgreSQL NOW()
    • PostgreSQL RANDOM
    • PostgreSQL round
    • PostgreSQL Trunc()
    • PostgreSQL TIME
    • PostgreSQL IS NULL
    • PostgreSQL CURRENT_TIME
    • PostgreSQL MOD()
    • Postgresql Count
    • PostgreSQL Datetime
    • PostgreSQL MIN()
    • PostgreSQL age()
    • PostgreSQL enum
    • PostgreSQL OR
    • PostgreSQL Wal
    • PostgreSQL NOT IN
    • PostgreSQL SET
    • PostgreSQL Current Date
    • PostgreSQL Compare Date
    • PostgreSQL SERIAL
    • PostgreSQL Database
    • PostgreSQL Clone Database
    • PostgreSQL Copy Database
    • PostgreSQL Restore Database
    • PostgreSQL DROP DATABASE
    • PostgreSQL ALTER DATABASE
    • Postgres DROP Database
    • Postgres Dump Database
    • PostgreSQL OFFSET
    • PostgreSQL GRANT
    • PostgreSQL COMMIT
    • PostgreSQL ROLLUP
    • PostgreSQL JSON
    • EXPLAIN ANALYZE in PostgreSQL
    • PostgreSQL Temporary Table
    • PostgreSQL cluster
    • PostgreSQL Replication
    • PostgreSQL Logical Replication
    • PostgreSQL flush privileges
    • PostgreSQL Tablespaces
    • CAST in PostgreSQL
    • PostgreSQL CTE
    • hstore in PostgreSQL
    • PostgreSQL DECODE()
    • PostgreSQL Vacuum
    • PostgreSQL EXCLUDE
    • Postgres Change Password
    • Postgres Delete Cascade
    • PostgreSQL EXCEPT
    • PostgreSQL Roles
    • PostgreSQL Link
    • PostgreSQL Partition
    • PostgreSQL column does not exist
    • PostgreSQL Log Queries
    • PostgreSQL escape single quote
    • PostgreSQL Query Optimization
    • PostgreSQL Character Varying
    • PostgreSQL Transaction
    • PostgreSQL Extensions
    • PostgreSQL Import CSV
    • PostgreSQL Client
    • PostgreSQL caching
    • PostgreSQL JDBC Driver
    • PostgreSQL Interview Questions
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Database Management
  • Machine Learning
  • All Tutorials
Certification Courses
  • All Courses
  • Data Science Course - All in One Bundle
  • Machine Learning Course
  • Hadoop Certification Training
  • Cloud Computing Training Course
  • R Programming Course
  • AWS Training Course
  • SAS Training Course

© 2020 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

*Please provide your correct email id. Login details for this Free course will be emailed to you
Book Your One Instructor : One Learner Free Class

Let’s Get Started

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

Special Offer - All in One Data Science Bundle (360+ Courses, 50+ projects) Learn More