A self-join in SQL allows you to join a table to itself. This enables you to compare rows within the same table as though you were joining two different tables. Self-joins are useful for comparing values in a hierarchical table, finding duplicate values, or comparing rows with other rows in the same table.

What Is Self-Join in SQL?

A self-join joins a table to itself, creating a virtual second instance of that table that can be referenced as a different table. To perform a self-join, you need to alias the table references; otherwise, SQL will not know how to distinguish the two instances of the table.

The join predicates in a self-join work just like any other join predicate. You can join columns across the two instances of the table to compare values and return related rows. The power of self-joins comes from the ability to match records from a table to other records in that same table.

Syntax

The self-join syntax involves aliasing the table references and joining them together, just like you would join two different tables.

SELECT t1.column1, t2.column2

FROM table AS t1

JOIN table AS t2 ON t1.id = t2.t1id

  • SELECT t1.column1, t2.Column: You select the columns you want from each instance of the table. Use the aliases to distinguish columns from the two table instances.
  • FROM table AS t1: defines the first reference to the table and aliases it as t1. This is the left side of the join.
  • JOIN table AS t2: Joins the second reference to the table, aliasing it as t2. This right side will be joined to the left side.
  • ON t1.id = t2.t1id: join predicate comparing the id column from the left table instance to the t1id column from the right instance. You can join any shared column.

So, in summary, you are creating two aliased references to the table that can then be treated as separate tables in the join syntax. The join predicate matches related rows from the two instances.

SQL Self-Join Examples

Here are some examples of using self-join in SQL:

Compare Columns In The Same Table

SELECT e1.name, e1.salary, e2.name, e2.salary

FROM employees e1

JOIN employees e2 ON e1.department_id = e2.department_id

AND e1.salary < e2.salary

This self-reference compares employee salaries in the same department. It allows you to match records where the salary is lower to other records with higher salaries.

Identify Duplicate Rows

SELECT t1.id, t1.name, t2.id, t2.name 

FROM table t1

JOIN table t2 ON t1.name = t2.name AND t1.id <> t2.id

This self-join example demonstrates how to use a self-join to identify duplicate name values, which could indicate an error in data entry or some other issue.

Hierarchical Data

SELECT e.first_name, m.first_name AS manager

FROM employees:

JOIN employees m ON e.manager_id = m.id

Self-joins are extremely useful for hierarchical data where you want to match records to related records. This example adds employees to their manager's record.

Comparing Historical Snapshot Data

You can use self-join in SQL with date-based tables to compare snapshots over time. For example:

SELECT t1.date, t1.value, t2.date, t2.value  

FROM metrics t1

JOIN metrics t2 ON t1.account_id = t2.account_id

AND t1.date = '2020-01-01'

AND t2.date = '2021-01-01'

Get access and complete hands-on experience on a plethora of software development skills in our unique Job Guarantee bootcamp. Get job-ready with HackerEarth and HIRIST by enrolling in our comprehensive Full Stack Java Developer Masters program today!

Conclusion

A self-join course powered by Simplilearn is a powerful technique for comparing and matching data within the same SQL table. By joining a table to itself and aliasing the references, you can leverage join logic to identify duplicates, hierarchical relationships, trends over time, and more. As this course has demonstrated, self-joins have many uses and open up creative options for querying your data. There are endless possibilities for what you can achieve with self-joint operations.

If you are looking to enhance your software development skills further, we would highly recommend you to check out Simplilearn’s Full Stack Java Developer. In collaboration with IBM, this course can help you hone the right skills and make you job-ready. 

If you have any questions or queries, feel free to post them in the comments section below. Our team will get back to you at the earliest.

FAQs

1. What Is the difference between self and cross-join?

A self-join matches a table to itself to compare or relate data within the same table, using aliases to avoid ambiguity. A cross-join produces a cartesian product, matching every row from multiple tables without needing a join condition relating to those tables at all.

2. Can I perform self-joins on any table?

Yes, you can perform a self-join on any table in SQL because a self-join simply involves joining a table to itself rather than another table. The key requirements are that you reference or alias the table twice in the query so that you have two instances that can then be joined or compared and that you establish a meaningful join condition between those instances. As long as those aliases and join logic are in place, any table can be self-joined.

3. What's the difference between inner-join and left-join in self-joins?

An inner-join only returns rows that match on both sides, while a left-join returns all rows from the left table and only matching ones from the right table

4. Can I perform self-joins across multiple columns?

Yes, you can join multiple columns between the table instances in a self-join. The join predicate can relate data across many fields, not just a single column.

5. Are self-joins limited to two instances of the same table?

No, self-joins are not limited to just two instances of the same table. You can create additional aliases to join a table to itself multiple times in the same query if needed.

Our Software Development Courses Duration And Fees

Software Development Course typically range from a few weeks to several months, with fees varying based on program and institution.

Program NameDurationFees
Caltech Coding Bootcamp

Cohort Starts: 17 Jun, 2024

6 Months$ 8,000
Full Stack Java Developer

Cohort Starts: 14 May, 2024

6 Months$ 1,449
Automation Test Engineer

Cohort Starts: 29 May, 2024

11 Months$ 1,499
Full Stack Developer - MERN Stack

Cohort Starts: 18 Jun, 2024

6 Months$ 1,449