I have the following tables:
Teachers:
+------+--------+
| id | deptId |
+------+--------+
| 1 | 2 |
+------+--------+
| 2 | 1 |
+------+--------+
Department:
+------+-----------+
| id | contactId |
+------+-----------+
| 1 | 2 |
+------+-----------+
| 2 | 6 |
+------+-----------+
Contact:
+----+--------------------+
| id | nonImportantColumn |
+----+--------------------+
| 2 | street street |
+----+--------------------+
| 4 | street2street |
+----+--------------------+
SchoolBranch:
+----+-----------+----------------+
| id | contactId | parentBranchId |
+----+-----------+----------------+
| 3 | 2 | null |
+----+-----------+----------------+
| 4 | 5 | 3 |
+----+-----------+----------------+
| 5 | 7 | null |
+----+-----------+----------------+
| 6 | 2 | 3 |
+----+-----------+----------------+
Given a teacher's ID let's say 2, I am trying to get the below depicted resultset format but don't know how to begin. I am not an expert.
+----------+-----------+----------------+--------------------+
| branchId | contactId | parentBranchId | nonImportantColumn |
+----------+-----------+----------------+--------------------+
| 3 | 2 | null | some street adr |
+----------+-----------+----------------+--------------------+
| 4 | 5 | 3 | some street adr |
+----------+-----------+----------------+--------------------+
| 6 | 2 | 3 | some street adr |
+----------+-----------+----------------+--------------------+
The result contains the data for branch id 4 and 6 as well because these branches are child branches of 3 (which is linked to our input teacher ID:2)
I am using SQL Server v 18, in case it helps.