What is Self Join in SQL And When Would You Use it?

Summary: In this tutorial, you will learn how to use the SQL Server self join in SQL to join a table to itself, and also you will learn when would you use it with example.
Overview: Self Join in SQL
A SQL Self Join is a join in which a table is joined with itself, which is also known as Unary relationships, Therefore when the table has a FOREIGN KEY which references its own PRIMARY KEY. furthermore, To join a table itself means that each row of the table is combined with itself and with every other row of the table.
Therefore, self join SQL allows you to join a table to itself, it is very useful for querying comparing rows within the same table or hierarchical data. A self joins in SQL uses the left join or inner join clause. Because the query that uses a self join references the same table. Furthermore, the table alias is used to assign different names to the same table within the query.
Note:
The self-join in SQL can be viewed as a join of two copies of the same table. therefore, The table is not actually copied, but SQL performs the command as though it were.
The General syntax of the command for joining a table to itself is almost the same as that for joining the two different tables. furthermore, to distinguish the column names from one another, aliases for the actual table name are used since both the tables have the same name. The table name aliases are defined in the FROM clause of the SELECT SQL statement.
The following syntax is given below :
|
1 2 3 |
SELECT a.column_name, b.column_name... FROM table1 a, table1 b WHERE a.common_filed = b.common_field; |
There are the given following below syntax of joining the table T to itself:
|
1 2 3 4 5 6 |
SELECT select_list FROM T t1 [INNER | LEFT] JOIN T t2 ON join_predicate; |
SQL Self Join Example
Furthermore, in this tutorial we have used a table TEACHER, that has one-to-many relationship.
Let’s see the following SQL Code to create the table TEACHER
|
1 2 3 4 5 6 7 8 |
CREATE TABLE Teacher(TeacherID varchar(6) NOT NULL, Teacher_Name varchar(30) NULL, City varchar(30) NULL, Join_Date date NULL, Teacher_Dept varchar(6) NULL, CONSTRAINT TeacherID PRIMARY KEY(TeacherID), CONSTRAINT Teacher_Dept FOREIGN KEY(Teacher_Dept) REFERENCES Teacher(TeacherID)); |
Sample Table: Teacher
There are the given below demo records in table Teacher
| TeacherID | Teacher_Name | City | Join_Date | Teacher_Dept |
|---|---|---|---|---|
| 1001 | Aarav Adriano | London | 2012-02-05 | NULL |
| 1002 | Armando Aurelio | France | 2012-02-15 | 1001 |
| 1003 | Gaston Guillaume | Greece | 2012-03-02 | 1002 |
| 1004 | Adeline Anais | Strasbourg | 2012-03-12 | 1001 |
| 1005 | Christine Claire | Buenos Aires | 2012-03-28 | 1002 |
| 1006 | Germaine Henriette | Guyana | 2012-04-05 | 1001 |
The Structure Of The Table

Therefore, in the Teacher table displayed above, TeacherID is the primary key. Teacher_Dept is the foreign key as you can see in the table structure or SQL code. Furthermore, If we want a list of teachers and the names of their department, then we’ll have to JOIN the TEACHER table to itself to get this list.
Let’s understand the relationship the following data into the table TEACHER as per given sample table data.
Unary Relationship To Teacher

Self Join Example in SQL
Furthermore, let’s us see the following example, we will use the table TEACHER twice and in order to do this we will use the alias of the table. Therefore, to get the list of Teacher and their Teacher name the following SQL statement has used:
SQL Statement:
|
1 2 3 4 |
SELECT a.TeacherID AS "TeacherID",a.Teacher_Name AS "Teacher Name", b.TeacherID AS "Teacher_Dept",b.Teacher_Name AS "Supervisor Name" FROM Teacher a, Teacher b WHERE a.Teacher_Dept = b.TeacherID; |