EDUCBA Logo

EDUCBA

MENUMENU
  • Explore
    • EDUCBA Pro
    • PRO Bundles
    • Featured Skills
    • New & Trending
    • Fresh Entries
    • Finance
    • Data Science
    • Programming and Dev
    • Excel
    • Marketing
    • HR
    • PDP
    • VFX and Design
    • Project Management
    • Exam Prep
    • All Courses
  • Blog
  • Enterprise
  • Free Courses
  • Log in
  • Sign Up
Home Data Science Data Science Tutorials PostgreSQL Tutorial Joins in PostgreSQL
 

Joins in PostgreSQL

Priya Pedamkar
Article byPriya Pedamkar

Updated May 3, 2023

Joins in PostgreSQL

 

 

What are Joins in PostgreSQL?

PostgreSQL joins used to join data from multiple tables. We have used this. Whenever the need to retrieve data from one or more tables. In PostgreSQL, join data from different tables combined using common columns from different tables. Using this, we have the possibility to combine the select and join statement into one statement. We can join a single table with multiple names as an alias in PostgreSQL. It will retrieve data according to the condition. They are instrumental in PostgreSQL to retrieve data from multiple tables.

Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more.

PostgreSQL Join with its Types

While joining two tables in PostgreSQL, we need have to use the primary key from the first table and the foreign key from the second table.

The following are the types of it.

  • CROSS JOIN
  • INNER JOIN
  • LEFT OUTER JOIN OR LEFT JOIN
  • RIGHT OUTER JOIN OR RIGHT JOIN
  • FULL OUTER JOIN OR FULL JOIN

We have used customer and department tables, for example, to describe types of joins. Please find below for the same.

Types of departments

1. CROSS JOIN

PostgreSQL cross join matches each row of the first table and each row of the second table. It will display all the columns of both tables. If table 1 has a C column and table 2 have D columns, then the join table’s result will have (C+D) columns.

Below is the image representation of the cross join.

Joins in PostgreSQL - CROSS JOIN

Following is the syntax of cross join.

Syntax:

Below is the syntax of cross join.

SELECT column1, column2, …  FROM table1 CROSS JOIN table2
Select (*) from table1 CROSS JOIN table2

Example:

Below is an example of the cross join.

select cust_id, cust_name from customer cross join department;

Output:

Cross Join

2. INNER JOIN

PostgreSQL inner join is also called as self-join. It is the most common type of join in PostgreSQL. This join returns all matching rows from multiple tables when the join condition is satisfied.

Below is the image representation of the Inner join.

Joins in PostgreSQL -INNER JOIN

Syntax:

Below is the syntax of Inner Join.

Select [ (*) | columnist ] from table1 Inner Join table2 on table1.columnname = table2.columnname
Select [ (*) | columnist ] from table1 Inner Join table2 using (Column_name);
Select [ (*) | columnist ] from table1, table2 where table1.columnname = table2.columnname

Example:

Below is an example of an Inner join.

select cust_id, cust_name from customer inner join department on customer.cust_id = department.id;

Output:

Inner Join 1

select * from customer, department where customer.cust_id = department.id;

Output:

Inner Join 2

3. LEFT OUTER JOIN or LEFT JOIN

While joining the table using the left outer join, PostgreSQL first does normal join and then it starts scanning from the left table. PostgreSQL left join retrieves all rows from the left table and all matching rows from the right table. If there is no match in both tables, the right tables have null values.

Below is the image representation of the left outer join is as follows.

Joins in PostgreSQL- LEFT JOIN

Syntax:

Below is the syntax of the left outer join is as follows.

Select columns from table_name1 LEFT OUTER JOIN table_name2 on table_name1.column = table_name2.column;
SELECT (*) FROM table_name1 LEFT OUTER JOIN table_name2 on table_name1.column = table_name2.column;

Example:

The below example shows the left outer join is as follows.

select * from customer LEFT OUTER JOIN department ON customer.cust_id = department.id;

Output:

Left Outer Join or Left Join 1

select cust_id, cust_balance from customer LEFT OUTER JOIN department ON customer.cust_id = department.id;

Output:

cust_balance

4. RIGHT OUTER JOIN OR RIGHT JOIN

While joining the table using the right outer join, PostgreSQL first does normal join and then it starts scanning from the right table. PostgreSQL right join retrieves all rows from the right table and all matching rows from the left table. If there is no match in both tables, the left tables have null values.

Below is the image representation of the right outer join is as follows.

Joins in PostgreSQL- RIGHT JOIN

Syntax:

Below is the syntax of the right outer join is as follows.

Select columns from table_name1 RIGHT OUTER JOIN table_name2 on table_name1.column = table_name2.column;
SELECT (*) FROM table_name1 RIGHT OUTER JOIN table_name2 on table_name1.column = table_name2.column;

Example:

The below example shows the left outer join is as follows.

select * from customer RIGHT OUTER JOIN department ON customer.cust_id = department.id;

Output:

select * from customer

select cust_id, cust_balance from customer RIGHT OUTER JOIN department ON customer.cust_id = department.id;

Output:

Right Join 2

5. FULL OUTER JOIN OR FULL JOIN

PostgreSQL full outer join returns all rows from the left table as well as the right table. It will put null when the full outer join condition was not satisfied. While joining the table using FULL OUTER JOIN first, it will join be using an inner join. The combination of left and right join is known as a full outer join.

Below is the image representation of a full outer join.

Table1

Syntax:

Below is the syntax of full join is as follows.

Select columns from table_name1 FULL JOIN table_name2 on table_name1.column = table_name2.column;
SELECT (*) FROM table_name1 FULL JOIN table_name2 on table_name1.column = table_name2.column;

Example:

The below example shows a full join are as follows.

select * from customer FULL JOIN department ON customer.cust_id = department.id;

Output:

 full Outer Join or Full Join 1

select cust_id, cust_balance from customer FULL JOIN department ON customer.cust_id = department.id;

Output:

select cust_id

Importance of Joins in PostgreSQL

The following are the importance of Joins in PostgreSQL:

  • It is used to join two or multiple tables in the database.
  • If we select data from multiple tables and retrieve the same joins is used in it.
  • As the joining of tables in it, we require input data.
  • Its combining fields from two tables by using values common to each other.
  • It is more important to retrieve data from one or more tables.

Conclusion

They are used to retrieving data from multiple tables using multiple conditions. There are multiple types of joins are available to join one or multiple tables. Data is retrieved as per the condition provided or which type of join we have used in the query.

Recommended Articles

We hope that this EDUCBA information on “Joins in PostgreSQL” was beneficial to you. You can view EDUCBA’s recommended articles for more information.

  1. Tableau Joins
  2. Joins in MySQL
  3. SQL Inner Join | How to Work? | Examples
  4. Guide to SQL Left Join

Primary Sidebar

Footer

Follow us!
  • EDUCBA FacebookEDUCBA TwitterEDUCBA LinkedINEDUCBA Instagram
  • EDUCBA YoutubeEDUCBA CourseraEDUCBA Udemy
APPS
EDUCBA Android AppEDUCBA iOS App
Blog
  • Blog
  • Free Tutorials
  • About us
  • Contact us
  • Log in
Courses
  • Enterprise Solutions
  • Free Courses
  • Explore Programs
  • All Courses
  • All in One Bundles
  • Sign up
Email
  • [email protected]

ISO 10004:2018 & ISO 9001:2015 Certified

© 2025 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA Login

Forgot Password?

🚀 Limited Time Offer! - 🎁 ENROLL NOW