/* ------------------------ My Meta Content Here SEO ------------------------ */

Pages

Main Menu




Wednesday, May 21, 2014

Select Top nth salary from table in sql server salary query

Top 3rd Amt Query

SELECT MIN(Amt) AS Amt FROM Salary WHERE Amt IN (SELECT TOP 3 Amt FROM Salary ORDER BY Amt DESC)

Second Highest Amount

SELECT MAX(Amt) FROM Salary WHERE Amt NOT IN (SELECT MAX(Amt) FROM Salary)

Top Nth Highest Amt

SELECT TOP 1 Amt FROM (SELECT DISTINCT TOP N Amt FROM B ORDER BY Amt DESC)PARA ORDER BY Amt

SELECT TOP 1 Amt FROM (SELECT DISTINCT TOP 5 Amt FROM B ORDER BY Amt DESC)PARA ORDER BY Amt

Using Row_Number() Method

SELECT * FROM (SELECT *, ROW_NUMBER() OVER (order by Amt DESC) AS RANK FROM Salary) v where RANK = 5;

SELECT Amt FROM (SELECT *, ROW_NUMBER() OVER (order by Amt DESC) AS RANK FROM Salary) v where RANK = 5;

No comments:

Post a Comment

Recent Posts

My Blog List