MySQL Order By - MySQL - Phptpoint.com

MySQL Order By

/ins>

How to use MySQL Order By

The ORDER BY is a keyword which is used to sort the results based on one or more columns.
by default it sorts the result set in ascending order . if we want to sort the results in descending order we use DESC keyword. for ascending order we use ASC keyword.

Syntax

Retrieve all records from empInfo table in ascending order.

Output :

Emp_id Name Email Mobile
1 devesh devesh@gmail.com 9910099100
2 deepak deepak@gmail.com 9210053520
3 ravi ravi@gmail.com 9810098100

In the above example there is a empInfo table the data is retrived in ascending order based on emp_id
the fetched data is stored in the val variable and results are displayed in tabular form.

Retrieve all records from empInfo table in descending order.

Output :

Emp_id Name Email Mobile
3 ravi ravi@gmail.com 9810098100
2 deepak deepak@gmail.com 9210053520
1 devesh devesh@gmail.com 9910099100

In the above example first we create connection with the database and data is retrived in descending order by using DESC keyword the fetched data is stored in the val variable and results are displayed in tabular form.