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                      
Do you have a collection of Interview Questions and interested to share with us!!
Please send that collection to along with your userid / name. ThanQ
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
How to find the second largest salary in the emp database and
also How to find 3rd,4th and so on ........ in the emp database

plz mail the answer @ mak2786@gmail.com
 Question Submitted By :: Arun
I also faced this Question!!     Rank Answer Posted By  
 
  Re: How to find the second largest salary in the emp database and also How to find 3rd,4th and so on ........ in the emp database plz mail the answer @ mak2786@gmail.com
Answer
# 1
select top 1 salary from (select top 5 salary from 
emp_table order by salary desc)temptable order by salary asc
 
Is This Answer Correct ?    6 Yes 9 No
Suhail
 
  Re: How to find the second largest salary in the emp database and also How to find 3rd,4th and so on ........ in the emp database plz mail the answer @ mak2786@gmail.com
Answer
# 2
select top 10 from tbl_name order by salary desc
 
Is This Answer Correct ?    2 Yes 9 No
Gunjan Sapra
 
 
 
  Re: How to find the second largest salary in the emp database and also How to find 3rd,4th and so on ........ in the emp database plz mail the answer @ mak2786@gmail.com
Answer
# 3
To find nth maximum salry

Select salary from tbl_name a where
n-1=(Select count(distinct(count(*))) from tbl_name b
where b.salary>a.salary)
 
Is This Answer Correct ?    2 Yes 5 No
Ashish Kumar
 
  Re: How to find the second largest salary in the emp database and also How to find 3rd,4th and so on ........ in the emp database plz mail the answer @ mak2786@gmail.com
Answer
# 4
To find second highest salary
select max(sal) from emp where sal not in (select max(sal) 
from emp)

to find order
 select top 5 8 from emp order by sal desc
 
Is This Answer Correct ?    4 Yes 5 No
Saikrishna Reddy .k
 
  Re: How to find the second largest salary in the emp database and also How to find 3rd,4th and so on ........ in the emp database plz mail the answer @ mak2786@gmail.com
Answer
# 5
To find any number of Salary..

Select * From Table E1 Where
    (n-1) = (Select Count(Distinct(E2.Sal)) From Table E2 
Where E2.Sal> E1.Sal)


**Vinay Singh
 
Is This Answer Correct ?    3 Yes 3 No
Vinay Singh
 
  Re: How to find the second largest salary in the emp database and also How to find 3rd,4th and so on ........ in the emp database plz mail the answer @ mak2786@gmail.com
Answer
# 6
select top 1 sal from emp where empid in(select top 2 empid
from emp order by sal desc) order by
sal asc

if you want to get 3rd one than put 'top 3' in subquery and
same for 4th, 5th....
 
Is This Answer Correct ?    8 Yes 4 No
Pankaj Arya
 
  Re: How to find the second largest salary in the emp database and also How to find 3rd,4th and so on ........ in the emp database plz mail the answer @ mak2786@gmail.com
Answer
# 7
first find second largest salary in the emp database:

select min(sal)sal from(select sal from emp order by sal 
desc)
where rownum<3
u can find 3rd,4th... so on ,only change rownum
like rownum<4,rownum<5 ... so on
 
Is This Answer Correct ?    4 Yes 0 No
Subhranghshu Bhattacharjee
 
  Re: How to find the second largest salary in the emp database and also How to find 3rd,4th and so on ........ in the emp database plz mail the answer @ mak2786@gmail.com
Answer
# 8
another way u can find 2nd largest salary,3rd largest 
salary ... so on

 select sal from(select sal from emp order by sal desc)
where rownum<3
 minus
 select sal from(select sal from emp order by sal desc)
where rownum<2

only change rownum to find various largest salary

Subhranghshu Bhattacharjee
 
Is This Answer Correct ?    1 Yes 1 No
Subhranghshu Bhattacharjee
 
  Re: How to find the second largest salary in the emp database and also How to find 3rd,4th and so on ........ in the emp database plz mail the answer @ mak2786@gmail.com
