Skip to main content
SQL โ€ข LESSON 22

SQL SELF JOIN

How do we query hierarchical relationships within the same table?

Advancedโฑ 3 Minutesโญ 390 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