SQL TutorialLesson 31Free preview
SQL Self Join
This lesson is part of SQL · All levels
Summary
Self join is combining a table with itself, usually for hierarchical data such as its employees and managers. Since the table names are the same, at least one side must be given an alias to keep the query ambiguous. Logically this is just a regular joint that happens to use the same table.
Key points
- Self join combines a table with itself.
- It's mandatory to give different aliases on each side, for example a and b.
- Suitable for hierarchical structures such as managers-employees or parent-child categories.
- It could be either the inner joint or the left joint.
- LEFT JOIN is used when a line without parent or manager still needs to appear.
Full lesson
Code examples and quizzes
Full lessons, code examples, and quizzes are available to subscribers. 5 questions.