Self Join

SQL · Joining Multiple Tables

Theory

A self join joins a table to itself. It is not a special keyword: you list the same table twice with different aliases and treat each copy as a separate role. The classic case is an employees table where manager_id points to another row's id; joining employees e to employees m ON e.manager_id = m.id puts each employee next to their manager's name.

Any join type can be a self join. An INNER self join drops the top manager (whose manager_id is NULL); a LEFT self join keeps them with a NULL manager. Self joins also compare rows within one table: pairs of employees in the same city (with e1.id < e2.id to avoid duplicate pairs), or each day's sales next to the previous day's. For deep hierarchies of unknown depth, recursive CTEs are the better tool.

Why we use it

Some relationships live inside one table, like employee to manager or product to parent product. A self join lets you read both ends of that relationship in a single row.

When to use it

Use a self join for one level of a hierarchy or for comparing rows of the same table. For many levels, use a recursive CTE. For comparing a row with its previous or next row, window functions like LAG are usually simpler.

How it works

  1. Alias the table twice: e (employee) and m (manager)
  2. Take a row from e
  3. Look for an m row where m.id = e.manager_id
  4. Is there a match? Yes: Output employee with manager. No: Drop (INNER) or keep with NULL (LEFT).
  5. Return employee-manager pairs

Syntax

SELECT a.col, b.col
FROM table a
JOIN table b ON a.key = b.key;

Example

SELECT e.name AS employee, m.name AS manager
FROM employees e
JOIN employees m ON e.manager_id = m.id;

Key points

SQL topics

Introduction to SQL

Querying Data

Sorting Rows

Limiting Rows

Filtering Rows

Joining Multiple Tables

Grouping Rows

SET Operators

Working with Tables

Modifying Data

Views & Triggers

Conditional Expressions

SQL Subqueries

Aggregate Functions

Window Functions

String Functions

Date Functions

Math Functions

Extra Concepts

T
Tech Town
Log InSign Up Free
Home SQL Python DSA Power BI Snowflake Databricks Spark Gen AI Practice Problems Contest Blog

Master SQL from Zero to Pro

All SQL topics in one place. Click any topic in the sidebar — content opens right here without leaving the page.

{{ sectionCount }}
Sections
{{ topicCount }}
Topics
Free
Forever
Hands-on practice Practice SQL Online Solve 150 SQL interview problems in a live editor with instant checking. Start practising →
Section {{ sec.num }}

{{ sec.label }}

SQL  /  {{ current.sectionLabel }}  /  {{ current.name }}

{{ current.name }}

Theory

{{ current.def }}

Why we use it

{{ rich.why }}

When to use it

{{ rich.when }}

Diagram

{{ rich.dg.caption }}

{{ tb.title }}
{{ c.v }}
{{ rich.dg.op }}
➜
{{ rich.dg.output.title }}
{{ c.v }}
{{ nd.label }}
{{ nd.sub }}
{{ nd.label }}
{{ nd.sub }}
{{ rich.dg.left.title }}
{{ it }}
{{ rich.dg.right.title }}
{{ it }}
Flowchart — how it works
{{ nd.text }}
{{ nd.text }}
◇ {{ nd.text }}
Yes → {{ nd.yes }}
No → {{ nd.no }}
Syntax
{{ current.syntax }}
Example
{{ current.example }}
▶ Try It Live — runs against a real sample database
{{ liveError }}
{{ col }}
{{ cell }}
Key Points — click to check off as you learn
  • {{ pt.mark }} {{ pt.text }}

Share this topic