Answer
# 9
TO FIND THE SECOND HIGHEST SAL IN EMP TABLE

select max(sal) from emp where sal not in(select max(sal) 
from emp)

TO FIND THE 3RD HIGHEST SAL IN EMP TABLE

select max(sal) from emp where sal not in(select distinct 
top 2 sal from emp order by sal desc)

TO FIND THE Nth HIGHEST SAL IN EMP TABLE

select max(sal) from emp where sal not in(select distinct 
top N-1 sal from emp order by sal desc)

yagneswara babu
yagnesh_sn@yahoo.co.in
 
Is This Answer Correct ?    11 Yes 1 No
S N Yagneswara Babu
 
  Re: How to find the second largest salary in the emp database and also How to find 3rd,4th and so on ........ in the emp database plz mail the answer @ mak2786@gmail.com
Answer
# 10
with ttble as 
(
select top 3 empid,empname,empsalary
from 
emptable 
order by empsalary desc
)
-- above will create a common table expression with name
--ttable 
-- to know about it search for commontable expression 
select top 1 empid,empname,empsalary 
from
ttable 
order by empsalary asc
 
Is This Answer Correct ?    1 Yes 3 No
Prasant Palo
 
  Re: How to find the second largest salary in the emp database and also How to find 3rd,4th and so on ........ in the emp database plz mail the answer @ mak2786@gmail.com
