4
Reply

To Find nth Maximum Value from a Table in Sql.

Murali Reddy

Murali Reddy

11y
1.2k
1
Reply

    Select max(salary) from tb as max

    SELECT * /*This is the outer query part */ FROM Employee Emp1 WHERE (N-1) = ( /* Subquery starts here */ SELECT COUNT(DISTINCT(Emp2.Salary)) FROM Employee Emp2 WHERE Emp2.Salary > Emp1.Salary)And Another Answer can be:DECLARE @N int SET @N = 3 -- Change the value here to pick a different salary rankSELECT Salary FROM (SELECT row_number() OVER (ORDER BY Salary DESC) as SalaryRank, SalaryFROM Salaries ) as SalaryCTE WHERE SalaryRank = @N

    select * from table1 t1 where (n-1)=(select Count(distinct(t2.salary)) from table1 t2 where t1.salary

    select * from table1 t1 where 2=(select Count(*)+1 from table1 t2 where t1.salary>t2.salary)