8

I would like to know if I could using select statement retrieve exact position of the rows. e.g rows between 235 & 250. Is this possible?

Thanks in advance, shashi

Cedric H.
  • 7,980
  • 10
  • 55
  • 82
user414977
  • 265
  • 3
  • 8
  • 24
  • 4
    Yes, this is possible. I'll tell you exactly how after you accept answers for your former questions. –  Sep 03 '10 at 12:37
  • 1
    What SQL language? Also what is the purpose? if you are doing that because you are showing a subset of results for multi-page viewing? – greektreat Sep 03 '10 at 12:38

8 Answers8

28

I don't know of a general way.. but each DB has a way. For example in oracle you can do it with a nested select

Oracle:

select * from (
select a, b, c from table_foo
where id = 124
)
WHERE rownum >= 235
and ROWNUM <= 250

MSSQL

select * from 
    (select Row_Number() over 
     (order by userID) as RowIndex, * from users) as Sub
    Where Sub.RowIndex >= 235 and Sub.RowIndex <= 250

MySQL

SELECT * FROM TableName LIMIT 235, 15
bwawok
  • 14,898
  • 7
  • 32
  • 43
  • 1
    Am I going daft, or does >= 235 and <= 250 still mean 16 rows, not 15? – Marjan Venema Sep 03 '10 at 13:47
  • The below code doesn't work, the while loop doesn't dispaly any values. If I change it to **0** to 150 it works fine. Please help me out here. Anything other than **0** doesn't retrieve any value. ResultSet rset1 = stmt.executeQuery(" SELECT * FROM (SELECT * FROM iris ) WHERE rownum BETWEEN 10 and 150"); while(rset1.next()) { System.out.println(rset1.getString(1)); } – user414977 Sep 05 '10 at 18:16
  • 1
    @bwawok How would you say from 235 and up in MySQL?LIMIT 235, INFINITE? – J2N Jan 09 '13 at 21:10
  • @JLaw - You could do: Limit 235, 999999999999999. Works fine up to 999999999999999 rows, not so good after that.. but there should be some sane bound. – bwawok Jan 10 '13 at 20:02
3

This can be done very easily in SQL Server 2012. By using the new feature of OFFSET and FETCH. This will help you to pull out desired rows in an already ordered/sorted result set.

Please see the below example:

SELECT
             PP.FirstName + ' ' + PP.LastName AS 'Name'
            ,PA.City
            ,PA.PostalCode
FROM  Person.Address PA
            INNER JOIN
                        Person.BusinessEntityAddress PBEA
                                    ON PA.AddressID = PBEA.AddressID 
            INNER JOIN
                       Person.Person PP
                                    ON PBEA.BusinessEntityID = PP.BusinessEntityID
ORDER BY PP.FirstName
            OFFSET 0 ROWS
                FETCH NEXT 5 ROWS ONLY

Please notice the OFFSET 0 and FETCH NEXT 5 written above.
This is will display only 5 rows starting from 0 the row.

sKhan
  • 9,694
  • 16
  • 55
  • 53
3

If your using mySQL you could use the limit command for example:

SELECT * FROM TableName LIMIT 235, 15

Where the first number is the start index and the second is the number of rows to return.

Malachi
  • 33,142
  • 18
  • 63
  • 96
3

No, that database is set not a sequence, this mean that You the don't have any specific order.

But when specify the order than everything is much simpler.

Oracle

SELECT * FROM ( SELECT * FROM TABLE ORDER BY COLUMN ) WHERE rownum BETWEEN 235 and 250

In this case You have to use rownum

rownum is a pseudo column. It numbers the records in a result set. The first record that meets the where criteria in a select statement is given rownum=1, and every subsequent record meeting that same criteria increases rownum.

MS SQL

WITH OrderedRecords AS
(
    SELECT ColumnA, 
    ROW_NUMBER() OVER (ORDER BY ColumnA) AS 'RowNumber'
    FROM Sales.SalesOrderHeader 
) 
SELECT * FROM OrderedRecords WHERE RowNumber BETWEEN 235 and 250
GO

For this You have to specify You own order column


For MySQL i don't know how the engine deal with this.

  • 1
    What do you think an order by clause does? – bwawok Sep 03 '10 at 12:42
  • Mainly specify the order but You probably refer to how it works; it depend of the db engine, but generally we can say that return and cursor that retrieve the data in specific order, that why in oracle we have to use another select, and in MS SQL we need to specify for our won an row now that oder the set first. This is always "used" last by the database (that why we can use there aliases). – Damian Leszczyński - Vash Sep 03 '10 at 12:53
  • @bwawok its critical to order especially by id column sorted in alphabetical order for example. – webs Aug 17 '22 at 01:55
3

In SQL Server,

select * from tablename order by columnname offset 20 rows fetch next 40 rows only

It treats 21st row as 1st row and fetches next 40 rows from a 21st row.

Nitika Chopra
  • 1,281
  • 17
  • 22
3

We can do this by multiple way.

  1. we can do with the help of offset-fetch clause.

    select * from Table_Name order by Column_Name offset 234 rows fetch next 16 rows only

it will fetch the record between 235-250. because it will skip first 234 rows and will fetch next 16 rows.

  1. we can use simple select statement with where clause.

    Select * from Table_Name where Column_Name Between 235 and 250

it will also fetch same result.

Hope it will help.

Nitika Chopra
  • 1,281
  • 17
  • 22
0

If you're using Microsoft SQL (2005>) you can use the ROW_NUMBER function

USE AdventureWorks;
GO
WITH OrderedOrders AS
(
    SELECT SalesOrderID, OrderDate,
    ROW_NUMBER() OVER (ORDER BY OrderDate) AS 'RowNumber'
    FROM Sales.SalesOrderHeader 
) 
SELECT * 
FROM OrderedOrders 
WHERE RowNumber BETWEEN 50 AND 60;
BenMorel
  • 34,448
  • 50
  • 182
  • 322
Christopher Klein
  • 2,773
  • 4
  • 39
  • 61
0

Following worked from me in oracle

select * from (select rownum serial,sp.* from sample_table st) sam 
where sam.serial > 10 and sam.serial <= 20;
Dileepa
  • 1,019
  • 1
  • 15
  • 40