Answer
# 11
select salary from emp
where salary = (select max(salary) from emp
                  where salary <>(select max(salary) from emp)
 
Is This Answer Correct ?    2 Yes 0 No
Neon
 
  Re: How to find the second largest salary in the emp database and also How to find 3rd,4th and so on ........ in the emp database plz mail the answer @ mak2786@gmail.com
Answer
# 12
1. solution taken as std_id instead of salary
select max( b.std_id)   from std_info a 
inner join std_info b on a.std_id <> b.std_id 
where a.std_id > b.std_id 

2. solution
select max(std_id) from std_info  
where std_id < ( select max(std_id) from std_info)
 
Is This Answer Correct ?    0 Yes 0 No
Prashant Narvekar
 
  Re: How to find the second largest salary in the emp database and also How to find 3rd,4th and so on ........ in the emp database plz mail the answer @ mak2786@gmail.com
Answer
# 13
select max(sal) 
from (select rownum 
         from emp 
            order by sal desc)
             where rownum=2
 
Is This Answer Correct ?    1 Yes 1 No
Savita Vishwakarma
 
  Re: How to find the second largest salary in the emp database and also How to find 3rd,4th and so on ........ in the emp database plz mail the answer @ mak2786@gmail.com
Answer
# 14
for second largest salary
Select * from emp a where 2=(select count(*) from emp b
where a.sal<=b.sal)

Above mentioned query can also be used for 3 ,4 ..n

Select * from emp a where 3=(select count(*) from emp b
where a.sal<=b.sal)
 
Is This Answer Correct ?    1 Yes 0 No
Vishakha Shrivastava
 
  Re: How to find the second largest salary in the emp database and also How to find 3rd,4th and so on ........ in the emp database plz mail the answer @ mak2786@gmail.com
Answer
# 15
select p.emp_id,p.name,p.salary from
( select emp_id,name,salary,dense_rank()over( order by 
salary desc)rank
from empTable )p
where rank=2
 
Is This Answer Correct ?    1 Yes 0 No
Bobby
 
  Re: How to find the second largest salary in the emp database and also How to find 3rd,4th and so on ........ in the emp database plz mail the answer @ mak2786@gmail.com
Answer
# 16
-- Create table #temp 
-- (
--  Emp_id int identity(1,1),
--  Emp_Name varchar (100) ,
--  Emp_Salary int 
-- )

-- 
-- Insert into #Temp (Emp_Name, Emp_Salary) values 
('kunal', '2000')  
-- Insert into #Temp (Emp_Name, Emp_Salary) values 
('aditya', '5000')  
-- Insert into #Temp (Emp_Name, Emp_Salary) values 
('abhishek', '6000')  
-- Insert into #Temp (Emp_Name, Emp_Salary) values 
('kailash', '5000')  
-- Insert into #Temp (Emp_Name, Emp_Salary) values 
('rohit', '8000')  
-- Insert into #Temp (Emp_Name, Emp_Salary) values 
('rakesh', '3000')  

Select a.* From #temp a,
(Select Max(a.Emp_salary)Emp_Salary From #temp a,
(Select Max(x.Emp_salary)Emp_Salary From #temp a,
(Select a.* From #temp a,(Select Max (Emp_Salary)Emp_Salary 
From #Temp) b
Where a.Emp_Salary < b.Emp_Salary ) x ) y
where a.Emp_Salary < y.Emp_Salary)Z
where  a.Emp_Salary = z.Emp_Salary
 
Is This Answer Correct ?    0 Yes 0 No
Sandee Saxena
 
  Re: How to find the second largest salary in the emp database and also How to find 3rd,4th and so on ........ in the emp database plz mail the answer @ mak2786@gmail.com
Answer
# 17
create table #a(rn varchar(30),salary int)
insert into #a values('a',10000)
insert into #a values('b',12000)
insert into #a values('c',15000)
insert into #a values('e',25000)
insert into #a values('d',22000)
insert into #a values('f',30000)
insert into #a values('h',35000)
insert into #a values('g',32000)
insert into #a values('i',32000)
select * from #a  order by salary desc
Select * from #a a where 1=(select count(distinct b.salary) 
from #a b where  a.salary <=b.salary) 
Select * from #a a where 2=(select count(distinct b.salary) 
from #a b where  a.salary <=b.salary) 
Select * from #a a where 3=(select count(distinct b.salary) 
from #a b where  a.salary <=b.salary) 
Select * from #a a where 4=(select count(distinct b.salary) 
from #a b where  a.salary <=b.salary)
 
Is This Answer Correct ?    1 Yes 0 No
Narendra Soni
 
  Re: How to find the second largest salary in the emp database and also How to find 3rd,4th and so on ........ in the emp database plz mail the answer @ mak2786@gmail.com
Answer
# 18
SELECT MAX(VCRG_ID) FROM dbo.TRN_VEHICLE_CARGO WHERE VCRG_ID< (SELECT MAX (VCRG_ID) FROM dbo.TRN_VEHICLE_CARGO)
 
Is This Answer Correct ?    0 Yes 0 No
Prafulla Borade
 

 
 
 
Other SQL Server Interview Questions
 
  Question Asked @ Answers
 
What is a Linked Server?  1
What is the datatype returned by count(*) Asian-CERC18
What's the difference between a primary key and a unique key? Wipro8
what is normalization? what is denormalization? Satyam5
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
After using delete statement in sql query to delete some records...to retrieve the deleted records we can get using rollback command but till that where it stores means particular location name i need....(after deleting and rollback ) iGate2
Why Do you want to work in this company? HCL3
i want to join two queries....and i want to run them as one after another that is want output of first query then second , then again output of first query then second and so on...  2
What are the pros and cons of creating extended stored procedures?  1
Explain Trigger with an example?  1
Rate yourself in .NET and SQL ? Cognizent1
How to list all tables having unique constraints in any of the columns in a database.  1
Can you give an example of Stored Procedure?  2
how can i store resumes in database? HCL2
How do I list the available tables in a database I'm querying?  3
Differences between functions and stored procedures? 247Customer6
Explain Active/Active and Active/Passive cluster configurations  1
to explain sql server 2000 architecture & authentication  1
What is the difference between a Application Server and a Database Oracle2
How to select Distinct columns from the table, table having 20 columns and i want all coulmns Wipro3
 
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