views:

61

answers:

3

One table with EmpSalary in Employee Table. I need to find the second largest Salary what is paid by the company.?

How to find the Second largest value(Salary) from a table.?

+6  A: 
;WITH CTE AS ( SELECT ROW_NUMBER() OVER (ORDER BY SortColumn DESC) AS RowNumber, * 
               FROM YourTable)
SELECT * FROM CTE WHERE RowNumber = 2
anivas
wats CTE in this??
pvaju896
Common Table Expression. You can use any name. CTE_SALARY for instance.
anivas
Wats happening here.. wen we running it..??
pvaju896
ROW_NUMBER() OVER (ORDER BY ...) Clause orders your data based on the order by specification and auto-numbers it in 'RowNumber' column. You are then picking the second row from it.
anivas
This is returning the second row but that might not be the second largest salary if more than 1 person has the highest salary as per Askashm's comment on question
Craig
+3  A: 

Try this: this should give the second largest salary:

SELECT MAX(EmpSalary) FROM employee WHERE EmpSalary < (SELECT MAX(EmpSalary) FROM employee);

Anurag
I'm assuming you are looking for a very simple answer and not needing CTEs
Anurag
this works better..!! But how..?/ Dint get this.!!
pvaju896
There are two queries in this one: the inner query (SELECT MAX(EmpSalary) FROM employee) returns the largest salary. The outer query has a where condition that returns the largest value in the table which is less than value returned by inner query. Since inner query returns largest salary, outer query returns second largest value. Hope I have explained it properly.
Anurag
Yup.. ThanX a lot bro..!!
pvaju896
A: 

select top(1) prodMrp from Product where not prodMrp = (select top(1) prodMrp from Product order by prodMrp DESC ) order by prodMrp DESC

pvaju896
If you're gonna copy and paste, at least take the trouble to make it *look* relevant.
cHao
This works fine for me..!! You can try..
pvaju896