What are Joins in PostgreSQL? PostgreSQL Join Types PostgreSQL Inner Join Theta Join
EQUI Join
Natural Join
Postgres Outer Join Postgres Left Outer Join Postgres Right Outer Join Full Outer Join in PostgreSQL Using pgAdmin
pgAdmin Inner Join How To Use Theta Join in PostgreSQL using pgAdmin EQUI Join
Natural Join
pgAdmin Simple Inner Join pgAdmin Outer Join Left Outer Join Right Outer Join Full Outer Join

PostgreSQL Inner Join

There are 3 types of Inner Joins in PostgreSQL:

Inner Joins Outer Joins

Theta join Natural join EQUI join

Theta Join

A theta join allows one to join two tables based on the condition that is represented by theta. Theta joins can work with all comparison operators. In most cases, the theta join is referred to as inner join. The theta join is the most basic type of JOIN. It will return all rows from the tables where the JOIN condition is satisfied. Syntax: Consider the following tables of the Demo database: Book:

Price:

We want to see the name of each book and the corresponding Price. We can run the following command: This will return the following:

Only 3 rows satisfied the join condition.

EQUI Join

The EQUI join provides us with a way of joining two tables based on primary key/foreign key relationship. For example: This will return the following:

Records have been returned from both tables based on the common columns, that is, the id column.

Natural Join

This type of join provides us with another way of writing an EQUI join. We can improve our previous example by adding the NATURAL keyword as shown below: This will return the following:

Only one id column has been returned. The NATURAL JOIN was able to note that the id column is common in the two tables. Only one was returned.

Postgres Outer Join

There are 3 types of Outer Joins in PostgreSQL:

Left Outer Join Right Outer Join Full Outer Join

Postgres Left Outer Join

The LEFT OUTER JOIN will return all rows in the table on the left-hand side and only the rows in the right-hand side table where the join condition has been satisfied. Syntax: We need to see the name of each book and the corresponding Price. We can run the following command: This returns the following:

All the 4 rows in the Book table have been returned. Only 3 rows from the Price table met the join condition. Hence they were returned. The last book has no corresponding price value.

Postgres Right Outer Join

The RIGHT OUTER JOIN returns all rows in the table on the right-hand side and rows in the table on the left-hand side where the join condition has been satisfied. Syntax: For example: This returns the following:

All the rows in the Price table have been returned. Only the rows in the Book table that met the join condition were returned. The 3rd row has no value for name since no match was found.

Full Outer Join in PostgreSQL

This type of JOIN will return all rows in the table on the left-hand side and all rows in the table on the right-hand side with nulls where the join condition is not satisfied. Syntax: For example: This returns the following:

All rows from all tables have been returned, with nulls where no match was found.

Using pgAdmin

The above tasks can be accomplished in pgAdmin as follows:

pgAdmin Inner Join

How To Use Theta Join in PostgreSQL using pgAdmin

Below are the steps to use Theta Join in Postgres using pgAdmin: Step 1) Login to your pgAdmin account Open pgAdmin and Login using your credentials Step 2) Create Demo database

From the navigation bar on the left- Click Databases. Click Demo.

Step 3) Type the query Type the below query in the query editor: Step 4) Execute the query Click the Execute button

It should return the following:

EQUI Join

Step 1) Login to your pgAdmin account. Step 2)

From the navigation bar on the left- Click Databases. Click Demo.

Step 3) Type the query in the query editor: Step 4) Click the Execute button.

It should return the following:

Natural Join

Step 1) Login to your pgAdmin account. Step 2)

From the navigation bar on the left- Click Databases. Click Demo.

Step 3) Type the query in the query editor: Step 4) Click the Execute button.

It should return the following:

pgAdmin Simple Inner Join

Step 1) Login to your pgAdmin account. Step 2)

From the navigation bar on the left- Click Databases. Click Demo.

Step 3) Type the query in the query editor: Step 4) Click the Execute button.

It should return the following:

pgAdmin Outer Join

Left Outer Join

Step 1) Login to your pgAdmin account. Step 2)

From the navigation bar on the left- Click Databases. Click Demo.

Step 3) Type the query in the query editor: Step 4) Click the Execute button.

It should return the following:

Right Outer Join

Step 1) Login to your pgAdmin account. Step 2)

From the navigation bar on the left- Click Databases. Click Demo.

Step 3) Type the query in the query editor: Step 4) Click the Execute button.

It should return the following:

Full Outer Join

Step 1) Login to your pgAdmin account. Step 2)

From the navigation bar on the left- Click Databases. Click Demo.

Step 3) Type the query in the query editor: Step 4) Click the Execute button.

It should return the following:

Summary

In PostgreSQL, we use JOINs when we need to retrieve values from more than one table. The INNER JOIN is the most basic type of JOIN. It returns all records where the specified JOIN condition was satisfied. The LEFT OUTER JOIN returns all rows in the left-hand table and only the rows in the other table where the join condition has been satisfied. The RIGHT OUTER JOIN returns all rows in the right-hand table and only rows in the other table where the join condition has been satisfied. This type of JOIN returns all rows in the left-hand table and all rows in the right-hand table with nulls where the join condition is not satisfied.

Download the Database used in this Tutorial