Common Table Expressions (CTE) will always save the day
with aCTE as (
SELECT
empdt.empId,
empdprt.deptName,
empdt.empExtension,
row_number() over(partition by case when empdt.empExtension is null then 1 else 0 end order by empdt.empId) as num
FROM
employeeDetails empdt
LEFT JOIN employeeDepartment empdprt ON empdt.empId = empdprt.empId
)
select
a.empId,
a.deptName,
isnull(a.empExtension,num)
from aCTE a