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

What is the use of for clause?

577


How to find tables without indexes?

626


What is normalization? Explain its different types?

597


How to concatenate two strings in SQL Server.

591


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






Give an example of why you would want to denormalize a database

511


Explain what are the basic functions for master, msdb, model, tempdb databases?

524


System requirements for sql server 2005 express edition?

563


How many types of local tables are there in sql server?

503


What are the built in functions in sql server?

484


What does set rowcount do?

509


Will sql server 2005 allow you to reduce the size of a column?

549


How to perform key word search in tables?

527


How to loop through result set objects using mssql_fetch_array()?

537


How to loop through returning rows?

574