3

If table Scores looks like this:

_id | score
---------
  1 | 1,000
  2 | 2,000
  3 | 3,000
  4 | 4,000
  5 |    -1
  6 |    -1
  7 |    -1

Will the following query always return the rows in _id ascending order?

SELECT * FROM Scores

Also, will the following query always return the first ordered occurrence of _id (that is, 5)?

SELECT _id FROM Scores WHERE Score = -1 LIMIT 0, 1

The key here is ALWAYS. I have tested it and it works as intended but I want to verify this outcome is guaranteed and that an order by clause is not needed in these cases.

Thank you all.

2 Answers2

1

One of the principles of the databases is that you can not suppose that your registers are ordered in any way, as the internal implementation of SQLite may differ between devices. You should use the operator order by to assure a certain order.

zozelfelfo
  • 3,776
  • 2
  • 21
  • 35
1

By default, the rows are logically stored in order of increasing rowid.

And if your select stament does not include an order by, or the table has no index, the default sorting order will always be the primary key column, which is the _ID column in this case

Read more here

NeedAnswers
  • 1,411
  • 3
  • 19
  • 43