0% found this document useful (0 votes)
198 views5 pages

PostgreSQL Self Join

This document discusses using self-joins in PostgreSQL to compare rows within the same table. It provides two examples: 1) finding employee/manager relationships by joining an employees table to itself using manager_id, and 2) finding pairs of films from a films table that have the same length by joining the table to itself and filtering for different film IDs but equal lengths. Self-joins allow comparing column values in the same table by treating it as two tables with aliases.

Uploaded by

Lord_King
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
198 views5 pages

PostgreSQL Self Join

This document discusses using self-joins in PostgreSQL to compare rows within the same table. It provides two examples: 1) finding employee/manager relationships by joining an employees table to itself using manager_id, and 2) finding pairs of films from a films table that have the same length by joining the table to itself and filtering for different film IDs but equal lengths. Self-joins allow comparing column values in the same table by treating it as two tables with aliases.

Uploaded by

Lord_King
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

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 query in which a table is joined to itself. Self-joins are useful for
comparing values in a column of 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.

The following query shows how to join the table A 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.

PostgreSQL self-join examples


1) Querying hierarchy data example

Let’s set up a sample table for the demonstration.

Suppose, we have the following organization structure:


The following SQL script creates the employee table and inserts some sample data:

1 CREATE TABLE employee (


2 employee_id INT PRIMARY KEY,
3 first_name VARCHAR (255) NOT NULL,
4 last_name VARCHAR (255) NOT NULL,
5 manager_id INT,
6 FOREIGN KEY (manager_id)
7 REFERENCES employee (employee_id)
8 ON DELETE CASCADE
9 );
10 INSERT INTO employee (
11 employee_id,
12 first_name,
13 last_name,
14 manager_id
15 )
16 VALUES
17 (1, 'Windy', 'Hays', NULL),
18 (2, 'Ava', 'Christensen', 1),
19 (3, 'Hassan', 'Conner', 1),
20 (4, 'Anna', 'Reeves', 2),
21 (5, 'Sau', 'Norman', 2),
22 (6, 'Kelsie', 'Hays', 3),
23 (7, 'Tory', 'Goff', 3),
24 (8, 'Salley', 'Lester', 3);

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.

To find who reports to whom, you use 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 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.

The join predicate finds employee/manager pair by matching values in the


employee_id and manager_id columns.
As you can see from the screenshot, the top manager did 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:

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;

2) Comparing the rows with the same table

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.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy