SQL self join | SQL Tutorial and Query Example

Text copied!

SQL self join


  • The syntax of the SQL self join generally looks like this :
    Here's an example of how you might use the SQL self join :

    Let's suppose we have a table :

    SELECT column_name(s)
    FROM table1 t1
    JOIN table1 t2 ON t1.column_name = t2.column_name;
    SELECT column_name(s)
    FROM table1 t1, table1 t2
    WHERE condition;
            
    SQL self join SQL self join
    SELECT Emp1.Employee_Name, Emp1.Department_Id, Emp2.Employee_Name
    FROM [Employees] Emp1
    JOIN [Employees] Emp2
    ON Emp1.Department_Id = Emp2.Department_Id
            
    SQL self join

    Frequently Asked Questions :

    A self join in SQL is when a table is joined with itself.
    The difference between inner join and self join in SQL is that inner join involves joining two separate tables, while self join involves joining a table with itself.
    An outer join in SQL combines rows from two tables even if there is no match, while a self join involves joining a table with itself.
    Self join in SQL involves joining a table with itself, while a Cartesian join combines every row from one table with every row from another table.