
SQL Self Join is a powerful tool that allows you to join a table with itself. It’s a useful technique when you need to compare or analyze data in the same table. In this tutorial, we’ll cover everything you need to know about SQL Self Join, including syntax, use cases, and examples. Other join types include Inner Join, Left Join and Right Join and Full Outer Join, each serving specific purposes for combining data effectively.
What is SQL Self Join?
A self join is a regular join, but instead of joining two tables, you join a table with itself. The table is treated as if it were two different tables, with different names. This technique is commonly used to compare data within the same table.
A self join requires that the table has at least one column that can be used to relate the rows to each other. This column is usually a primary key or a foreign key.

Syntax
The syntax of SQL Self Join is similar to that of a regular join. Here’s an example:
SELECT t1.column1, t2.column2
FROM table_name t1
JOIN table_name t2 ON t1.related_column = t2.related_column
In this example, table_name
is the name of the table you want to join, and related_column
is the column that relates the rows to each other.
Note that you need to use two aliases for the same table. In this example, we used t1
and t2
, but you can use any aliases you like, as long as they are different from each other.
Types of Self Join
There are two types of self join: inner join and outer join.
Inner Join
An inner join returns only the rows that have matching values in both tables. Here’s an example:
SELECT t1.column1, t2.column2
FROM table_name t1
JOIN table_name t2 ON t1.related_column = t2.related_column
In this example, only the rows that have a matching value in the related_column
column will be returned.
Outer Join
An outer join returns all the rows from both tables, and fills in any missing data with NULL values. There are two types of outer join: left outer join and right outer join.
A left outer join returns all the rows from the first table, and fills in any missing data from the second table with NULL values. Here’s an example:
SELECT t1.column1, t2.column2
FROM table_name t1
LEFT JOIN table_name t2 ON t1.related_column = t2.related_column
In this example, all the rows from t1
will be returned, and any missing data from t2
will be filled in with NULL values.
A right outer join returns all the rows from the second table, and fills in any missing data from the first table with NULL values. Here’s an example:
SELECT t1.column1, t2.column2
FROM table_name t1
RIGHT JOIN table_name t2 ON t1.related_column = t2.related_column
In this example, all the rows from t2
will be returned, and any missing data from t1
will be filled in with NULL values.
Use Cases
SQL Self Join is a powerful technique that can be used in a variety of use cases. Here are some common examples:
Hierarchical Data
SQL Self Join is commonly used to query hierarchical data. For example, you might have a table that contains data about employees and their managers. By using a self join, you can create a hierarchical view of the data, with each employee linked to their manager.
Comparing Data
SQL Self Join is also useful for comparing data within the same table. For example, you might want to compare sales data for different regions or time periods. By using a self join, you can easily compare the sales data for the same product in different regions or time periods.
Recursive Queries
SQL Self Join can also be used to create recursive queries, where a row can be related to itself. For example, you might have a table that contains data about employees and their managers, where a manager can also be an employee. By using a self join, you can create a recursive query to retrieve all the employees in a specific hierarchy.
Examples
Let’s take a look at some examples of SQL Self Join in action.
Example 1: Comparing Sales Data
Suppose you have a table that contains data about sales for different products and regions. Here’s an example:
Product | Region | Sales |
---|---|---|
A | East | 100 |
A | West | 200 |
B | East | 150 |
B | West | 250 |
To compare the sales data for the same product in different regions, you can use a self join like this:
SELECT t1.product, t1.region, t1.sales, t2.region, t2.sales
FROM sales_data t1
JOIN sales_data t2 ON t1.product = t2.product AND t1.region <> t2.region
In this example, we used an inner join to join the sales_data
table with itself on the product
column. We also added a condition to exclude rows where the region
column is the same in both tables.
The result of this query will be:
Product | Region | Sales | Region | Sales |
---|---|---|---|---|
A | East | 100 | West | 200 |
A | West | 200 | East | 100 |
B | East | 150 | West | 250 |
B | West | 250 | East | 150 |
This shows the sales data for the same product in different regions side-by-side.
Example 2: Hierarchical Data
Suppose you have a table that contains data about employees and their managers. Here’s an example:
Employee | Manager |
---|---|
Alice | Bob |
Bob | Charlie |
Charlie | David |
To create a hierarchical view of this data, you can use a self join like this:
SELECT t1.employee, t2.employee AS manager, t3.employee AS top_manager
FROM employee_data t1
JOIN employee_data t2 ON t1.manager = t2.employee
JOIN employee_data t3 ON t2.manager = t3.employee
In this example, we used two inner joins to join the employee_data
table with itself twice, using the manager
column to relate the rows. We used aliases t1
, t2
, and t3
to distinguish between the different instances of the table.
The result of this query will be:
Employee | Manager | Top Manager |
---|---|---|
Alice | Bob | Charlie |
Bob | Charlie | David |
This shows the hierarchical relationship between the employees and their managers.
Conclusion
SQL Self Join is a powerful technique that can be used to compare or analyze data within the same table. By using a self join, you can create complex queries that would otherwise be difficult or impossible to write. In this tutorial, we covered the syntax of SQL Self Join. Additionally, exploring techniques like SQL Group By and SQL Union expands our data manipulation capabilities and allows us to handle a wider range of challenges.