I have already written above logic using c# code but I want to handle it at database query level as optimization.
I am using mysql database The schema of department is
Department Table
Deptid DeptName ParentDeptid
1 CEO -
2 HR 1
3 IT 1
4 Dev 3
5 QA 3
6 clerk 5
Employee table
Empid EmpName deptid
1 E1 1
2 E2 2
3 E3 3
4 E4 4
5 E5 4
6 E6 4
7 E7 5
8 E8 5
9 E9 5
10 E10 6
I need output as if I have selected deptid is 3 I want all descendent of dept 3. It means for dept id 3
deptid parentdept count
3 1 1
4 3 3
5 3 3
6 5 1
deptid = 2how are you expecting to get rows withdeptid3, 4 or 5 as of your first sample output table? Which other constraint should fire?DeptNameandParentDeptNameor it is wrong allover.3 1 1doesn't fit as deptid 3 is not a descendant of itself and has a parent of 1 as all of the other actually have 3