πŸŽ‰ 75% of content is free forever β€” Unlock Premium from $10/mo β†’
CW
NEWSLIVESearch All Content
πŸ’Ό Servicesℹ️ Aboutβœ‰οΈ ContactView Pricing Plansfrom $10

Self JOIN

SQL JoinsJoins🟒 Free Lesson
SQL Joins

Self JOIN

Join a table to itself to uncover relationships within the same dataset.

  • Hierarchical Queries β€” traverse employee–manager trees and org charts
  • Row Comparison β€” find duplicates, pairs, and running differences
  • Alias-Driven β€” requires distinct aliases to distinguish table instances The same table, two perspectives.

What Is a Self JOIN?

Self JOIN: Employee Hierarchyemployees (e)e.employee_id = 1e.first_name = 'Alice'e.manager_id = NULLAlias: e (employee instance)employees (m)m.employee_id = 1m.first_name = 'Alice'm.manager_id = NULLAlias: m (manager instance)Resulte.first_name | m.first_nameAlice | NULL (top)Bob | AliceCarol | AliceDan | BobON e.manager_id = m.employee_id
SELECT
    e.first_name AS employee,
    m.first_name AS manager
FROM employees e
INNER JOIN employees m
    ON e.manager_id = m.employee_id;

Syntax

SELECT columns
FROM table_name alias1
[INNER|LEFT|RIGHT] JOIN table_name alias2
    ON alias1.column = alias2.column;
ElementPurpose
alias1First instance of the table
alias2Second instance of the table
ONRelates rows within the same table
Join typeINNER, LEFT, RIGHT, or CROSS β€” choose as needed

How It Works

A Self JOIN treats one table as two separate entities. The database engine processes it like any other join, but the same physical table appears twice with different aliases.

Use CaseAlias 1Alias 2ON Condition
Employee β†’ Managere (employee)m (manager)e.manager_id = m.employee_id
Find duplicatesaba.col = b.col AND a.id < b.id
Previous rowcurrprevcurr.date > prev.date

Basic Example β€” Employee and Manager

SELECT
    e.employee_id,
    e.first_name AS employee_name,
    m.employee_id AS manager_id,
    m.first_name AS manager_name
FROM employees e
LEFT JOIN employees m
    ON e.manager_id = m.employee_id
ORDER BY e.employee_id;

Multi-Level Hierarchy

-- Walk four levels of management hierarchy
SELECT
    e.first_name  AS employee,
    m1.first_name AS direct_manager,
    m2.first_name AS skip_manager,
    m3.first_name AS director
FROM employees e
LEFT JOIN employees m1 ON e.manager_id = m1.employee_id
LEFT JOIN employees m2 ON m1.manager_id = m2.employee_id
LEFT JOIN employees m3 ON m2.manager_id = m3.employee_id;

Finding Duplicates

-- Find employees who share the same email address
SELECT
    e1.first_name,
    e1.last_name,
    e1.email
FROM employees e1
INNER JOIN employees e2
    ON e1.email = e2.email
   AND e1.employee_id < e2.employee_id;
Duplicate CheckJoin ConditionNotes
Same emaile1.email = e2.emailUnique constraint violation
Same salarye1.salary = e2.salaryCommon comparison
Same department + rolee1.dept = e2.dept AND e1.role = e2.roleComposite check

Comparing Current vs Previous Values

-- Compare each employee's current salary to their previous salary
SELECT
    e.first_name,
    jh1.start_date  AS current_start,
    jh1.salary      AS current_salary,
    jh2.start_date  AS previous_start,
    jh2.salary      AS previous_salary,
    jh1.salary - jh2.salary AS salary_change
FROM job_history jh1
INNER JOIN job_history jh2
    ON jh1.employee_id = jh2.employee_id
   AND jh1.start_date > jh2.start_date
WHERE NOT EXISTS (
    SELECT 1
    FROM job_history jh3
    WHERE jh3.employee_id = jh1.employee_id
      AND jh3.start_date > jh2.start_date
      AND jh3.start_date < jh1.start_date
)
ORDER BY e.first_name, jh1.start_date;

Finding Pairs and Combinations

-- Find all pairs of employees in the same city
SELECT
    e1.first_name AS employee1,
    e2.first_name AS employee2,
    e1.city
FROM employees e1
INNER JOIN employees e2
    ON e1.city = e2.city
   AND e1.employee_id < e2.employee_id
ORDER BY e1.city, e1.first_name;

-- Find products in the same category with similar prices
SELECT
    p1.product_name AS product1,
    p2.product_name AS product2,
    p1.category,
    ABS(p1.price - p2.price) AS price_difference
FROM products p1
INNER JOIN products p2
    ON p1.category = p2.category
   AND p1.product_id < p2.product_id
WHERE ABS(p1.price - p2.price) < 10;

Self JOIN with Different Join Types

-- LEFT Self JOIN: include employees without managers
SELECT
    e.first_name AS employee,
    m.first_name AS manager
FROM employees e
LEFT JOIN employees m
    ON e.manager_id = m.employee_id;

-- CROSS Self JOIN: every possible pair (excluding self)
SELECT
    e1.first_name AS employee1,
    e2.first_name AS employee2
