write the query for find the top 2 highest salary in sql
server

Answer Posted / sums

Step1:
Create the "Salary" table,

Create table Salary
( Design_name varchar(20),
Basic_Sal int)

Step2:
Insert the values into "Salary" table,

Insert into Salary values('Office_Boy',4000)
Go
Insert into Salary values('Clerk',5000)
Go
Insert into Salary values('Head_Clerk',6000)
Go
Insert into Salary values('Accountant',7000)
Go
Insert into Salary values('Manager',8000)
Go
Insert into Salary values('PA',9000)
Go
Insert into Salary values('GM',10000)

Step3:
Write the Query aganist "Salary" table to find 'N'th
Maximum Basic Salary.

Query:

Select * from Salary s1 where (N =(select count(distinct
(s2.Basic_Sal)) from Salary s2
where s2.Basic_Sal>=s1.Basic_Sal))

N=1 --> Finds the first maximum Basic_sal
N=2 --> Finds the second maximum Basic_sal
N=3 --> Finds the Third maximum Basic_sal
.
.
.
N='N'--> Finds the 'N'th maximum Basic_sal

To find '2' maximum:

Select * from Salary s1 where (2=(select count(distinct
(s2.Basic_Sal)) from Salary s2
where s2.Basic_Sal>=s1.Basic_Sal))

Output:

Design_name Basic_sal

PA 9000

Is This Answer Correct ?    6 Yes 3 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

Write the SQL query to drop, truncate and delete table.

726


how would you improve etl (extract, transform, load) throughput?

761


Data table as parameter in sql server?

721


Please explain that what are the basic functions for master, msdb, model, tempdb and resource databases? : SQL Server Architecture

748


What is the federation in sql azure?

91






How do you clear a log file?

709


Write a query to include a constraint, to check whether the employee salary is greater than 5000?

1347


What is the difference between indexing and hashing?

754


What is exclusive locks?

705


What is master database? : SQL Server Architecture

709


What is dml command?

715


Explain throw statement in sql server 2008?

737


Explain differences between web edition and business edition?

115


Create and insert into temp table in sql server?

781


how to avoid cursors? : Sql server database administration

654