PostgreSQL Self-Join
Summary: in this tutorial, you will learn how to use the PostgreSQL self-join technique to compare rows within the same table.
Introduction to PostgreSQL self-join
A self-join is a regular join that joins a table to itself. In practice, you typically use a self-join to query hierarchical data or to compare rows within the same table.
To form a self-join, you specify the same table twice with different table aliases and provide the join predicate after the ON
keyword.
The following query uses an INNER JOIN
that joins the table to itself:
In this syntax, the table_name
is joined to itself using the INNER JOIN
clause.
Alternatively, you can use the LEFT JOIN
or RIGHT JOIN
clause to join the table to itself like this:
PostgreSQL self-join examples
Let’s take some examples of using self-joins.
1) Querying hierarchical data example
Let’s set up a sample table for the demonstration.
Suppose, you have the following organizational structure:
The following statements create the employee
table and insert some sample data into the table.
Output:
In this employee
table, the manager_id
column references the employee_id
column.
The manager_id
column indicates the direct relationship, showing the manager to whom the employee reports.
If the manager_id
column contains NULL, which signifies that the respective employee does not report to anyone, essentially holding the top managerial position.
The following query uses the self-join to find who reports to whom:
Output:
This query references the employees
table twice, one as the employee and the other as the manager. It uses table aliases e
for the employee and m
for the manager.
The join predicate finds the employee/manager pair by matching values in the employee_id
and manager_id
columns.
Notice that the top manager does not appear on the output.
To include the top manager in the result set, you use the LEFT JOIN
instead of INNER JOIN
clause as shown in the following query:
Output:
2) Comparing the rows with the same table
See the following film
table from the DVD rental database:
The following query finds all pairs of films that have the same length,
Output:
The join predicate matches two different films (f1.film_id > f2.film_id
) that have the same length (f1.length = f2.length
)
Summary
- A PostgreSQL self-join is a regular join that joins a table to itself using the
INNER JOIN
orLEFT JOIN
. - Self-joins are very useful for querying hierarchical data or comparing rows within the same table.