Skip to main content
Module 5 · Lesson 2 of 418/57 in the course~14 min
Module lessons (2/4)

Self-JOIN: A Table Joined to Itself

Lesson preview5 hands-on exercises readySQL Course · Module 5 — Advanced JOINs

Query internal hierarchies by aliasing the same table in two distinct roles.

What you'll learn in this lesson

  • Join a table with itself (SELF JOIN) giving two distinct aliases to the two roles involved.
  • Navigate hierarchical parent-child structures (such as categories with parent_id).
  • Generate unique pairs within the same entity using a.id < b.id.

Overview

Can a table do JOIN with itself? Absolutely yes — and it happens whenever a row points to another row in the same table: categories with a parent category (categories.parent_id -> categories.id), employees with a manager_id, or comparisons between colleagues in the same department.

For SQL, giving the same table two different aliases (c for child and p for parent) is equivalent to reading two independent copies with distinct roles.

Sections covered in this lesson

  1. 11. Mental Model: One Table, Two Roles
  2. 22. Progressive & Annotated Examples
  3. 33. Interactive Lab: Visualize Hierarchical Self-JOIN
  4. 44. Diagnostic Verification (Quiz)
  5. 55. Progressive Practical Laboratory
Pro Lesson · Module 5 — Advanced JOINs

Unlock "Self-JOIN: A Table Joined to Itself" and the full SQL Course curriculum

Query internal hierarchies by aliasing the same table in two distinct roles.

Unlock all 5 hands-on exercises in this lesson plus every advanced WebAssembly module with automated checks.

  • Instant access to all advanced modules in SQL Course plus 17 more courses
  • Hands-on browser exercises with automated test suites and solutions
  • 14-day money-back guarantee processed via Paddle.com

14-day guarantee · Cancel anytimeModule 1, all 18 interactive games, and cheatsheets are always free.