ALLInterview.com :: Home Page KalAajKal.com
 Advertise your Business Here     
Browse  |   Placement Papers  |   Company  |   Code Snippets  |   Certifications  |   Visa Questions
Post Question  |   Post Answer  |   My Panel  |   Search  |   Articles  |   Topics  |   ERRORS new
   Refer this Site  Refer This Site to Your Friends  Site Map  Bookmark this Site  Set it as your HomePage  Contact Us     Login  |  Sign Up                      
tip   SiteMap shows list of All Categories in this site.
Google
 
Categories  >>  Software  >>  Databases  >>  SQL Server
 
 


 

 
 Oracle interview questions  Oracle Interview Questions
 SQL Server interview questions  SQL Server Interview Questions
 MS Access interview questions  MS Access Interview Questions
 MySQL interview questions  MySQL Interview Questions
 Postgre interview questions  Postgre Interview Questions
 Sybase interview questions  Sybase Interview Questions
 DB Architecture interview questions  DB Architecture Interview Questions
 DB Administration interview questions  DB Administration Interview Questions
 DB Development interview questions  DB Development Interview Questions
 SQL PLSQL interview questions  SQL PLSQL Interview Questions
 Databases AllOther interview questions  Databases AllOther Interview Questions
Question
can anybody tell us, how to select 2nd max salary from 
table.
my id is ashish.akk@gmail.com
 Question Submitted By :: Ashish
I also faced this Question!!     Rank Answer Posted By  
 
  Re: can anybody tell us, how to select 2nd max salary from table. my id is ashish.akk@gmail.com
Answer
# 1
CREATE TABLE T1(SALARY int, [NAME] varchar(50)) 
SELECT TOP 1 SALARY FROM (SELECT TOP 2 SALARY FROM T1 ORDER 
BY SALARY DESC)T1 ORDER BY SALARY
 
Is This Answer Correct ?    6 Yes 2 No
Preeti Chauhan
 
  Re: can anybody tell us, how to select 2nd max salary from table. my id is ashish.akk@gmail.com
Answer
# 2
If you need the second highest salary only
then following will also work,

select Max(Salary) from TableName
where Salary not in(
Select Max(Salary) from TableName)
 
Is This Answer Correct ?    11 Yes 2 No
Dileep.t
 
 
 
  Re: can anybody tell us, how to select 2nd max salary from table. my id is ashish.akk@gmail.com
Answer
# 3
hi,
my id is ravi106109@gmail.com

select sal from emp a
where 1=(select count(b.sal) from emp b
where a.sal<b.sal) order by sal desc;

and also dileep is write
 
Is This Answer Correct ?    2 Yes 2 No
Tulasi Ravi Kumar
 
  Re: can anybody tell us, how to select 2nd max salary from table. my id is ashish.akk@gmail.com
Answer
# 4
In the case more than one highest SALARY, only the Dilip’s 
answer is correct.
 
Is This Answer Correct ?    2 Yes 0 No
Arun Kumar Ks
 
  Re: can anybody tell us, how to select 2nd max salary from table. my id is ashish.akk@gmail.com
Answer
# 5
I think Daleep answer is not so precise, if you want 3rd or 
ny specified answer, this only can only be seems right for 
2nd highest salary, else, its not optimise query.I found 
Preeti's query more optimistic and accurate.
 
Is This Answer Correct ?    0 Yes 0 No
Neeru
 
  Re: can anybody tell us, how to select 2nd max salary from table. my id is ashish.akk@gmail.com
Answer
# 6
SELECT Min(Sal) FROM (SELECT TOP 2 Sal FROM emp ORDER BY 
Sal desc) as Temp

or

SELECT Min(Sal) FROM emp WHERE Sal IN(SELECT TOP 2 Sal FROM 
emp ORDER BY Sal DESC)
 
Is This Answer Correct ?    1 Yes 3 No
Sabin V Jacob
 
  Re: can anybody tell us, how to select 2nd max salary from table. my id is ashish.akk@gmail.com
Answer
# 7
1 My Second Answer.

Select Top 1 * From Tablename where salary in
(select top 2 salary from tablename order by salary desc)
order by salary asc.
 
Is This Answer Correct ?    0 Yes 3 No
Kumar
 
  Re: can anybody tell us, how to select 2nd max salary from table. my id is ashish.akk@gmail.com
Answer
# 8
select max(salary) from table where salary<(select max
(salary) from table)
 
Is This Answer Correct ?    1 Yes 1 No
Kumar_kisna
 
  Re: can anybody tell us, how to select 2nd max salary from table. my id is ashish.akk@gmail.com
Answer
# 9
we have to use distinct key word there is a possibility 
that two person  having same salary.



select top 1 sry  from (select distinct top 2 sry  from emp 
order by sry desc)t1 order by sry asc
 
Is This Answer Correct ?    1 Yes 1 No
Dhaval
 
  Re: can anybody tell us, how to select 2nd max salary from table. my id is ashish.akk@gmail.com
Answer
# 10
This query is generalised query if u replace 2 by 100 , 
then you will get 100th max salary


select top 1 salary from employee where salary in( select 
top 2 salary from employee order by salary desc) order by 
salary asc
 
Is This Answer Correct ?    0 Yes 1 No
Kavitha.r
 
  Re: can anybody tell us, how to select 2nd max salary from table. my id is ashish.akk@gmail.com
Answer
# 11
In SQL Server 2000 Query:
-------------------------

select max(salary) from Employee where salary not in(select 
max(salary) from employee)