FROM employees e1
CROSS JOIN employees e2
WHERE e1.employee_id != e2.employee_id;
Join TypeUse CaseRows Returned
INNEROnly matched pairsMatched rows only
LEFTAll left rows + matchesIncludes unmatched left
RIGHTAll right rows + matchesIncludes unmatched right
CROSSAll combinationsM Γ— N rows (filtered with WHERE)

Performance Considerations

FactorRecommendation
IndexesIndex all columns used in the ON condition
AliasesAlways use aliases β€” the optimizer needs them
Duplicate preventionUse < or != to avoid redundant pairs
Large tablesAvoid CROSS Self JOIN on large datasets
Execution planUse EXPLAIN to verify index usage
-- Recommended indexes for common Self JOIN patterns
CREATE INDEX idx_employees_manager_id ON employees(manager_id);
CREATE INDEX idx_employees_department_id ON employees(department_id);
CREATE INDEX idx_employees_email ON employees(email);
CREATE INDEX idx_job_history_employee_date ON job_history(employee_id, start_date);

Practice Exercises

Exercise 1: List every employee with their direct manager, including employees who have no manager.

-- Solution
SELECT
    e.employee_id,
    e.first_name AS employee_name,
    m.employee_id AS manager_id,
    m.first_name AS manager_name
FROM employees e
LEFT JOIN employees m ON e.manager_id = m.employee_id
ORDER BY e.employee_id;

Exercise 2: Find all pairs of employees who work in the same department.

-- Solution
SELECT
    e1.first_name AS employee1,
    e2.first_name AS employee2,
    d.department_name
FROM employees e1
INNER JOIN employees e2
    ON e1.department_id = e2.department_id
   AND e1.employee_id < e2.employee_id
INNER JOIN departments d
    ON e1.department_id = d.department_id
ORDER BY d.department_name, e1.first_name;

Exercise 3: Find employees who earn more than their direct manager.

-- Solution
SELECT
    e.first_name AS employee_name,
    e.salary AS employee_salary,
    m.first_name AS manager_name,
    m.salary AS manager_salary,
    e.salary - m.salary AS salary_difference
FROM employees e
INNER JOIN employees m ON e.manager_id = m.employee_id
WHERE e.salary > m.salary
ORDER BY salary_difference DESC;

See Also


Extended Learning: Beyond the Basics

Key Concepts Summary

This lesson covers fundamental concepts that serve as building blocks for more advanced topics. Master these basics before moving to intermediate and advanced material.

Prerequisite Knowledge

Before studying this topic in depth, ensure you understand:

  • Basic definitions and terminology
  • Core formulas and their derivations
  • Common applications in real-world scenarios
  • Typical pitfalls and misconceptions

Step-by-Step Walkthrough

Let us work through a complete example to solidify understanding:

Step 1: Identify the problem type. Determine which category this problem falls into and which approach is most appropriate.

Step 2: Apply the relevant formula. Substitute known values into the appropriate equation, paying attention to units and constraints.

Step 3: Simplify and solve. Work through the algebra step by step, checking each intermediate result.

Step 4: Verify the answer. Check that the solution satisfies the original conditions and makes logical sense.

Common Mistakes to Avoid

  1. Misidentifying the problem type β€” Always start by categorizing the problem
  2. Incorrect formula application β€” Verify the formula applies to your specific case
  3. Unit errors β€” Always check units are consistent throughout
  4. Sign errors β€” Pay careful attention to positive and negative values
  5. Rounding too early β€” Keep full precision until the final answer
  6. Forgetting constraints β€” Check that solutions satisfy all given conditions

Real-World Applications

Understanding this concept is essential for:

  • Advanced problem solving in examinations
  • Building intuition for more complex topics
  • Developing analytical thinking skills
  • Applying mathematical reasoning to practical situations

Practice Exercises

  1. Basic: Solve 5 problems of increasing difficulty using the core concept.
  2. Intermediate: Apply the concept to word problems with multiple steps.
  3. Advanced: Combine this concept with 2-3 other topics in a comprehensive problem.
  4. Challenge: Create your own problem that requires this concept plus additional knowledge.

Interview and Exam Tips

  • Common question patterns: Identify which exam or interview formats test this topic
  • Time management: Practice completing problems within time constraints
  • Common traps: Know the typical mistakes examiners set as distractors
  • Verification strategy: Develop a quick method to check your answer

Related Topics

Quick Reference Card

ConceptFormula/RuleKey InsightCommon Use
Core DefinitionSee main textUnderstand the intuitionFoundation for all applications
Key FormulaSee main textMemorize the derivationDirect problem solving
Special CaseSee main textRecognize when it appliesSimplifies complex problems
Edge CaseSee main textKnow the limitationsAvoids errors

Further Reading

For students who want to go deeper:

  • Standard textbooks on the subject
  • Research papers applying this concept
  • Online courses with video explanations
  • Practice problem collections with detailed solutions
CW

ChatWhole Learn Editorial Team

Written by experienced data scientists, software engineers, and educators. All content is reviewed by industry professionals to ensure accuracy and relevance.

Last reviewed: 2026-11-14

β€”
β˜†β˜†β˜†β˜†β˜†
0 ratings

Rate & Feedback

Need Expert SQL Help?

Get personalized tutoring, project support, or professional consulting.

Advertisement