I have a table in my database.
+--------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------+-------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| rollno | int(11) | NO | | NULL | |
| name | varchar(20) | NO | | NULL | |
| marks | int(11) | NO | | NULL | |
+--------+-------------+------+-----+---------+----------------+
By default if I query
select * from students;
Shows result sorted by id INT (auto-increment).
+----+--------+------------+-------+
| id | rollno | name | marks |
+----+--------+------------+-------+
| 1 | 65 | John Doe | 89 |
| 2 | 62 | John Skeet | 76 |
| 3 | 33 | Mike Ross | 78 |
+----+--------+------------+-------+
3 rows in set (0.00 sec)
I want to change default sorting behaviour and make rollno the default sorting field, how do I do this?
ORDER BYonto your query.ALTER TABLE students ORDER BY rollno ASC;after every insert/update, but you would take a huge performance hit as opposed to just sorting the results of aselect. Which is more expensive, sorting an entire table, or sorting only the result of aselectstatement? What is stopping you from just addingorder byto yourselectstatement?