Beruflich Dokumente
Kultur Dokumente
Summary: in this tutorial, you will learn how to use the PostgreSQL self-join technique
to compare rows within the same table.
To form a self-join, you specify the same table twice with different aliases, set up the
comparison, and eliminate cases where a value would be equal to itself.
1 SELECT column_list
2 FROM A a1
3 INNER JOIN A b1 ON join_predicate;
In this syntax, table A is joined to itself using the INNER JOIN clause. Note that you can
also use the LEFT JOIN or RIGHT JOIN clause.
The value in the manager_id column represents the manager who the employee reports
to. If the value in the manager_id column is null, then the employee does not report to
anyone. In other words, that employee is the top manager.
1 SELECT
2 e.first_name || ' ' || e.last_name employee,
3 m .first_name || ' ' || m .last_name manager
4 FROM
5 employee e
6 INNER JOIN employee m ON m .employee_id = e.manager_id
7 ORDER BY
8 manager;
This query referenced the employees table twice, one as the employee and another as
the manager. It uses table aliases e for the employee and m for the manager.
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:
1 SELECT
2 e.first_name || ' ' || e.last_name employee,
3 m .first_name || ' ' || m .last_name manager
4 FROM
5 employee e
6 LEFT JOIN employee m ON m .employee_id = e.manager_id
7 ORDER BY
8 manager;
See the following film table from the DVD rental database:
The following query finds all pair of films that have the same length.
1 SELECT
2 f1.title,
3 f2.title,
4 f1. length
5 FROM
6 film f1
7 INNER JOIN film f2 ON f1.film_id <> f2.film_id
8 AND f1. length = f2. length;
This query joined the film table to itself. The join predicate matches different films
specified by film_id that have the same length.
In this tutorial, you have learned how to use the PostgreSQL self-join technique to join
a table to itself.