September 16, 2013 at 5:27 am
Could anyone know how to write the query fro fetching the second highest salary without using top, rowcount and rank keywords?
September 16, 2013 at 5:33 am
hopefully following will help
http://pramodsingla.wordpress.com/category/interview-questions-2/
Pramod
SQL Server DBA | MCSE SQL Server 2012/2014
in.linkedin.com/in/pramodsingla/
http://pramodsingla.wordpress.com/
September 16, 2013 at 7:28 am
DENSE_RANK should do it 😉
Or SELECT 'me' if you're a bit on the joking side...
Or:
SELECT MAX(salary)
FROM t
WHERE salary < (SELECT MAX(salary) FROM t)
September 16, 2013 at 7:46 am
karunakar2351 (9/16/2013)
Could anyone know how to write the query fro fetching the second highest salary without using top, rowcount and rank keywords?
Why the strange restrictions?
--Jeff Moden
Change is inevitable... Change for the better is not.
September 16, 2013 at 7:48 am
psingla (9/16/2013)
http://pramodsingla.wordpress.com/category/interview-questions-2/%5B/quote%5D
siggemannen (9/16/2013)
DENSE_RANK should do it 😉
Neither of the proposed solutions meet the restrictions posed by the OP.
--Jeff Moden
Change is inevitable... Change for the better is not.
September 16, 2013 at 8:01 am
Jeff Moden (9/16/2013)
psingla (9/16/2013)
hopefully following will helphttp://pramodsingla.wordpress.com/category/interview-questions-2/%5B/quote%5D
siggemannen (9/16/2013)
DENSE_RANK should do it 😉Or SELECT 'me' if you're a bit on the joking side...
Or:
SELECT MAX(salary)
FROM t
WHERE salary < (SELECT MAX(salary) FROM t)
Neither of the proposed solutions meet the restrictions posed by the OP.
Jeff,I suppose the following two solution that you can find at http://pramodsingla.wordpress.com/category/interview-questions-2/ are meeting the restrictions:
declare @n smallint;
set @n =4
;with cte as (SELECT name,salary,
dense_rank() OVER( ORDER BY salary DESC) AS rowid
FROM #t1
)
SELECT *
FROM cte
WHERE rowid = @n
select distinct SALARY from #t1 as n
where (select count(distinct SALARY) from #t1 where SALARY>=n.SALARY)=4
None of these is using rank,top or rowcount
correct me if i am missing something
Pramod
SQL Server DBA | MCSE SQL Server 2012/2014
in.linkedin.com/in/pramodsingla/
http://pramodsingla.wordpress.com/
September 16, 2013 at 8:35 am
Hi
You can use rownumber
Select * from (
SELECT ROW_NUMBER() over(partition by columna order by columnname <desc or asc>) Get_Rows
FROM Table_Name
) m where Get_Rows = 2
I hope it helps.
Shaun
September 16, 2013 at 5:48 pm
psingla (9/16/2013)
Jeff Moden (9/16/2013)
psingla (9/16/2013)
hopefully following will helphttp://pramodsingla.wordpress.com/category/interview-questions-2/%5B/quote%5D
siggemannen (9/16/2013)
DENSE_RANK should do it 😉Or SELECT 'me' if you're a bit on the joking side...
Or:
SELECT MAX(salary)
FROM t
WHERE salary < (SELECT MAX(salary) FROM t)
Neither of the proposed solutions meet the restrictions posed by the OP.
Jeff,I suppose the following two solution that you can find at http://pramodsingla.wordpress.com/category/interview-questions-2/ are meeting the restrictions:
declare @n smallint;
set @n =4
;with cte as (SELECT name,salary,
dense_rank() OVER( ORDER BY salary DESC) AS rowid
FROM #t1
)
SELECT *
FROM cte
WHERE rowid = @n
select distinct SALARY from #t1 as n
where (select count(distinct SALARY) from #t1 where SALARY>=n.SALARY)=4
None of these is using rank,top or rowcount
correct me if i am missing something
Technically, you're correct. DENSE_RANK wasn't precisely mentioned. But, since RANK isn't allowed, I'd have to ask the OP if DENSE_RANK is allowed. I'm thinking "not" but I could be wrong.
--Jeff Moden
Change is inevitable... Change for the better is not.
September 16, 2013 at 5:57 pm
siggemannen (9/16/2013)
SELECT MAX(salary)
FROM t
WHERE salary < (SELECT MAX(salary) FROM t)
Apologies. I missed this. This would do it while meeting all of the expressed and implied restrictions.
--Jeff Moden
Change is inevitable... Change for the better is not.
September 17, 2013 at 12:37 am
Move to Sql Server 2012.
They have introduced OFFSET and FETCH
Example:
Select salary from table
order by salary desc
offset 1 row 'Start at 2nd row
fetch next 1 rows only 'show only the 2nd row
September 17, 2013 at 3:42 am
If you are concerned only about output then use any of these solutions (MAX or OFFSET...FETCH).
If you are also looking for performance (and your table has proper indexes and primary keys) then use (OFFSET...FETCH).
Viewing 11 posts - 1 through 10 (of 10 total)
You must be logged in to reply to this topic. Login to reply