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
What is the use of for clause?
How to find tables without indexes?
What is normalization? Explain its different types?
How to concatenate two strings in SQL Server.
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?
Give an example of why you would want to denormalize a database
Explain what are the basic functions for master, msdb, model, tempdb databases?
System requirements for sql server 2005 express edition?
How many types of local tables are there in sql server?
What are the built in functions in sql server?
What does set rowcount do?
Will sql server 2005 allow you to reduce the size of a column?
How to perform key word search in tables?
How to loop through result set objects using mssql_fetch_array()?
How to loop through returning rows?