EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 360+ Courses All in One Bundle
  • Login

Joins in PostgreSQL

By Priya PedamkarPriya Pedamkar

Home » Data Science » Data Science Tutorials » PostgreSQL Tutorial » Joins in PostgreSQL

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.

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.

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

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.

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.

LEFT OUTER JOIN or 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;

Popular Course in this category
Sale
SQL Training Program (7 Courses, 8+ Projects)7 Online Courses | 8 Hands-on Projects | 73+ Hours | Verifiable Certificate of Completion | Lifetime Access
4.5 (8,621 ratings)
Course Price

View Course

Related Courses

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.

RIGHT OUTER 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

This is a guide to Joins in PostgreSQL. Here we discuss the introduction to PostgreSQL Join with its 5 joins & importance, along with appropriate syntaxes and respective examples. You can also go through our other related articles to learn more –

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

SQL Training Program (7 Courses, 8+ Projects)

7 Online Courses

8 Hands-on Projects

73+ Hours

Verifiable Certificate of Completion

Lifetime Access

Learn More

0 Shares
Share
Tweet
Share
Primary Sidebar
PostgreSQL Tutorial
  • Basic
    • What is PostgreSQL
    • PostgreSQL Features
    • How to Install PostgreSQL
    • PostgreSQL Versions
    • PostgreSQL Architecture
    • PostgreSQL GUI
    • Postgres Command-Line
    • PostgreSQL Variables
    • PostgreSQL Data Types
    • PostgreSQL NOT NULL
    • PostgreSQL Integer
    • PostgreSQL Boolean
    • PostgreSQL BIGINT
    • PostgreSQL NULLIF
    • PostgreSQL Administration
    • PostgreSQL Commands
    • PostgreSQL Operators
    • PostgreSQL IN Operator
    • Postgres like query
    • PostgreSQL encode
    • PostgreSQL Cheat Sheet
    • PostgreSQL List Databases
    • PostgreSQL Rename Database
  • Control Statement
    • PostgreSQL IF Statement
    • PostgreSQL if else
    • PostgreSQL CASE Statement
    • PostgreSQL LOOP
    • PostgreSQL For Loop
    • PostgreSQL While Loop
  • Joins
    • Joins in PostgreSQL
    • PostgreSQL Inner Join
    • PostgreSQL Outer Join
    • LEFT OUTER JOIN in PostgreSQL
    • PostgreSQL FULL OUTER JOIN
    • PostgreSQL LEFT JOIN
    • PostgreSQL Full Join
    • PostgreSQL Cross Join
    • PostgreSQL NATURAL JOIN
    • PostgreSQL UPDATE JOIN
  • Queries
    • PostgreSQL Queries
    • PostgreSQL INSERT INTO
    • PostgreSQL WHERE Clause
    • PostgreSQL WITH Clause
    • PostgreSQL ORDER BY
    • PostgreSQL ORDER BY Random
    • PostgreSQL ORDER BY DESC
    • PostgreSQL GROUP BY
    • PostgreSQL group_concat
    • PostgreSQL HAVING
    • PostgreSQL Recursive Query
  • Advanced
    • PostgreSQL Schema
    • Postgres List Schemas
    • PostgreSQL Drop Schema
    • PostgreSQL VARCHAR
    • Array in PostgreSQL
    • PostgreSQL DDL
    • PostgreSQL List Users
    • Postgres Default User
    • Postgres add user
    • PostgreSQL User Password
    • PostgreSQL log_statement
    • PostgreSQL repository
    • PostgreSQL shared_buffer
    • PostgreSQL String Functions
    • PostgreSQL Compare Strings
    • PostgreSQL Text Search
    • PostgreSQL TEXT
    • PostgreSQL String Array
    • PostgreSQL where in array
    • PostgreSQL Constraints
    • PostgreSQL UNIQUE Constraint
    • PostgreSQL CHECK Constraint
    • PostgreSQL INTERSECT
    • PostgreSQL Like
    • Cursors in PostgreSQL
    • PostgreSQL UNION ALL
    • Indexes in PostgreSQL
    • PostgreSQL Index Types
    • PostgreSQL REINDEX
    • PostgreSQL UNIQUE Index
    • PostgreSQL Clustered Index
    • PostgreSQL DROP INDEX
    • PostgreSQL DISTINCT
    • PostgreSQL FETCH
    • PostgreSQL RAISE EXCEPTION
    • PostgreSQL Auto Increment
    • Sequence in PostgreSQL
    • Wildcards in PostgreSQL
    • PostgreSQL Subquery
    • PostgreSQL Alias
    • PostgreSQL LIMIT
    • PostgreSQL Limit Offset
    • PostgreSQL LAG()
    • PostgreSQL Table
    • Postgres Show Tables
    • PostgreSQL Describe Table
    • PostgreSQL Lock Table
    • PostgreSQL ALTER TABLE
    • Postgres Rename Table
    • PostgreSQL List Tables
    • PostgreSQL TRUNCATE TABLE
    • PostgreSQL Table Partitioning
    • Postgres DROP Table
    • PostgreSQL Functions
    • PostgreSQL Math Functions
    • PostgreSQL Window Functions
    • Aggregate Functions in PostgreSQL
    • PostgreSQL Primary Key
    • Foreign Key in PostgreSQL
    • PostgreSQL Procedures
    • PostgreSQL Stored Procedures
    • PostgreSQL Views
    • PostgreSQL Materialized Views
    • Postgres Create View
    • PostgreSQL Triggers
    • PostgreSQL DROP TRIGGER
    • PostgreSQL Date Functions
    • PostgreSQL TO_DATE()
    • PostgreSQL datediff
    • PostgreSQL Timestamp
    • PostgreSQL CURRENT_TIMESTAMP()
    • PostgreSQL Notify
    • PostgreSQL LENGTH()
    • PostgreSQL blob
    • PostgreSQL Median
    • PostgreSQL kill query
    • PostgreSQL Formatter
    • PostgreSQL RANK()
    • PostgreSQL Select
    • PostgreSQL Average
    • PostgreSQL DATE_PART()
    • PostgreSQL EXECUTE
    • PostgreSQL COALESCE
    • PostgreSQL EXTRACT()
    • PostgreSQL Sort
    • PostgreSQL TO_CHAR
    • PostgreSQL Interval
    • PostgreSQL Number Types
    • PostgreSQL ROW_NUMBER
    • Alter Column in PostgreSQL
    • PostgreSQL Identity Column
    • PostgreSQL SPLIT_PART()
    • PostgreSQL CONCAT()
    • PostgreSQL replace
    • PostgreSQL TRIM()
    • PostgreSQL MAX
    • PostgreSQL DELETE
    • PostgreSQL Float
    • PostgreSQL OID
    • PostgreSQL log
    • PostgreSQL REGEXP_MATCHES()
    • PostgreSQL MD5 
    • PostgreSQL NOW()
    • PostgreSQL RANDOM
    • PostgreSQL round
    • PostgreSQL Trunc()
    • PostgreSQL TIME
    • PostgreSQL IS NULL
    • PostgreSQL CURRENT_TIME
    • PostgreSQL MOD()
    • Postgresql Count
    • PostgreSQL Datetime
    • PostgreSQL MIN()
    • PostgreSQL age()
    • PostgreSQL enum
    • PostgreSQL OR
    • PostgreSQL Wal
    • PostgreSQL NOT IN
    • PostgreSQL SET
    • PostgreSQL Current Date
    • PostgreSQL Compare Date
    • PostgreSQL SERIAL
    • PostgreSQL UUID
    • PostgreSQL Merge
    • PostgreSQL Database
    • PostgreSQL Clone Database
    • PostgreSQL Copy Database
    • PostgreSQL Show Databases
    • PostgreSQL Restore Database
    • PostgreSQL DROP DATABASE
    • PostgreSQL ALTER DATABASE
    • Postgres DROP Database
    • Postgres Dump Database
    • PostgreSQL OFFSET
    • PostgreSQL GRANT
    • PostgreSQL COMMIT
    • PostgreSQL ROLLUP
    • PostgreSQL JSON
    • EXPLAIN ANALYZE in PostgreSQL
    • PostgreSQL Temporary Table
    • PostgreSQL Show Tables
    • PostgreSQL cluster
    • PostgreSQL Replication
    • PostgreSQL Logical Replication
    • PostgreSQL flush privileges
    • PostgreSQL Tablespaces
    • CAST in PostgreSQL
    • PostgreSQL CTE
    • hstore in PostgreSQL
    • PostgreSQL Encryption
    • PostgreSQL DECODE()
    • PostgreSQL Vacuum
    • PostgreSQL EXCLUDE
    • Postgres Change Password
    • Postgres Delete Cascade
    • PostgreSQL EXCEPT
    • PostgreSQL Roles
    • PostgreSQL Link
    • PostgreSQL Partition
    • PostgreSQL column does not exist
    • PostgreSQL Log Queries
    • PostgreSQL escape single quote
    • PostgreSQL Query Optimization
    • PostgreSQL Character Varying
    • PostgreSQL Transaction
    • PostgreSQL Extensions
    • PostgreSQL Import CSV
    • PostgreSQL Client
    • PostgreSQL caching
    • PostgreSQL Incremental Backup
    • PostgreSQL JSON vs JSONNB
    • PostgreSQL JDBC Driver
    • PostgreSQL Interview Questions
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Database Management
  • Machine Learning
  • All Tutorials
Certification Courses
  • All Courses
  • Data Science Course - All in One Bundle
  • Machine Learning Course
  • Hadoop Certification Training
  • Cloud Computing Training Course
  • R Programming Course
  • AWS Training Course
  • SAS Training Course

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

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

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

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

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

By signing up, you agree to our Terms of Use and Privacy Policy.

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

EDUCBA

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

By signing up, you agree to our Terms of Use and Privacy Policy.

Special Offer - SQL Training Program (7 Courses, 8+ Projects) Learn More