Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I select from a specific number of rows?

In a SQL query, how do I select a specific number of rows from row number 10 to 50, for example.

SELECT top 15000 [ID].... 

will get the first 15000 rows, but what would I do if I wanted to get the next 15000?

like image 692
xarzu Avatar asked Dec 16 '22 06:12

xarzu


2 Answers

The syntax for MySQL would be

SELECT * FROM table LIMIT numberOfRowsToSkip, numberOfRowsToSelect

So in your case:

SELECT * FROM table LIMIT 9, 41; --selects from row no. 10 to no. 50

SELECT * FROM table LIMIT 15000, 15000; --selects from 15001st row, next 15000 rows

For reference visit MySQL SELECT documentation. Philippe provides an alternate syntax to this.

For SQL Server, see this.

like image 134
nawfal Avatar answered Jan 15 '23 19:01

nawfal


SELECT *
FROM
(
SELECT 
ROW_NUMBER() OVER (ORDER BY ID),
*
FROM table
)  
WHERE ROW_NUMBER() between [start_range] and [end_range]
like image 22
user172839 Avatar answered Jan 15 '23 18:01

user172839