SQL Self Join

Summary: in this tutorial, you will learn how to use a special kind of join called SQL self Join to join a table to itself.

Introduction to SQL self join

SQL self join is used to join or compare a table to itself. SQL self joins are used to compare values of a column with values of another column in the same table.

To construct a self join, you select from the same table twice by using the SELECT statement with an inner join or outer join clause. Because you refer to the same table twice in the same statement, you have to use table aliases. The following illustrates the syntax of a self join:

SELECT 
    column1, column2,...
FROM
    table AS A
(LEFT | INNER) JOIN
    table AS B ON join_conditionCode language: SQL (Structured Query Language) (sql)

SQL self join examples

We will use the employees table in the sample database for the demonstration.