A PostgreSQL JOIN combines rows from two or more tables based on a related column. It helps in:
- Matching records using common columns.
- Simplifying data retrieval from relational databases.
- Creating meaningful result sets by combining information from different tables.
Types of JOIN
PostgreSQL supports different types of joins based on how rows from multiple tables are matched and combined.
INNER JOIN
INNER JOIN returns only the rows that have matching values in both tables. It helps in:
- Retrieving only matching records from both tables.
- Combining related data using a common column.
- Excluding rows without matching values.
- Maintaining accurate relationships between tables.
Syntax:
SELECT table_1.column_1, table_1.column_2, table_2.column_1, ...
FROM table_1
INNER JOIN table_2
ON table_1.common_column = table_2.common_column;
Note: In PostgreSQL, JOIN and INNER JOIN are equivalent. You can use either syntax.
Example of INNER JOIN
Firstly, create the Students and Enrollments tables and insert some records.
students table:

enrollments table:

The following query displays the student names along with their enrolled courses.
Query:
SELECT
e.course_id,
s.student_name,
s.age
FROM student s
INNER JOIN course e
ON s.student_id = e.student_id;
Output:

LEFT JOIN
LEFT JOIN returns all rows from the left table and matching rows from the right table. It helps in:
- Returning all records from the left table.
- Displaying matching records from the right table.
- Showing NULL values when no matching record exists.
- Retrieving complete data from the left table.
Syntax
SELECT table_1.column_1, table_2.column_1
FROM table_1
LEFT JOIN table_2
ON table_1.common_column = table_2.common_column;
Example
The following query returns all students along with their enrolled courses.
Query:
SELECT
s.student_name,
e.course_id
FROM student s
LEFT JOIN course e
ON s.student_id = e.student_id;
Output:

RIGHT JOIN
RIGHT JOIN returns all rows from the right table and the matching rows from the left table. It helps in:
- Returning all records from the right table.
- Displaying matching records from the left table.
- Showing NULL values where no match exists.
- Retrieving complete data from the right table.
Syntax:
SELECT table_1.column_1, table_2.column_1
FROM table_1
RIGHT JOIN table_2
ON table_1.common_column = table_2.common_column;
Note: RIGHT JOIN and RIGHT OUTER JOIN are the same in PostgreSQL.
Example
The following query returns all course enrollments along with student names.
Query:
SELECT
s.student_name,
e.course_id
FROM student s
RIGHT JOIN course e
ON s.student_id = e.student_id;
Output:

FULL JOIN
FULL JOIN combines the results of both LEFT JOIN and RIGHT JOIN. It helps in:
- Returning all rows from both tables.
- Displaying matching records from both tables.
- Showing NULL values where no match exists.
- Retrieving complete information from both tables.
Syntax
SELECT table_1.column_1, table_2.column_1
FROM table_1
FULL JOIN table_2
ON table_1.common_column = table_2.common_column;
Example
The following query returns all students and all enrollments.
Query:
SELECT
s.student_name,
e.course_id
FROM student s
FULL OUTER JOIN course e
ON s.student_id = e.student_id;
Output:

NATURAL JOIN
A NATURAL JOIN automatically joins two tables based on columns having the same name and compatible data types. It returns only the rows where the common column values match.
- Automatically joining tables using common column names.
- Returning matching rows from both tables.
- Displaying the common column only once in the result.
- Reducing the need to specify the join condition explicitly.
Syntax
SELECT column_list
FROM table_1
NATURAL JOIN table_2;
Example
The following query displays students and their enrolled courses using NATURAL JOIN.
Query:
SELECT
student_name,
course_id
FROM student
NATURAL JOIN course;
Output:


