Back to SQL Notes
Topic #99

SELF JOIN in SQL

A SELF JOIN joins a table to itself — used when rows in a table relate to other rows in that same table, like an employee and their manager (who is also an employee).

Sample Table: employees (with a manager reference)

idnamemanager_id
1AmanNULL
2Riya1
3Karan1
4Neha2

Aman has no manager (he's the top). Riya and Karan report to Aman. Neha reports to Riya.

Syntax

There's no special SELF JOIN keyword — you use a regular join, but reference the same table twice under two different aliases:

SELECT a.column, b.column
FROM table_name a
JOIN table_name b ON a.some_column = b.some_column;

Example — Employee and Their Manager's Name

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

Expected Output:

employeemanager
AmanNULL
RiyaAman
KaranAman
NehaRiya

A LEFT JOIN is used here (not INNER JOIN) so that Aman — who has no manager — still appears in the result.

Why the Aliases Are Required

Since both sides of the join come from the same physical table, SQL needs a way to tell them apart. e and m here represent "the employee row" and "the manager row" respectively — same table, two different roles in this query.

Practical Use Case

Any hierarchical or relational structure stored flat in one table: org charts (employee → manager), category trees (subcategory → parent category), or finding pairs within the same table (e.g. products in the same category with different prices).

Common Mistakes

  • Forgetting the aliases entirely — employees JOIN employees ON ... is ambiguous and will error
  • Using INNER JOIN when top-level rows (with no self-reference, like Aman here) need to still appear — use LEFT JOIN instead
  • Confusing which alias plays which role in the ON condition, especially in queries with more than two conditions

Interview Relevance

"Find each employee's manager's name" or "find employees who earn more than their manager" are classic self-join interview questions — practice both until the aliasing pattern feels automatic.

Practice Question

Using the table above, write a query to find employees who earn more than their manager (assume both have a salary column).

Want to go beyond the notes?

Join Coding Now Tech Institute's SQL course — live mentorship, real projects, and 100% placement support.

Enroll Now — Free Demo Available

SELF JOIN in SQL – FAQs

Quick answers about learning SELF JOIN in SQL in SQL.

This free note from Coding Now Tech Institute explains SELF JOIN in SQL in SQL — concept, syntax and worked code examples you can copy, run and revise before interviews.
Yes. Every SQL topic on Coding Now Tech Institute, including SELF JOIN in SQL, is 100% free with no signup required.
With focused practice, most students grasp SELF JOIN in SQL in 1–3 days from these notes; pairing it with Coding Now Tech Institute's mentor-led course takes you to job-ready depth faster.
Use the code examples in this note, then ask doubts for free on the Coding Now Tech Institute Community (/community) — expert instructors answer within 24 hours.
Call NowEnroll Now