Using the Self join
The tables we are joining don't have to be different ones. We can join a table with itself. This is called a self join. In this case, we will use aliases for the table; otherwise, PostgreSQL will not know which column of which table instance we mean. To join a table with itself means that each row of the table is combined with itself, and with every other row of the table. The self join can be viewed as a joining of two copies of the same table. The table is not actually copied but SQL carries out the command as though it were.
The syntax of the command to join a table with itself is almost the same as that of joining two different tables:
SELECT a.column_name, b.column_name... FROM table1 a, table1 b WHERE condition1 and/or condition2
To distinguish the column names from one another, aliases for the actual table names are used as both the tables have the same name. Table name aliases are defined in the FROM
clause of the SELECT
statement.
Let's consider an example where you want to find a list of employees and their supervisor. For this example, we will consider the Employee table that has the columns Employee_id, Employee_name, and Supervisor_id. The Supervisor_id contains nothing but the Employee_id of the person who the employee reports to.
In the following example, we will use the table Employee twice; and in order to do this, we will use the alias of the table:
SELECT a.emp_id AS "Emp_ID", a.emp_name AS "Employee Name", b.emp_id AS "Supervisor ID",b.emp_name AS "Supervisor Name" FROM employee a, employee b WHERE a.supervisor_id = b.emp_id;
For every record, it will compare the Supervisor_id to the Employee_id and the Employee_name to the supervisor name.