Hierarchical Tree Structures in MySQL

Adjacency list vs. nested set — explore the same org chart two ways.

Org chart — rendered from parent_id pointers
  • Company118
    • Engineering29
      • Backend36
        • API Team45
      • Frontend78
    • Operations1017
      • HR1112
      • Finance1316
        • Payroll1415
Nested Set — tree derived from lft / rgt ranges, with depth
Company [1, 18] · depth 0
├── Engineering [2, 9] · depth 1
│   ├── Backend [3, 6] · depth 2
│   │   └── API Team [4, 5] · depth 3
│   └── Frontend [7, 8] · depth 2
└── Operations [10, 17] · depth 1
    ├── HR [11, 12] · depth 2
    └── Finance [13, 16] · depth 2
        └── Payroll [14, 15] · depth 3