Dear readers of our blog, we'd like to recommend you to visit the main page of our website, where you can learn about our product SQLS*Plus and its advantages.
 
SQLS*Plus - best SQL Server command line reporting and automation tool! SQLS*Plus is several orders of magnitude better than SQL Server sqlcmd and osql command line tools.
 

REQUEST COMPLIMENTARY SQLS*PLUS LICENCE

Enteros UpBeat offers a patented database performance management SaaS platform. It proactively identifies root causes of complex revenue-impacting database performance issues across a growing number of RDBMS, NoSQL, and deep/machine learning database platforms. We support Oracle, SQL Server, IBM DB2, MongoDB, Casandra, MySQL, Amazon Aurora, and other database systems.

PostgreSQL Join

9 September 2020

PostgreSQL Join

PostgreSQL JOIN is used to extract data from multiple tables. PostgreSQL JOIN is executed whenever two or more tables are combined in an SQL statement.

There are different types of PostgreSQL connections:

  • PostgreSQL INNER JOIN (or sometimes called a simple connection)
  • PostgreSQL LEFT OUTER JOIN (or sometimes called LEFT JOIN)
  • PostgreSQL RIGHT OUTER JOIN (or sometimes called RIGHT JOIN)
  • PostgreSQL FULL OUTER JOIN (or sometimes called FULL JOIN)

So, let’s discuss the JOIN syntax in PostgreSQL, take a look at the visual illustrations of JOIN in PostgreSQL, and look at the JOIN examples.

INNER JOIN (simple connection)

Most likely, you have already written a query that uses PostgreSQL INNER JOIN. This is the most common type of connection. INNER JOIN returns all rows from multiple tables where the connection condition is met.

The syntax for INNER JOIN in PostgreSQL

SELECT columns
FROM table1
INNER JOIN table2
ON table1.column = table2.column;

Visual Illustration

PostgreSQL Join

On this visual diagram, PostgreSQL INNER JOIN returns the shaded area:
PostgreSQL INNER JOIN will return records where table1 and table2 intersect.

Here is an example of INNER JOIN PostgreSQL

SELECT suppliers.supplier_id, suppliers.supplier_name, orders.order_date
FROM suppliers
INNER JOIN orders
ON.supplier_id = orders.supplier_id;

This PostgreSQL INNER JOIN example will return all rows from the suppliers and orders tables where the corresponding value supplier_id is present in the suppliers and orders tables.

Let’s take a look at some data to explain how internal connections work:
We have a table of suppliers with two fields (supplier_id and supplier_name).

It contains the following data:

supplier_idsupplier_name
10000IBM
10001Hewlett Packard
10002Microsoft
10003NVIDIA

 

We have another table called orders with three fields (order_id, supplier_id, and order_date). It contains the following data:

order_idsupplier_idorder_date
5001251000010.04.2019
5001261000120.04.2019
5001271000430.04.2019

 

If we run the PostgreSQL SELECT operator (which contains INNER JOIN) below:

SELECT suppliers.supplier_id, suppliers.supplier_name, orders.order_date
FROM suppliers
INNER JOIN orders
ON.supplier_id = orders.supplier_id;

Our result set will look like this:

supplier_idnameorder_date
10000IBM10.04.2019
10001Hewlett Packard20.04.2019

 

Rows for ‘Microsoft’ and ‘NVIDIA’ from the supplier table will be omitted because 10002 and 10003 supplier_id do not exist in both tables. Row for 500127 (order_id) from the orders table will be omitted because supplier_id 10004 does not exist in the supplier’s table.

Old syntax

In conclusion, it’s worth mentioning that the INGER JOIN PostgreSQL example above can be rewritten using the older implicit syntax as follows (but we still recommend using the INNER JOIN keyword syntax):

SELECT suppliers.supplier_id, suppliers.supplier_name, orders.order_date
FROM suppliers, orders
WHERE suppliers.supplier_id = orders.supplier_id;
LEFT OUTER JOIN

Another type of connection is called PostgreSQL LEFT OUTER JOIN. This type of connection returns all rows from tables with a left-hand connection specified in the ON condition, and only those rows from another table where the fields to be joined are equal (the connection condition is fulfilled).

The syntax for PostgreSQL LEFT OUTER JOIN

SELECT columns
FROM table1
LEFT OUTER JOIN table2
ON table1.column = table2.column;

Visual Illustration

On this visual diagram, PostgreSQL LEFT OUTER JOIN returns the shaded area:

PostgreSQL LEFT OUTER JOIN

PostgreSQL LEFT OUTER JOIN will return all records from table1 and only those records from table2 that intersect with table1.

An example of PostgreSQL LEFT OUTER JOIN

SELECT suppliers.supplier_id, suppliers.supplier_name, orders.order_date
FROM suppliers
LEFT OUTER JOIN orders
ON.supplier_id = orders.supplier_id;

In this example, the LEFT OUTER JOIN will return all rows from the employee’s table and only those rows from the orders table where the combined fields are equal.

PostgreSQL: Inner Joins | Course

 
Tags: , , , ,

MORE NEWS

 

Preamble​​NoSql is not a replacement for SQL databases but is a valid alternative for many situations where standard SQL is not the best approach for...

Preamble​​MongoDB Conditional operators specify a condition to which the value of the document field shall correspond.Comparison Query Operators $eq...

5 Database management trends impacting database administrationIn the realm of database management systems, moreover half (52%) of your competitors feel...

The data type is defined as the type of data that any column or variable can store in MS SQL Server. What is the data type? When you create any table or...

Preamble​​MS SQL Server is a client-server architecture. MS SQL Server process starts with the client application sending a query.SQL Server accepts,...

First the basics: what is the master/slave?One database server (“master”) responds and can do anything. A lot of other database servers store copies of all...

Preamble​​Atom Hopper (based on Apache Abdera) for those who may not know is an open-source project sponsored by Rackspace. Today we will figure out how to...

Preamble​​MongoDB recently introduced its new aggregation structure. This structure provides a simpler solution for calculating aggregated values rather...

FlexibilityOne of the most advertised features of MongoDB is its flexibility.  Flexibility, however, is a double-edged sword. More flexibility means more...

Preamble​​SQLShell is a cross-platform command-line tool for SQL, similar to psql for PostgreSQL or MySQL command-line tool for MySQL.Why use it?If you...

Preamble​​Writing an application on top of the framework on top of the driver on top of the database is a bit like a game on the phone: you say “insert...

Preamble​​Oracle Coherence is a distributed cache that is functionally comparable with Memcached. In addition to the basic function of the API cache, it...

Preamble​​IBM pureXML, a proprietary XML database built on a relational mechanism (designed for puns) that offers both relational ( SQL / XML ) and...

  What is PostgreSQL array? In PostgreSQL we can define a column as an array of valid data types. The data type can be built-in, custom or enumerated....

Preamble​​If you are a Linux sysadmin or developer, there comes a time when you need to manage an Oracle database that can work in your environment.In this...

Preamble​​Starting with Microsoft SQL Server 2008, by default, the group of local administrators is no longer added to SQL Server administrators during the...