There is no default ORDER BY value in MySQL. You need to specify ORDER BY clause explicitly. Following is the syntax −
ORDER BY ASC; OR ORDER BY DESC;
Let us first create a table −
mysql> create table DemoTable -> ( -> StudentName varchar(100) -> ); Query OK, 0 rows affected (0.82 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable values('Sam'); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable values('Chris'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values('David'); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable values('Bob'); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable values('Robert'); Query OK, 1 row affected (0.25 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
Output
This will produce the following output −
+-------------+ | StudentName | +-------------+ | Sam | | Chris | | David | | Bob | | Robert | +-------------+ 5 rows in set (0.00 sec)
Case 1 − If you want the result in ascending order −
mysql> select *from DemoTable order by StudentName asc;
Output
This will produce the following output −
+-------------+ | StudentName | +-------------+ | Bob | | Chris | | David | | Robert | | Sam | +-------------+ 5 rows in set (0.00 sec)
Case 2 − If you want the result in descending order −
mysql> select *from DemoTable order by StudentName desc;
Output
This will produce the following output −
+-------------+ | StudentName | +-------------+ | Sam | | Robert | | David | | Chris | | Bob | +-------------+ 5 rows in set (0.00 sec)