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       Ask Questions on ANYTHING, that arise in your Daily Life at     FORUM9.COM
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 ?    7 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 ?    12 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 ?    3 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
 
what is a stored procedure and trigger?  2
Can you give an example of Stored Procedure?  2
What is The Use Of TIMESTAMP DataType in SQL Server 2005?  3
what are default? Is there a column to which a default cant be bound?  1
how to connect sybase to sql server 2005?. ABC1
Please give me the SP for the below scenario. I have two tables named Table1 and Table2...I need to fetch record by record from Table1 and insert the record in to table2 where the value in the sno column of the table1 is even number. Value-Labs4
How will u get 5 years back record?  4
what is Constraint? How many types of constraints in SQL ? NIIT7
What about UPDATESTATISTICS ? Intelligroup2
what are the joins,primary key,foriegn key, candidate key, super key and expain them? Polaris2
How can count the string ? for ex: If i have string like 'bhaskar' then i need like b:1 h:1 a:2 s:1 k:1 r:1 please give any idea on that TCS5
Let’s say the table in the database is named as TBL_Register. The fields in this table include: 1. User_Name, 2. User_Telephone, 3. Register_Date The field Register_Date stores the current date and time of the registration. Write the SQL statement that inserts the data into the table. Techno-Solutions2
if we have a column (Key) in a table. and values of that column is Key 1 1 1 2 2 3 3 4 4 5 5 5 and we want to show the data after query..like.. 1(3) 2(3) 3(2) 4(2) 5(3) how many times a single term comes.. Rolta5
Hi SQL gurus, i am working for an MNC... My team is having a problem in sql server. when user slects date prompts from jan 1st to april 30, it should display all months data like : jan aa feb bb mar cc but when it comes to april its taking data like : jan aa feb bb mar cc apr dd...and so on means its taking data again from jan to april which we dont want. we want the data only april month as we are getting jan, feb and mar... can any one write the code to relsove the issue please would be greatful if you can send to shiva_sans@yahoo.co.in and also please send your email also ...so that we will be in touch for any kind of queries ... Thanks a lot in Advance !!!  1
Can you have a nested transaction? HCL3
How do you know which index a table is using?  4
I Have Employee table having column name as ID,SALARY how to get second max salary from employee table with id ex ID SALARY 1 20000 7 37000 2 5000  11
What are tasks? Wipro1
What is the difference between views and stored procedures? Can we have input parameters for views?  3
What is the use of CASCADE CONSTRAINTS?  2
 
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