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
write the query for find the top 2 highest salary in sql 
server
 Question Submitted By :: Kuttislg
I also faced this Question!!     Rank Answer Posted By  
 
  Re: write the query for find the top 2 highest salary in sql server
Answer
# 1
select Distinct Top 2 Salary from EMP order by Salary desc
 
Is This Answer Correct ?    17 Yes 19 No
Arun Kumar K S
 
  Re: write the query for find the top 2 highest salary in sql server
Answer
# 2
select e1.sal from emp e1 where 2=(select count(distnct
(e2.sal) from emp e2 where e2.sal>=e1.sal)
 
Is This Answer Correct ?    14 Yes 8 No
Srinivas
 
 
 
  Re: write the query for find the top 2 highest salary in sql server
Answer
# 3
select ename,sal from emp E where 1=(select count(*) from
emp where E.sal<sal)
 
Is This Answer Correct ?    2 Yes 12 No
Sivashankar.soma
 
  Re: write the query for find the top 2 highest salary in sql server
Answer
# 4
select top 2 salary from emp order by salary desc
 
Is This Answer Correct ?    10 Yes 8 No
Suhail Qaiser
 
  Re: write the query for find the top 2 highest salary in sql server
Answer
# 5
or if u want 2nd highest then write this query

select Top 1 salary from ( select top 2 salary from 
emp_table order by salary desc)temptable order by salary 
asc.
 
Is This Answer Correct ?    13 Yes 5 No
Suhail Qaiser
 
  Re: write the query for find the top 2 highest salary in sql server
Answer
# 6
select max(salary) as salary1 from employee where salary < 
select max(salary) as salary2 from employee
 
Is This Answer Correct ?    4 Yes 4 No
Naren
 
  Re: write the query for find the top 2 highest salary in sql server
Answer
# 7
select * from emp e where 2>=(select count(distinct esal) 
from  emp where e.esal<=esal)
 
Is This Answer Correct ?    2 Yes 6 No
Krishna
 
  Re: write the query for find the top 2 highest salary in sql server
Answer
# 8
select Top 1 salary from ( select top 2 salary from 
emp_table order by salary desc)temptable order by salary 
desc.
 
Is This Answer Correct ?    5 Yes 5 No
Santosh Kumar
 
  Re: write the query for find the top 2 highest salary in sql server
Answer
# 9
select Top 1 salary from ( select top 2 salary from 
emp_table order by salary desc)temptable order by salary 
asc.
 
Is This Answer Correct ?    2 Yes 3 No
Santosh Kumar
 
  Re: write the query for find the top 2 highest salary in sql server
Answer
# 10
select Distinct Top 2 Salary from EMP order by Salary desc
 
Is This Answer Correct ?    1 Yes 5 No
Santhosh Kumar
 
  Re: write the query for find the top 2 highest salary in sql server
Answer
# 11
select * from emp e where 2>(select count(esal) from emp 
where e.esal<=esal)
 
Is This Answer Correct ?    1 Yes 3 No
Krishna
 
  Re: write the query for find the top 2 highest salary in sql server
Answer
# 12
select * from Emp e where 1=(select count(Distinct
d.esal)from emp d where d.esal>e.esal)

i am sure that it should work.


Any thing wrong plz inform me
 
Is This Answer Correct ?    2 Yes 5 No
Uday
 
  Re: write the query for find the top 2 highest salary in sql server
Answer
# 13
SELECT TOP 1 salary FROM (SELECT DISTINCT TOP 2 salary FROM 
employee ORDER BY salary DESC) a ORDER BY salary
 
Is This Answer Correct ?    2 Yes 2 No
Ram Murthy
 
  Re: write the query for find the top 2 highest salary in sql server
Answer
# 14
Find The 4 Top Salary in a Company
----------------------------------

CREATE TABLE EMPLOYEE2 (NAME VARCHAR2(20), SALARY NUMBER) ;

CREATE TABLE employee2 (
	name char(20),
	salary float);
INSERT INTO employee2 VALUES (
	'Raja',
	10000);
INSERT INTO employee2 VALUES (
	'Arithas',
	10000);
INSERT INTO employee2 VALUES (
	'Balaji',
	12000);
INSERT INTO employee2 VALUES (
	'Vairam',
	13000);
INSERT INTO employee2 VALUES (
	'Muthu Krishnan',
	14000);
INSERT INTO employee2 VALUES (
	'Muthu Kumar',
	15000);
INSERT INTO employee2 VALUES (
	'Ramesh',
	9000);

select e.name,e.salary from Employee2 e where 4>(select 
count(Distinct
d.salary)from employee2 d where d.salary>e.salary) order by 
e.salary desc;
 
Is This Answer Correct ?    9 Yes 1 No
Raja.p
 
  Re: write the query for find the top 2 highest salary in sql server
Answer
# 15
select * from emp e where 2=(select count(sal) from emp
where e.sal<=sal)
 
Is This Answer Correct ?    3 Yes 3 No
Ramu
 
  Re: write the query for find the top 2 highest salary in sql server
Answer
# 16
select max(salary)from emp where salary <(select sum(salary)
from emp)
 
Is This Answer Correct ?    6 Yes 6 No
K.elumalai
 
  Re: write the query for find the top 2 highest salary in sql server
Answer
# 17
select * from tbl_htl_cntry e where 2>=(select
count(distinct salary) from  tbl_htl_cntry where
e.salary<=salary)

it is aslo working syntax.
ranjeetvasu@rediffmail.com
 
Is This Answer Correct ?    6 Yes 0 No
Ranjeet Kumar Shrivastava
 
  Re: write the query for find the top 2 highest salary in sql server
Answer
# 18
select ename,sal from emp where rownum<=3 order by sal desc

Note:- every table has rownum column but it is hidden by 
default you can see it by this
select rownum from your_table_name
 
Is This Answer Correct ?    0 Yes 2 No
Neeraj Sharma
 
  Re: write the query for find the top 2 highest salary in sql server
Answer
# 19
select distinct top 2 salary from employee order by salary 
desc
 
Is This Answer Correct ?    0 Yes 0 No
Saravanan P
 
  Re: write the query for find the top 2 highest salary in sql server
Answer
# 20
select * from 
(select ID,Salary from employee order by Salary desc)
where rownum < 3
 
Is This Answer Correct ?    2 Yes 1 No
Kapil Singh Chauhan
 
  Re: write the query for find the top 2 highest salary in sql server
Answer
# 21
select * from(select * from Employee order by salary desc) where rowNum<3
 
Is This Answer Correct ?    0 Yes 0 No
Shahid
 
  Re: write the query for find the top 2 highest salary in sql server
Answer
# 22
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 ?    3 Yes 0 No
Sums
 
  Re: write the query for find the top 2 highest salary in sql server
Answer
# 23
select max(sal) from emp where sal=(select max(sal) from emp
where sal<(select max(sal) from emp));
 
Is This Answer Correct ?    0 Yes 0 No
Vijayalaxmi M Khot
 

 
 
 
Other SQL Server Interview Questions
 
  Question Asked @ Answers
 
What is one of the first things you would do to increase performance of a query? For example, a boss tells you that ?a query that ran yesterday took 30 seconds, but today it takes 6 minutes? Wipro1
1.what is stored procedure?Its significance with example? 2.Explain about index with syntax and example? plz do reply.........  1
Can you explain the types of Joins that we can have with Sql Server?  2
What is the difference between IN and EXISTS operators in SQL Server? Intelligroup3
how to get the automatic backup of the database in the sql server  3
what is maximum size of temp db? iSoft4
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
What are the magic tables in SQL Server 2000? Infogain5
What are user defined datatypes and when you should go for them?  1
How can i give the restrictions for the data entry, if i wish to enter only I ,II, III, IV in the grade actegory of student table?  3
Alternative way to DetDate() function?  4
Explain DBMS, RDBMS?  4
What is a materialized view?  2
can foreign key take role of primary key? TCS4
How to give a user the option of importing Excel and a delimited text file into a SQL Server Database without manually using SQL DTS? GE1
which query u can write to sql server doesn't work inbetween 7.00PM to nextday 9.00AM Wipro4
What is SQL Profiler what is the use of it? 247Customer2
What value could be assigned to Varchar Type?  3
What are defaults? Is there a column to which a default can't be bound?  2
Explain Different types of Projects?  1
 
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