Sql select * between exact number of rows

I would like to know if I can use the select statement to get the exact position of the rows. for example, lines between 235 and 250. Is this possible?

Thanks in advance, Shashi

+5
source share
8 answers

I don’t know a common way ... but every DB has a way. For example, in oracle you can do this 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
+24
source

If you use mySQL, you can use the limit command, for example:

SELECT * FROM TableName LIMIT 235, 15

Where the first number is the starting index and the second is the number of rows returned.

+3
source

SQL Server 2012. OFFSET FETCH. / .

, :

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

OFFSET 0 and FETCH NEXT 5, .
5 , 0 .

+3

, , , .

, .

Oracle

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

rownum

rownum - . . , select rownum = 1, 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


MySQL , .

+2

SQL Server

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

21- 1- 40 21- .

+1

.

  1. offset-fetch.

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

235-250. 234 16 .

  1. select where.

    Select * from Table_Name where Column_Name Between 235 and 250

.

, .

+1

Microsoft SQL (2005 > ), ROW_NUMBER

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;
0

select * from (select rownum serial,sp.* from sample_table st) sam 
where sam.serial > 10 and sam.serial <= 20;
0

Source: https://habr.com/ru/post/1763067/


All Articles