Updated May 3, 2023
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.
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.
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.
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:
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.
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:
select * from customer, department where customer.cust_id = department.id;
Output:
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.
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:
select cust_id, cust_balance from customer LEFT OUTER JOIN department ON customer.cust_id = department.id;
Output:
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.
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 cust_id, cust_balance from customer RIGHT OUTER JOIN department ON customer.cust_id = department.id;
Output:
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.
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:
select cust_id, cust_balance from customer FULL JOIN department ON customer.cust_id = department.id;
Output:
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.