May 2020
Beginner
564 pages
14h 9m
English
A SELF JOIN is used to join a table to itself. This join would be useful in the case of a table containing hierarchical data such as employees and managers. Here, we have a table named Employees containing the following columns and rows:
|
EmployeeID |
FirstName |
LastName |
ManagerID |
|
1 |
Jane |
Smith |
NULL |
|
2 |
Peter |
Jones |
1 |
|
3 |
Jessica |
Lewis |
2 |
|
4 |
Donna |
Nickols |
2 |
|
5 |
Joel |
Rogers |
3 |
|
6 |
Joseph |
Edwards |
7 |
|
7 |
Ruth |
Chapman |
3 |
|
8 |
Theodore |
Clark |
6 |
|
9 |
Adam |
Berry |
6 |
|
10 |
Lucy |
Slater |
5 |
You can use a query such as this to self-join on the preceding table, as follows:
SELECT e.FirstName + ' ' + e.LastName AS EmployeeName, ...
Read now
Unlock full access