Module lessons (2/4)
Self-JOIN: A Table Joined to Itself
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
categorieswithparent_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
- 11. Mental Model: One Table, Two Roles
- 22. Progressive & Annotated Examples
- 33. Interactive Lab: Visualize Hierarchical Self-JOIN
- 44. Diagnostic Verification (Quiz)
- 55. Progressive Practical Laboratory
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.