SQL Keywords

SQL SELF JOIN

What is a SQL SELF JOIN?

A SELF JOIN joins a table to itself so you can compare or combine rows within the same table.
Sign up to get up to date news on SQL keywords
Welcome to the Galaxy, Guardian!
You'll be receiving a confirmation email

Follow us on twitter :)
Oops! Something went wrong while submitting the form.

Compatible dialects for SQL SELF JOIN: PostgreSQL, MySQL, SQL Server, Oracle, SQLite, MariaDB, Snowflake, BigQuery, Redshift

SQL SELF JOIN Full Explanation

A SELF JOIN is not a separate SQL keyword but a technique that uses any standard JOIN (INNER, LEFT, etc.) to relate a table to itself. By assigning different aliases to the same table, you create two logical instances that the optimizer can treat as separate sources. This is useful when rows in a table reference other rows in the same table (such as parent-child hierarchies, predecessor-successor relationships, or time-based comparisons). A SELF JOIN behaves like the JOIN type you specify (INNER, LEFT, RIGHT) and inherits its performance characteristics, including the need for indexed join columns to avoid full table scans. Because the table is scanned twice, large tables can be expensive to self-join without proper indexing. SELF JOINs can be combined with aggregation, window functions, and subqueries. Most SQL dialects require aliases to distinguish the two table instances; failing to alias will raise an ambiguous-column error or reference-cycle warning.

SQL SELF JOIN Syntax

SELECT t1.column_a,
       t2.column_b
FROM   table_name AS t1
JOIN   table_name AS t2
       ON t1.join_key = t2.join_key;

SQL SELF JOIN Parameters

Example Queries Using SQL SELF JOIN

-- 1. Find employees and their managers stored in the same table
SELECT e.employee_id,
       e.employee_name,
       m.employee_name AS manager_name
FROM   employees AS e
LEFT JOIN employees AS m
       ON e.manager_id = m.employee_id;

-- 2. Detect duplicate email addresses in a users table
SELECT u1.user_id AS id1,
       u2.user_id AS id2,
       u1.email
FROM   users AS u1
JOIN   users AS u2
       ON u1.email = u2.email
      AND u1.user_id < u2.user_id;

-- 3. Compare sales this month vs last month for the same product
SELECT cur.product_id,
       cur.month  AS current_month,
       cur.sales  AS current_sales,
       prev.sales AS previous_sales,
       cur.sales - prev.sales AS growth
FROM   monthly_sales AS cur
JOIN   monthly_sales AS prev
       ON cur.product_id = prev.product_id
      AND cur.month = prev.month + INTERVAL '1 month';

Expected Output Using SQL SELF JOIN

  • Each query returns a result set combining rows that meet the ON condition while treating the single source table as two logical tables
  • For example 1, every employee row is matched with a manager row, yielding a list of employees alongside their managers
  • Nulls appear in manager_name for top-level employees when LEFT JOIN is used

Use Cases with SQL SELF JOIN

  • Query hierarchical data such as organizational charts or category trees
  • Compare current and previous period metrics stored in the same table
  • Identify duplicates by joining on a unique-constraint column and filtering out identical row IDs
  • Find relational chains like friends-of-friends in social graphs
  • Combine parent and child attributes in a single result set without denormalizing the table

Common Mistakes with SQL SELF JOIN

  • Omitting table aliases, leading to ambiguous column references
  • Forgetting to qualify columns with the correct alias, returning wrong or duplicate data
  • Using INNER JOIN when unmatched rows are needed, resulting in lost data
  • Joining on non-indexed columns, causing performance issues on large tables
  • Misinterpreting SELF JOIN as a unique keyword rather than a standard JOIN pattern

Related Topics

JOIN, INNER JOIN, LEFT JOIN, RIGHT JOIN, CROSS JOIN, TABLE ALIASES, HIERARCHICAL QUERIES

First Introduced In

SQL-92 (conceptual join technique)

Frequently Asked Questions

What is the difference between SELF JOIN and INNER JOIN?

A SELF JOIN is a pattern that can use INNER JOIN or any other join type. The term simply means the table appears on both sides of the JOIN.

Do I need table aliases for a SELF JOIN?

Yes. Aliases are required so the database engine can distinguish between the two logical instances of the same table and resolve column names unambiguously.

How do I improve SELF JOIN performance?

Create indexes on the join columns, limit the rows scanned with WHERE clauses, and avoid selecting unneeded columns to reduce I/O.

Can I chain multiple SELF JOINs on the same table?

Yes. You can join the same table multiple times with different aliases to traverse deeper hierarchical levels or compare several periods at once.

Sign up to get up to date news on SQL keywords
Welcome to the Galaxy, Guardian!
You'll be receiving a confirmation email

Follow us on twitter :)
Oops! Something went wrong while submitting the form.
Trusted by top engineers on high-velocity teams
Aryeo Logo
Assort Health
Curri
Rubie Logo
Bauhealth Logo
Truvideo Logo

Check out other commonly used SQL Keywords!