0

I have a MySQL table with columns id, name and active. I need to write a SQL select statement that will return a subset of the id's from this table starting at a specific row number, returning the next 20 rows inclusive. For example:

id   name   active
------------------
a    One    true
b    Two    true
c    Three  true
d    Four   false
e    Five   true
f    Six    false

I need to be able to say start at row 3 in the table and return the id's for the next 3 rows inclusive (c, d and e).

Thanks in advance!

MeanwhileInHell
  • 6,780
  • 17
  • 57
  • 106

2 Answers2

3

Use LIMIT:

SELECT 
  *
FROM 
  MyTable
Limit 2,3
CristiC
  • 22,068
  • 12
  • 57
  • 89
1

Generally, the syntax is:

SELECT ... LIMIT <start>, <length>

So in your case:

SELECT ... LIMIT 2, 3
Jimmy Sawczuk
  • 13,488
  • 7
  • 46
  • 60
  • I've not seen that syntax before, is it specific to MySQL or will it work in MS Sql Server? – TabbyCool May 25 '11 at 13:44
  • 1
    I [don't believe MS SQL has `LIMIT`](http://stackoverflow.com/questions/603724/how-to-implement-limit-with-microsoft-sql-server), I think it has `TOP` instead. – Jimmy Sawczuk May 25 '11 at 13:47