How to find the second largest salary in the emp database and
also How to find 3rd,4th and so on ........ in the emp database

plz mail the answer @ mak2786@gmail.com

Answer Posted / narendra soni

create table #a(rn varchar(30),salary int)
insert into #a values('a',10000)
insert into #a values('b',12000)
insert into #a values('c',15000)
insert into #a values('e',25000)
insert into #a values('d',22000)
insert into #a values('f',30000)
insert into #a values('h',35000)
insert into #a values('g',32000)
insert into #a values('i',32000)
select * from #a order by salary desc
Select * from #a a where 1=(select count(distinct b.salary)
from #a b where a.salary <=b.salary)
Select * from #a a where 2=(select count(distinct b.salary)
from #a b where a.salary <=b.salary)
Select * from #a a where 3=(select count(distinct b.salary)
from #a b where a.salary <=b.salary)
Select * from #a a where 4=(select count(distinct b.salary)
from #a b where a.salary <=b.salary)

Is This Answer Correct ?    2 Yes 2 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

Can I delete event logs?

544


Do you know what is similarity and difference between truncate and delete in sql?

577


If the job running very slow what is the action you do

1427


What is the difference between SQL notification and SQL invalidation?

591


What is filtered index?

654






What is molap and its advantage? : sql server analysis services, ssas

547


Which language is supported by sql server?

559


You are designing a strategy for synchronizing an SQL Azure database and multiple remote Microsoft SQL Server 2008 databases. The SQL Azure database contains many tables that have circular foreign key relationships?

99


How to use "begin ... End" statement structures in ms sql server?

524


List out the differences between the clustered index and non-clustered index in sql server?

505


How to test odbc dsn connection settings?

536


What is sql profiler. What are the default templates with it? : sql server database administration

521


We need to perform what steps in the following order to work with a cursor?

712


How to create hyperlink from returned sql query ?

625


Write a query for primary key constraint with identity key word?

576