Skip to main content
SQL • LESSON 22

SQL SELF JOIN

How do we query hierarchical relationships within the same table?

Advanced3 Minutes390 XP
🤔 THE QUESTION

How do we query hierarchical relationships within the same table?

💡 WHAT IS IT?

A SELF JOIN joins a table to itself using distinct aliases to represent parent/child or manager/employee roles.

🎯 WHAT IS IT USED FOR?

Organizational hierarchy, category breadcrumbs, graph node-to-parent relationships.

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

🎯 Mission Objectives

Practice typing production-grade SQL code for SQL SELF JOIN.

  • SELF JOIN
  • Hierarchy mapping
  • Table Aliases (e, m)
  • Manager relationships