Introduction to PostgreSQL NATURAL JOIN
PostgreSQL NATURAL JOIN In simple terms, the join is where two or more tables are joined, and as a result, we will get the new set of rows for combined columns requested for each table. A joint operation creates an implicit join clause on the common columns for the tables that we have defined in the join clause. A Join operation actually creates a temporary table for a set of rows to work on two or more tables. The tables defined in the join clause should have at least one common column, and the common columns should have a relation between them.
SELECT * FROM table_name_1 NATURAL [INNER, LEFT, RIGHT] JOIN table_name_2;
Explanation: The table_name_1 and table_name_2 are the two tables on which we perform a joining to get the combined result for the columns having the same name in both tables. The table_name_1 is generally read as a left (first) table, and The table_name_2 is generally read right (second) table.
Joins in PostgreSQL NATURAL JOIN
NATURAL JOIN operation can be any of the following:
- Inner join
- Left join
- Right join
The PostgreSQL uses the INNER JOIN by default if we do not explicitly define the name of join as INNER JOIN, LEFT JOIN, or RIGHT JOIN.
How NATURAL JOIN works in PostgreSQL?
Suppose we create a SQL statement having an asterisk (*) instead of column names in the SELECT clause with the NATURAL JOIN operation. In that case, the columns will be considered in the following order for an asterisk (*) :
- All the common columns are from the left (first) table and right (second) table.
- Every column in the left (first) table which is not common with the right (second) table columns.
- Every column in the second (right) table which is not common with the left (first) table columns.
In PostgreSQL, the NATURAL JOIN is an INNER JOIN where we combine all columns with the same name in both tables.
Examples of implementing NATURAL JOIN in PostgreSQL
Let’s create two tables named’ transaction’ and ‘invoices’ to understand the PostgreSQL NATURAL JOIN examples.
The following CREATE TABLE statements will create the transaction and invoices table.
CREATE TABLE transaction ( transaction_id serial PRIMARY KEY, transaction_data VARCHAR (256) NOT NULL ); CREATE TABLE invoices ( invoice_id serial PRIMARY KEY, transaction_id INT NOT NULL, invoice_data VARCHAR (256) NOT NULL, FOREIGN KEY (transaction_id) REFERENCES transaction (transaction_id) );
- The transaction_id is the transaction table’s primary key, referred to as a foreign key for the invoices table. So while performing the natural join operation, we will use the transaction_id column as it is the common column for both tables. The transaction may have zero or more invoices, and the invoice will belong to one and only one transaction.
Now insert some data into the transaction and invoices tables using the INSERT statement as follows:
INSERT INTO transaction (transaction_data) VALUES ('Purchase of Mobile'), ('Purchase of PC'), ('Purchase of Headphone'); INSERT INTO invoices (invoice_data, transaction_id) VALUES ('Purchase of Mobile', 1), ('Purchase of Mobile', 1), ('Purchase of PC', 2), ('Purchase of PC', 2), ('Purchase of Headphone', 3), ('Purchase of Headphone', 3);
- Illustrate the content of the transaction table using the following statement and snapshot.
select * from transaction;
- Illustrate the content of the invoices table using the following statement and snapshot.
select * from invoices;
- Join the invoices table with the transaction table using the NATURAL JOIN clause:
SELECT * FROM invoices NATURAL JOIN transaction; Illustrate the result of the above statement using the following snapshot.
- The above statement with the NATURAL JOIN clause is similar to the information with the INNER JOIN clause as follows:
SELECT * FROM invoices INNER JOIN transaction USING (transaction_id);
- Illustrate the result of the above statement using the following snapshot.
- It is not required to define the join clause as the NATURAL JOIN uses an implicit join clause based on the common column.
- But if possible, we should avoid using the NATURAL JOIN because having multiple common columns gives us an unexpected result.
Consider the following example for two tables named COUNTRIES and CITIES. The following CREATE TABLE statements will create the COUNTRIES and CITIES tables.
CREATE table COUNTRIES ( country_id serial PRIMARY KEY, country_name VARCHAR (256) NOT null, last_updated DATE NULL ); CREATE table CITIES ( CITY_id serial PRIMARY KEY, country_id INT NOT NULL, city_name VARCHAR (256) NOT NULL, last_updated DATE null, FOREIGN KEY (country_id) REFERENCES COUNTRIES (country_id) );
- Now insert some data into the COUNTRIES and CITIES tables using the INSERT statement as follows:
INSERT INTO COUNTRIES (country_name,last_updated) VALUES ('India','06-01-2020'), ('US','07-01-2020'), ('CHINA','08-01-2020'); INSERT INTO CITIES (country_id, city_name,last_updated) VALUES (1,'Pune','06-02-2020'), (1,'Mumbai', '07-02-2020'), (2,'New York', '08-02-2020'), (2,'Los Angeles', '09-02-2020'), (3,'Beijing ', '10-02-2020'), (3,'Shanghai', '11-02-2020');
select * from COUNTRIES;
- Illustrate the content of the COUNTRIES table using the following statement and snapshot.
select * from CITIES;
- Illustrate the content of the CITIES table using the following statement and snapshot.
- Here, Both COUNTRIES and CITIES tables have two common columns named country_id and last_updated, so to join these tables, we can use the NATURAL JOIN as follows:
SELECT * FROM COUNTRIES NATURAL JOIN CITIES;
- In the above statement, the COUNTRIES and CITIES tables have common columns, country_id and last_updated. Even if the last_updated column is common, it should not be used in the joint operation.
- However, the last_updated column is getting used in the NATURAL JOIN clause, ending in an unexpected result.
We hope you have learned how the PostgreSQL NATURAL JOIN works from the above article. Also, we have added some examples to demonstrate how to use the NATURAL JOIN to query data from two or more tables.
We hope that this EDUCBA information on “PostgreSQL NATURAL JOIN” was beneficial to you. You can view EDUCBA’s recommended articles for more information.