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
how to select 5 to 7 rows from a table, which contains 10 rows?
 Question Submitted By :: Durga Prasad
I also faced this Question!!     Rank Answer Posted By  
 
  Re: how to select 5 to 7 rows from a table, which contains 10 rows?
Answer
# 1
Should Implement ROW_NUMBER() method.

Just take this Example.

Create table emp(empid int, ename varchar(50),salary numeric
(9,2)

Insert into emp values(1,'Employee1',10000)
Insert into emp values(2,'Employee2',20000)
.
.
Insert into emp values(10,'Employee10',100000)

   Consider the above table which have 10 records. Now u 
want to select 5,6,7 Rows in this table. Just try this 
query.

SELECT E.EmpID,E.EName,E.Salary FROM
(
SELECT ROW_NUMBER() OVER(ORDER BY EmpID ASC) AS Rno, * 
FROM   emp
) E
WHERE E.Rno >= 5 and E.Rno <= 7

Thats all.
If anyone have other such good idea kindly share........
 
Is This Answer Correct ?    13 Yes 2 No
Soorai Ganesh
 
  Re: how to select 5 to 7 rows from a table, which contains 10 rows?
Answer
# 2
You can achieve this by using following Query

select * from(select top 3 * from employees where 
employeeid in(select  top 7 employeeid from employees where 
employeeid not in(select top 4 employeeid from employees 
order by employeeid)))e order by employeeid.

Thanks
AyyappanRamachandran
 
Is This Answer Correct ?    7 Yes 1 No
Ayyappanramachandran
 
 
 
  Re: how to select 5 to 7 rows from a table, which contains 10 rows?
Answer
# 3
select * from 
       (select top 3 * from emp where empid in
            (select top 7 * from emp where empid not in 
   
(select top 4 * from emp order by empid)) order by empid
 
Is This Answer Correct ?    0 Yes 4 No
Manas
 
  Re: how to select 5 to 7 rows from a table, which contains 10 rows?
Answer
# 4
select * from emp where EmpID between 5 and 7
 
Is This Answer Correct ?    4 Yes 4 No
Guest
 
  Re: how to select 5 to 7 rows from a table, which contains 10 rows?
Answer
# 5
select rnum, d.* from dept d, (select rownum rnum , deptno 
from dept ) e
where d.deptno = e.deptno and rnum between 5 and 7;
 
Is This Answer Correct ?    2 Yes 2 No
Sajida
 
  Re: how to select 5 to 7 rows from a table, which contains 10 rows?
Answer
# 6
select * from emp where empid >=5 and empid <=7
 
Is This Answer Correct ?    2 Yes 4 No
Sam
 
  Re: how to select 5 to 7 rows from a table, which contains 10 rows?
Answer
# 7
CREATE TABLE Names 
( 
    NameID INT IDENTITY(1, 1) 
        PRIMARY KEY CLUSTERED, 
    FName VARCHAR(32) 
) 
GO 
 
SET NOCOUNT ON 
INSERT Names(FName) VALUES('Aaron') 
INSERT Names(FName) VALUES('Greg') 
INSERT Names(FName) VALUES('Alex') 
INSERT Names(FName) VALUES('Luan') 
INSERT Names(FName) VALUES('John') 
INSERT Names(FName) VALUES('Todd') 
INSERT Names(FName) VALUES('Scott') 
INSERT Names(FName) VALUES('Jess') 
INSERT Names(FName) VALUES('Drew') 
INSERT Names(FName) VALUES('Katherine') 
INSERT Names(FName) VALUES('Paul') 
GO 
 
-- solution #1: nested top 
 
SELECT TOP 1 FName 
FROM 
( 
    SELECT TOP 10 FName 
    FROM Names 
    ORDER BY FName 
) sub 
ORDER BY FName DESC 
 
-- solution #2: NOT IN 
 
SELECT TOP 1 FName  
FROM Names WHERE FName NOT IN 
( 
    SELECT TOP 9 FName 
    FROM Names 
    ORDER BY FName 
) 
ORDER BY FName 
 
-- solution #3: derived count 
-- this assumes FName is unique 
 
SELECT FName 
    FROM Names 
    WHERE  
    ( 
        SELECT COUNT(*) 
        FROM Names n2 
        WHERE n2.FName <= Names.FName 
    ) = 10 
 
-- solution #4: MAX 
 
SELECT FName = MAX(FName) FROM  
( 
    SELECT TOP 10 FName 
    FROM Names 
    ORDER BY FName 
) sub 
 
-- solution #5: relative fetch from cursor 
 
-- yes, cursors are generally evil, but 
-- sometimes you might be surprised 
 
DECLARE FNames CURSOR 
    LOCAL STATIC READ_ONLY FOR 
    SELECT FName 
        FROM Names 
        ORDER BY FName 
 
DECLARE @FName VARCHAR(32) 
 
OPEN FNames 
 
FETCH RELATIVE 10 FROM FNames INTO @FName 
 
CLOSE FNames 
DEALLOCATE FNames 
 
SELECT FName = @FName 
 
DROP TABLE Names 
GO
 
Is This Answer Correct ?    1 Yes 1 No
Rakesh Kumar Jaiswal
 
  Re: how to select 5 to 7 rows from a table, which contains 10 rows?
Answer
# 8
select * from (select row_number() over (order by empid) as 
row, *
from employee)a
where a.row between 4 and 5
 
Is This Answer Correct ?    4 Yes 2 No
Smitha
 
  Re: how to select 5 to 7 rows from a table, which contains 10 rows?
Answer
# 9
If you knowfor sure that the number of rows is 10 then the 
above answers given above a great examples.  So what if you 
don't know the total number of rows in that table?  Can you 
use Select count(*) from nameoftable;
 
Is This Answer Correct ?    0 Yes 0 No
John T
 
  Re: how to select 5 to 7 rows from a table, which contains 10 rows?
Answer
# 10
select * from emp where sno between 5 and 7









ajay.thomala@gmail.com
 
Is This Answer Correct ?    1 Yes 0 No
Ajay
 
  Re: how to select 5 to 7 rows from a table, which contains 10 rows?
Answer
# 11
i have other idea if there is no duplicate record in the 
table you can use this


select top 7 *from employee
except
select top 4 *from employee
 
Is This Answer Correct ?    0 Yes 0 No
Monty
[Reliance]
 
  Re: how to select 5 to 7 rows from a table, which contains 10 rows?
Answer
# 12
Please ignore my above answer

select * from tablename where sno between 4 and 8


Ajay
 
Is This Answer Correct ?    0 Yes 0 No
Ajay (esd)
 
  Re: how to select 5 to 7 rows from a table, which contains 10 rows?
Answer
# 13
select *  from ( select rownum r, comp_name from metatest )
where r >  4 and  r < 8;
 
Is This Answer Correct ?    0 Yes 0 No
S. Ramesh
 
  Re: how to select 5 to 7 rows from a table, which contains 10 rows?
Answer
# 14
select top 3* from Tablename 
where columnname in ( select top 7columnname from table 
name order by columnname desc )
and  columnname not in( select top 4columnname from 
tablename order by columnname desc )
order by  columnname desc
 
Is This Answer Correct ?    0 Yes 0 No
Purabi Roy(sarkar)
 

 
 
 
Other SQL Server Interview Questions
 
  Question Asked @ Answers
 
what is a stored procedure and trigger?  1
What is referential integrity and how is it achieved? Adea-Solutions1
How to Get the last identity value used  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 is maximum size of temp db? iSoft4
Can you explain the types of Joins that we can have with Sql Server?  2
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
what is the Surrogate key?and wt is the diff between Primary key and Surrogate Key?  2
what is IDE,DMV in sql server? Value-Labs1
How do you find the number of rows in a table?  5
i want only duplicates rows from coloumn ex. emp_id(colomn name)1,1,2,3,3,4,5,5. so i want only duplicates no. iFlex2
how can u select the Distinct values in the table, table having 20 columns , i want all columns  1
Difference between Triggers and Stored Procedure Protech9
How to select Distinct columns from the table, table having 20 columns and i want all coulmns Wipro3
Rate yourself in .NET and SQL ? Cognizent1
how to get the rowid/rownumbes of a table in sqlserver IBM5
What is INTVAL( )and where we use Plz any body help me  1
how to get the automatic backup of the database in the sql server  3
Can we rewrite subqueries into simple select statements or with joins? Example? ASD1
Hi Friends, I have a table in which there are thousands of records and in city field there is NULL value for all records now i want to change that null value with distinct values in each record say delhi, bihar, agra, jaipur etc, what will be the query for that????? its not possible to update thousands of records one by one. is there any alternative ...? Plz help ... its urgent Thanx in advance  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