This Query only selects the 2'nd Higehest Salary in that 
table. So, you will try...
 
Is This Answer Correct ?    0 Yes 0 No
Selvaraj.v
 
  Re: can anybody tell us, how to select 2nd max salary from table. my id is ashish.akk@gmail.com
Answer
# 12
Select Top 1 * From Tablename where salary in
(select top 2 salary from tablename order by salary desc)
order by salary asc
 
Is This Answer Correct ?    1 Yes 0 No
Rajendra
 
  Re: can anybody tell us, how to select 2nd max salary from table. my id is ashish.akk@gmail.com
Answer
# 13
select max(salary) as secondMax from employee  where id <>
(select max(salary) from employee)
 
Is This Answer Correct ?    0 Yes 0 No
Harikumar
 
  Re: can anybody tell us, how to select 2nd max salary from table. my id is ashish.akk@gmail.com
Answer
# 14
select max(salary) as secondMax from employee  where salary 
<> (select max(salary) from employee)
 
Is This Answer Correct ?    0 Yes 0 No
Harikumar
 
  Re: can anybody tell us, how to select 2nd max salary from table. my id is ashish.akk@gmail.com
Answer
# 15
select max(sal) from tablename where sal<(select
max(sal)from tablename)
 
Is This Answer Correct ?    1 Yes 0 No
Divya
 
  Re: can anybody tell us, how to select 2nd max salary from table. my id is ashish.akk@gmail.com
Answer
# 16
To find Top 3 max Sal.... to put any number on the place of
3 to find nth max sal...


select top 3 sal from emp order by sal desc
 
Is This Answer Correct ?    0 Yes 1 No
Kumar Amit Ranjan
 
  Re: can anybody tell us, how to select 2nd max salary from table. my id is ashish.akk@gmail.com
Answer
# 17
select max(salary)from tablename where salary <(select
max(salary)from tablename)
 
Is This Answer Correct ?    0 Yes 0 No
Elumalai.k
 
  Re: can anybody tell us, how to select 2nd max salary from table. my id is ashish.akk@gmail.com
Answer
# 18
select top 1 *  from (select top 2 * from emp order by sal 
desc  ) a order by sal asc  



Plz try this.
 
Is This Answer Correct ?    0 Yes 0 No
Darshan Shah
 
  Re: can anybody tell us, how to select 2nd max salary from table. my id is ashish.akk@gmail.com
Answer
# 19
select a.sal from emp a
where 2=(select count(distinct b.sal) from emp b
where a.sal<= b.sal)


for getting nth max sal ..replace 2 by n


all the best
 
Is This Answer Correct ?    0 Yes 0 No
Syaam
 
  Re: can anybody tell us, how to select 2nd max salary from table. my id is ashish.akk@gmail.com
Answer
# 20
select e.* from 
(select row_number() over (order by salary desc) as Rno,* 
from employee)e
where e.Rno = 2
 
Is This Answer Correct ?    1 Yes 0 No
Naufal Basheer
 
  Re: can anybody tell us, how to select 2nd max salary from table. my id is ashish.akk@gmail.com
Answer
# 21
Select max(sal) from emp
where id='1233' and Sal< (select max(sal) from emp)
 
Is This Answer Correct ?    1 Yes 0 No
Mahesh
 

 
 
 
Other SQL Server Interview Questions
 
  Question Asked @ Answers
 
How to write a script for upate the data in prod , i have 50000 row are there TCS2
what is the purpose of creating view is sql server 2000 Wipro8
How to determine the service pack currently installed on SQL Server? HCL3
What is the difference between a stored procedure and a user defined function in sql server? Millennium2
Alternative way to DetDate() function?  4
How to restart SQL Server in single user mode? How to start SQL Server in minimal configuration mode?  1
How do you persist objects, permissions in tempdb  1
What's the purpose of Stored Procedure? Wipro4
What is Query Execution Plan? How does it help optimize or tune a database driven application? Accenture1
Table student containing 2 columns,Join date,Employee name. Under join date 4 rows r ter =1-jan-2008,2-feb-2008,3-mar- 2008,4-feb-2008.Under Employee name 4 difeerent names jaison,robin,binoy,rahul Result set is, Table containing 4-column name=jan,feb,mar,april,,beneath these months count is given as 1,2,1,0 means these counts representing number of emplooyees joined in a month(january 1employee,february 2 employee,march 1 employee,april 0 employee) Can you give me the required sql query Wipro3
logshipping is Any difference 2000 and 2005?  1
What are the type of Indexes? which one is best, why?  5
How to work on DTS?what is the main requirement? ivan1
What is transaction ? Give me one example. Melstar7
What is the Query of getting last 10 transaction Reports (like insert, update, Delete Data from Tabele) ? Wipro4
Can we call SP inside a query? Wipro4
Difference between Function and Stored Procedure? C1-India4
What are sub-queries? Give example? In which case sub-queries are not feasible? Infosys3
How to display n-1 columns from n number of columns, from a single table in MS SQL server 2005?  1
Explian different types of BACKUPs avaialabe in SQL Server? Given a particular scenario, how would you go about choosing a backup plan? HCL2
 
For more SQL Server Interview Questions Click Here 
 
 
 
 
 
   
Copyright Policy  |  Terms of Service  |  Help  |  Site Map 1  |  Articles  |  Site Map  |   Site Map  |  Contact Us interview questions urls   External Links 
   
Copyright © 2007  ALLInterview.com.  All Rights Reserved.

ALLInterview.com   ::  Forum9.com   ::  KalAajKal.com