I have a table in my database.
+--------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra
Run this
ALTER TABLE students ORDER BY rollno ASC;
select * from students order by rollno asc;
will return your results sorted by that column. It should be noted that there is no default sorting behavior as far as data is actually stored in the database (aside from identities and indexes); you should never depend on your results being sorted a certain way unless you explicitly sort them (using order by
).
The DB returns the data in the fastest way possible. If this happen to be the order in which it is stored or a key is defined then this is up to the system. You can't rely on that.
Think about it: Why would the DB use performace to order something by default if you don't need it ordered. DBs are optimised for speed.
If you want it being ordered then you have to specify that in an order by
clause.