YogeshChauhan . com

FULL OUTER JOIN in Postgres

in Postgres on October 24, 2020

The FULL OUTER JOIN keyword returns all records when there is a match in left (table1) or right (table2) table records.

If there are no matches at all, FULL OUTER JOIN can potentially return very large result-sets because it’s going to include everything from left and right tables!

The OUTER keyword doesn’t make any difference in the query. FULL OUTER JOIN and FULL JOIN are the same.

Basically, it looks like this.

FULL OUTER JOIN in PostgreSQL

The highlighted part from the image above is our query results when we apply FULL OUTER JOIN between 2 tables. 

Syntax:


SELECT a.column2, b.column2
FROM table a
FULL OUTER JOIN table b
ON a.column = b.column;

I am using alias as well in the syntax. You can use name of the table instead of alias. Alias makes it better to write the query. Learn more about alias in this post:

Column And Table Alias In Postgres

FULL OUTER JOIN Query Example 2 tables

I am using the database for all examples. It is available on my Github public repo


SELECT c.contact_name, o.order_date 
FROM customers c 
FULL OUTER JOIN orders o ON c.customer_id = o.customer_id

//Output
contact_name.       order_date
....
....
....
"Guillermo Fern├índez"    "1998-05-05"
"Jytte Petersen"    "1998-05-06"
"Michael Holz"    "1998-05-06"
"Laurence Lebihan"    "1998-05-06"
"Paula Wilson"    "1998-05-06"
"Marie Bertrand"    null
"Diego Roel"        null
...
...
832 rows

As we can see there were no matches from the right table, so the query result included the data from left table with null. In this case the FULL OUTER join behaves like LEFT join.

What will happen if we apply LEFT join to 3 tables?

INNER JOIN Query Example 3 tables


SELECT c.contact_name, o.order_date, od.product_id
FROM customers c 
FULL OUTER JOIN orders o ON c.customer_id = o.customer_id
FULL OUTER JOIN order_details od ON od.order_id = o.order_id

//Output
contact_name.    order_date.   count
...
...
...
"Paula Wilson"    "1998-05-06"    73
"Paula Wilson"    "1998-05-06"    75
"Paula Wilson"    "1998-05-06"    77
"Marie Bertrand"        null    null
"Diego Roel"            null    null
...
...
2157 rows

As we can see we are getting all the data possible from LEFT tables and for right tables, if there are not any match, it simply returns null. If we want to filter this data more, we can add an aggregate function and add GROUP BY clause.

As we saw in the previous query, in this query the FULL OUTER join behaves like LEFT join as well.

amazon

Most Read

#1 How to check if radio button is checked or not using JavaScript? #2 Solution to “TypeError: ‘x’ is not iterable” in Angular 9 #3 How to add Read More Read Less Button using JavaScript? #4 How to uninstall Cocoapods from the Mac OS? #5 How to Use SQL MAX() Function with Dates? #6 PHP Login System using PDO Part 1: Create User Registration Page

Recently Posted

Jun 16 What are Stored Procedures for SQL Server? Jun 16 What are Class Constants in PHP? Jun 15 A short basic guide on states in React Jun 15 How to define constants in PHP? Jun 15 How to define visibility for a property in PHP? Jun 15 How to use @if and @else in SCSS?

You might also like these

How to get category name using post id in WordPress?WordPressSolution to “Call to undefined function mysql_error()” in RevSlider WordPress PluginWordPressHow to add Google Map in WordPress using ACF?WordPressHow to insert Bootstrap 4 in Angular 9 app?AngularCheck if mixin is being called in a style rule or not in SCSSSCSSIs monitoring an employee’s work on a computer a desirable or undesirable activity?Misc