PostgreSQL Tutorial

  • Home
  • Stored Procedures
  • Triggers
  • Views
  • Interfaces
    • PostgreSQL PHP
    • PostgreSQL Python
    • PostgreSQL JDBC
  • Functions
Home / PostgreSQL Tutorial / PostgreSQL NATURAL JOIN Explained By Examples

PostgreSQL NATURAL JOIN Explained By Examples

Summary: in this tutorial, you will learn how to use the PostgresQL NATURAL JOIN to query data from two or more tables.

A natural join is a join that creates an implicit join based on the same column names in the joined tables. See the following syntax of PostgreSQL natural join:

1
2
3
SELECT *
FROM T1
NATURAL [INNER, LEFT, RIGHT] JOIN T2;

A natural join can be an inner join, left join, or right join. If you do not specify a join explicitly e.g., INNER JOIN, LEFT JOIN, RIGHT JOIN, PostgreSQL will use the INNER JOIN by default.

If you use the asterisk (*) in the select list, the result will contain the following columns:

  • All the common columns, which are the columns in the both tables that have the same name
  • Every column in the first and second tables that is not a common column

PostgreSQL NATURAL JOIN examples

To demonstrate the PostgreSQL natural join, we will create two tables: categories and products. The following CREATE TABLE statements create the categories and products table.

1
2
3
4
5
6
7
8
9
10
11
CREATE TABLE categories (
category_id serial PRIMARY KEY,
category_name VARCHAR (255) NOT NULL
);
 
CREATE TABLE products (
product_id serial PRIMARY KEY,
product_name VARCHAR (255) NOT NULL,
category_id INT NOT NULL,
FOREIGN KEY (category_id) REFERENCES category (category_id)
);

Each category has zero or many products whereas each product belongs to one and only one category. The category_id column in the products table is the foreign key that references to the primary key of the categories table. The category_id is the common column that we will use to perform the natural join.

The following INSERT statements insert some sample data into the categories and products tables.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
INSERT INTO categories (category_name)
VALUES
('Smart Phone'),
('Laptop'),
('Tablet');
 
INSERT INTO products (product_name, category_id)
VALUES
('iPhone', 1),
('Samsung Galaxy', 1),
('HP Elite', 2),
('Lenovo Thinkpad', 2),
('iPad', 3),
('Kindle Fire', 3);

The following statement uses the NATURAL JOIN clause to join the products table with the categories table:

1
2
3
4
5
SELECT
*
FROM
products
NATURAL JOIN categories;

1
2
3
4
5
6
7
8
9
category_id | product_id |  product_name   | category_name
-------------+------------+-----------------+---------------
           1 |          1 | iPhone          | Smart Phone
           1 |          2 | Samsung Galaxy  | Smart Phone
           2 |          3 | HP Elite        | Laptop
           2 |          4 | Lenovo Thinkpad | Laptop
           3 |          5 | iPad            | Tablet
           3 |          6 | Kindle Fire     | Tablet
(6 rows)

The above statement is equivalent to the following statement that uses the INNER JOIN clause.

1
2
3
4
5
SELECT
*
FROM
products
INNER JOIN categories USING (category_id);

The convenience of the NATURAL JOIN is that it does not require you to specify the join clause because it uses an implicit join clause based on the common column.

However, you should avoid using the NATURAL JOIN whenever possible because sometimes it may cause an unexpected result.

For example, let’s take a look at the city and country tables. Both tables have the same country_id column so we can use the NATURAL JOIN to join these tables as follows:

1
2
3
4
5
SELECT
*
FROM
city
NATURAL JOIN country;

1
2
3
country_id | last_update | city_id | city | country
------------+-------------+---------+------+---------
(0 rows)

The query returns an empty result set.

The reason is that…

Both tables also have a common column named last_update, which cannot be used for the join. However, the NATURAL JOIN clause just uses the last_update column.

In this tutorial, we have explained to you how the PostgreSQL NATURAL JOIN works and shown you how to use it to query data from two or more tables.

Related Tutorials

  • PostgreSQL INNER JOIN
  • PostgreSQL LEFT JOIN
  • PostgreSQL Cross Join By Example
  • PostgreSQL FULL OUTER JOIN
Previous Tutorial: PostgreSQL LEFT JOIN
Next Tutorial: PostgreSQL Cross Join By Example

PostgreSQL Quick Start

  • What is PostgreSQL?
  • Install PostgreSQL
  • Connect to Database
  • Download PostgreSQL Sample Database
  • Load Sample Database
  • Explore Server and Database Objects

PostgreSQL Fundamentals

  • PostgreSQL Select
  • PostgreSQL Order By
  • PostgreSQL Select Distinct
  • PostgreSQL Where
  • PostgreSQL LIMIT
  • PostgreSQL IN
  • PostgreSQL Between
  • PostgreSQL Like
  • PostgreSQL Inner Join
  • PostgreSQL Left Join
  • PostgreSQL Full Outer Join
  • PostgreSQL Cross Join
  • PostgreSQL Natural Join
  • PostgreSQL Group By
  • PostgreSQL Having
  • PostgreSQL Union
  • PostgreSQL Intersect
  • PostgreSQL Except
  • PostgreSQL Subquery
  • PostgreSQL Insert
  • PostgreSQL Update
  • PostgreSQL Delete
  • PostgreSQL Data Types
  • PostgreSQL Create Table
  • PostgreSQL Alter Table
  • PostgreSQL Drop Table
  • PostgreSQL Truncate Table
  • PostgreSQL CHECK Constraint
  • PostgreSQL Not-Null Constraint
  • PostgreSQL Foreign Key
  • PostgreSQL Primary Key
  • PostgreSQL UNIQUE Constraint

About PostgreSQL Tutorial

PostgreSQLTutorial.com is a website dedicated to developers and database administrators who are working on PostgreSQL database management system.

We constantly publish useful PostgreSQL tutorials to keep you up-to-date with the latest PostgreSQL features and technologies. All PostgreSQL tutorials are simple, easy-to-follow and practical.

Recent PostgreSQL Tutorials

  • How To Change The Password of a PostgreSQL User
  • PostgreSQL AGE Function
  • PostgreSQL DATE_PART Function
  • PostgreSQL List Users
  • PostgreSQL NOW Function
  • PostgreSQL DATE_TRUNC Function
  • PostgreSQL TO_DATE Function: Convert String to Date
  • A Look at PostgreSQL User-defined Data Types
  • PostgreSQL Copy Database Made Easy
  • How to Get Table, Database, Indexes, Tablespace, and Value Size in PostgreSQL

More Tutorials

  • PostgreSQL Cheat Sheet
  • PostgreSQL Administration
  • PostgreSQL PHP
  • PostgreSQL Python
  • PostgreSQL JDBC
  • PostgreSQL Resources

Site Info

  • Home
  • About Us
  • Contact Us
  • Privacy Policy

Copyright © 2017 by PostgreSQL Tutorial Website. All Rights Reserved.