third or nth maximum salary from salary table


declare @NthRow int
declare @RN int

WITH CTE AS
(
    SELECT EmpID, EmpName, EmpSalary,
           RN = ROW_NUMBER() OVER (ORDER BY EmpSalary DESC)
    FROM dbo.Salary
)
SELECT EmpID, EmpName, EmpSalary
FROM CTE
WHERE RN = @NthRow



select top  1 sid from
(select top 5 sid from TblStudents order by sid ) a
order by sid desc

select top  1 sid from TblStudents a
where 5-1=(select distinct count(sid) from TblStudents b where a.sid>b.SID  ) 

select * from 
( select *,ROW_NUMBER() over (order by sid ) rn from TblStudents  ) a
where rn=5


select * from 
( select *,RANK() over (partition by name order by sid ) rn from TblStudents  ) a
where rn=1


select * from TblStudents order by sid desc

Comments

Popular posts from this blog

loop example

View vs Partial View MVC