0

I am using the command

SELECT * FROM table LIMIT 0, 10

to retrieve the firs 10 rows of the table.

Is there a way to do the same thing but with the last 10 rows?

Thanks in advance.

Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786
Ryan
  • 105
  • 4
  • 14
  • 4
    without an `ORDER BY`, your current query isn't necessarily returning the "first 10 rows of the table" – Lamak Jun 01 '17 at 19:56
  • 1
    A query such as you have provided does not necessarily get the first ten rows of a table; it get the first tens rows of the query results. Without a precise enough ORDER BY, LIMIT is not deterministic. – Uueerdo Jun 01 '17 at 20:16

1 Answers1

6

SQL tables represent unordered sets. Your query gets 10 arbitrary rows, which are generally the first 10 rows inserted into the table. But, you should not depend on this ad-hoc functionality.

If you have an auto-incremented id, then you get the first ten rows by doing:

select t.*
from t
order by id
limit 10;

To get the last ten rows, use order by id desc.

Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786