In InnoDB, strings are stored in primary key order. If you use LIMIT without ORDER BY , you will always get the rows with the smallest primary key values, even if you inserted them in random order.
create table foo (id int primary key, x char(1), y int) engine=InnoDB; insert into foo values (5, 'A', 123); insert into foo values (9, 'B', 234); insert into foo values (2, 'C', 345); insert into foo values (4, 'D', 456); insert into foo values (1, 'E', 567); select * from foo; +
In MyISAM, strings are saved wherever they fit. Initially, this means that lines are added to the data file, but as you delete lines and insert new ones, spaces left by deleted lines will be reused by new lines.
create table bar (id int primary key, x char(1), y int) engine=MyISAM; insert into bar values (1, 'A', 123); insert into bar values (2, 'B', 234); insert into bar values (3, 'C', 345); insert into bar values (4, 'D', 456); insert into bar values (5, 'E', 567); select * from bar; +
Another exception, if you use InnoDB, is that you retrieve rows from the secondary index instead of the primary index. This happens when you see the "Using Index" note on the output of EXPLAIN.
alter table foo add index (x); select id, x from foo; +
If you have more complex queries with joins, this becomes even more complicated because you get the rows returned by the default order of the first available table (where the βfirstβ depends on the optimizer choosing the order of the tables), then the rows from the joined table will depend on row order from the previous table.
select straight_join foo.*, bar.* from bar join foo on bar.x=foo.x; +----+------+------+----+------+------+ | id | x | y | id | x | y | +----+------+------+----+------+------+ | 1 | E | 567 | 5 | E | 567 | | 5 | A | 123 | 1 | A | 123 | | 9 | B | 234 | 2 | B | 234 | +----+------+------+----+------+------+ select straight_join foo.*, bar.* from foo join bar on bar.x=foo.x; +----+------+------+----+------+------+ | id | x | y | id | x | y | +----+------+------+----+------+------+ | 5 | A | 123 | 1 | A | 123 | | 9 | B | 234 | 2 | B | 234 | | 1 | E | 567 | 5 | E | 567 | +----+------+------+----+------+------+
The bottom line is that it's best to be explicit: when you use LIMIT , specify ORDER BY .