Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 problem in writing sql query ?????

Author  Topic 

SanjaySutar
Starting Member

18 Posts

Posted - 2008-04-09 : 06:59:29
How to find out 2nd highest salary using sql?

thanx in advance

elancaster
A very urgent SQL Yakette

1208 Posts

Posted - 2008-04-09 : 07:13:18
did your teacher give you any sample data?

Em
Go to Top of Page

raky
Aged Yak Warrior

767 Posts

Posted - 2008-04-09 : 07:41:03
DECLARE @employee TABLE ( empid INT IDENTITY(1,1), ename VARCHAR(30), EMPSAL BIGINT)
INSERT INTO @employee VALUES('RAJA',50000)
INSERT INTO @employee VALUES('RAVI',60000)
INSERT INTO @employee VALUES('RAMU',15000)
INSERT INTO @employee VALUES('SURESH',8000)
INSERT INTO @employee VALUES('MOHAN',15000)

--SELECT * FROM @employee

SELECT
EMPSAL
FROM
( SELECT ROW_NUMBER() OVER (ORDER BY EMPSAL DESC)AS 'ROWNO',EMPSAL FROM @employee ) E
WHERE
E.ROWNO = 2
THIS WORKS WELL IF YOU ARE USING SQL SERVER 2005
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2008-04-09 : 09:09:07
Many more methods
http://sqlblogcasts.com/blogs/madhivanan/archive/2007/08/27/find-nth-maximum-value.aspx

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -