select * from #employee1
select * from #department1
;with cte(empname,sal,row)
as
(
select empname,empsal,ROW_NUMBER() OVER (Order by empsal desc) AS RowNumber from #employee1 e
join #department1 d on e.depid=d.depid
where d.depname='HR'
)
select * from cte where row=2
select * from EmployeeDetail IdEmployeeName 1Anurag Nayak 2 Ritika mohanty 3 Siba Dalai 4Biswa Panda Trick 1;with cte(id,name,finalIndex,startIndex)as ( select id, EmployeeName, LEN(EmployeeName) as LengthName, CHARINDEX(' ',EmployeeName) as StartIndex from dbo.EmployeeDetail ) select id,name,SUBSTRING(name,startIndex+1,finalIndex) as final from cte order by final id name final 3Siba Dalai Dalai 2Ritika mohanty 1Anurag Nayak Nayak 4Biswa Panda Panda
Trick 2 select EmployeeName from employeedetail order by REVERSE(substring(reverse(EmployeeName),1,CHARINDEX(' ',reverse(EmployeeName))))