Ad space (banner)
🗄️SQL Lessons
Lesson 23 / 50

Self Joins

This lesson covers self joins — a technique for relating rows within a single table to each other. It's for anyone searching "SQL self join tutorial."

A self join joins a table to itself, useful for relationships within one table — like "senior and junior" or "manager and direct report."

The example references the employees table twice using aliases e and m, matching each employee's manager_id against the manager's own id, so it can display employee and manager names side by side. The key idea: giving a table two different aliases lets you reference it twice.

A common early mistake is not understanding why the same table needs two different aliases — since you're referencing the same table twice, each reference needs its own name to tell them apart. Without aliases, SQL can't tell which "copy" of the table a column belongs to. This pattern shows up whenever hierarchical relationships — an org chart, a family tree — are stored inside a single table, and once it clicks, it becomes a genuinely powerful technique.

SQL
OUTPUT

💡 The SQL engine may take a few seconds to load the first time you run code.

Ad space (banner)
Ad space (in-article)