SQLforGeeks
  • Home
  • SQL
  • SQL Tutorial
  • SQL Syntax
  • Our Services
Contact
  1. SQL
  2. SQL self join

Text copied!

« Previous
Next »

SQL self join

Dec. 30, 2022, 11:21 p.m. under SQL

  • Hi! You should check SQL cross join post first.

    SELF JOIN is the type of join, where a table is joined with itself as if it were two separate tables. It is useful where a table contains hierarchical data or when comparing data within the same table.

    The syntax of the SQL self join generally looks like this :
    SELECT column_name(s)
    FROM table1 t1
    JOIN table1 t2 ON t1.column_name = t2.column_name;

    OR

    SELECT column_name(s)
    FROM table1 t1, table1 t2
    WHERE condition;
            

    • 'table1' and 'table2' refer to the two tables being joined together.

    • 'column_name(s)' refer to the selected columns from the tables being joined together.

    • 'Aliases' names (t1, t2) is used to give different names to the same table.

    • 'ON' clause is used to specify the conditions for joining two or more tables together.

    • 'WHERE' clause is used to specify the conditions to filter the data.

    SQL self join

    As seen in above image, SQL self join produces a result set that includes each rows from 'Table-A' along with any matching rows from the same table ( 'Table-A' ).

    Here's an example of how you might use the SQL self join :

    Let's suppose we have a table :

    [a] Employees Table :

    → It contains columns [Employee_Id], [Employee_Name], [Gender] and [Department_Id].

    SQL self join

    1. Let's use SQL self join to retrieve the department id's and their respective employee names by joining the [Employees] with itself based on matching values in the same table.

    2. Please run the following SQL statement :

    SELECT Emp1.Employee_Name, Emp1.Department_Id, Emp2.Employee_Name
    FROM [Employees] Emp1
    JOIN [Employees] Emp2
    ON Emp1.Department_Id = Emp2.Department_Id
            

    3. Self join creates two copies of the same table and treats it as two separate tables. it uses 'aliases' table name to differentiate between them that allows the columns from each copy of the same table to be referenced separately within the query.

    4. Above statement selects all the columns from the same table based on matching values in the 'DepartmentId' column. It returns all the rows from a table and the matching rows from the same table as the output.

    SQL self join

    • As you have seen, Only specific selected columns ( [Employee_Name], [Department_Id] from "Employees" table and [Employee_Name] from another copy of the same table ) are returned.

    • It returns all the rows from a table and all the matching rows from another copy of the same table based on the matching values in the 'DepartmentId' column.

    • As marked it screenshot above, It returned 4 rows where the [Department_Id] column contained the values '1'. This was done because '1' was the value shared by both copies of the same table tables.

    That's it! You have successfully retrieved data using 'SELF JOIN' statement.

    Frequently Asked Questions :

    What is a self join SQL?

    A self join in SQL is when a table is joined with itself.

    What is the difference between inner join and self join in SQL?

    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.

    What is outer join and self join in SQL?

    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.

    What is self join and Cartesian join?

    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.
    Thank You! You should check INSERT INTO SELECT statement post next.
    « Previous
    Next »
    RELATED :

    What is SQL

    What is T-SQL

    Difference between DBMS and Data Warehouse

    Download SQL Server

    Install SQL Server

    Download SQL Server Management Studio SSMS

    SQL Server Management Studio

    SQL Database

    Download database

    Restore database

    Backup database

    Attach database

    Detach database

    Create database

    Delete database

    Rename database

    Select database

    Database offline

    Database online

    SQL Commands

    SQL Tables

    Create table

    Truncate table

    Delete table

    Rename table

    Select table

    Alter table

    SQL Data Types

    SQL Comments

    SQL Constraints

    SQL Joins

    SQL inner join

    SQL left join

    SQL right join

    SQL full join

    SQL cross join

    INSERT INTO SELECT statement

    INSERT INTO statement

    SQL Clauses

    SELECT clause

    FROM clause

    WHERE clause

    GROUP BY clause

    HAVING clause

    ORDER BY clause

    JOIN clause

    UNION clause

    UNION ALL clause

    TOP clause

    DISTINCT clause

    SQL Operators

    SQL Arithmetic operators

    SQL Comparison operators

    SQL Logical operators

    UNION operator

    UNION ALL operator

    INTERSECT operator

    EXCEPT operator

    LIKE operator

    NOT LIKE operator

    IN operator

    NOT IN operator

    IS NULL operator

    IS NOT NULL operator

    EXISTS operator

    NOT EXISTS operator

    BETWEEN operator

    NOT BETWEEN operator

    SQL Functions

    SQL Built-In functions

    CHARINDEX function

    DATEADD function

    CONCAT function

    LEN function

    REPLACE function

    SUBSTRING function

    CASE statement

    GETDATE function

    DATEPART function

    DATEDIFF function

    CAST function

    TRY_CAST function

    CONVERT function

    TRY_CONVERT function

    ISNULL function

    NULLIF function

    COALESCE function

    SQL Window functions

    ROW_NUMBER function

    RANK function

    DENSE_RANK function

    IIF function

    CHOOSE function

    SQL Store Procedure

    Store Procedure vs. Function

    SQL Subquery

    SQL Aliases

    Temp table

    SQL Error Handling

    SQL Variables

    SQL Views

    SQL Merge

    SQL CTE

    SQL WITH TIES

    Define Transaction in DBMS

    ACID properties in DBMS

    Types of Triggers in DBMS


    • Have Some Questions?
    logo

    Elevate your data experience with SQL excellence

    Quick Links
    •  Home
    •  SQL Tutorial
    •  SQL Syntax
    •  Our Services
    Our Services
    • Web Development
    • BI Development
    • Data Warehousing
    • Data Integration ETL


    Follow Us

    GST Registered: XXAXXXXXXXZX
    Mumbai, Maharashtra, India support@sqlforgeeks.com

    © 2025 Copyright™ | All Rights Reserved | Privacy Policy