Mysql

MySQL Order By

The data return by a SELECT query can be sorted into a specified order using ORDER BY clause.

Syntax:

SELECT column-name FROM table-name ORDER BY column-name;

When the ORDER BY keywords are followed by a table column name the retrieved data will be sorted into order based upon the type of data in the specified column. Typically, if the column data type is numerical, the retrieved data will be sorted in ascending numerical order. If the column data type is textual the retrieved data will be sorted into alphabetical order from A to Z.

Ex: MySQL Table tblemployee

idEmp_NameEmp_CodeEmp_Email
1Anuj Kumar10806121phpgurukulofficial@gmail.com
2Sanjeev Kumar1908763sanjv @test.com
3Jonh Doe10899781jonhdie@abc.com
SELECT  *  FROM  tblemployee ORDER BY id;

Output:

idEmp_NameEmp_CodeEmp_Email
1Anuj Kumar10806121phpgurukulofficial@gmail.com
2Sanjeev Kumar1908763sanjv @test.com
3Jonh Doe10899781jonhdie@abc.com
SELECT  *  FROM  tblemployee ORDER BY Emp_Name;

Output:

idEmp_NameEmp_CodeEmp_Email
1Anuj Kumar10806121phpgurukulofficial@gmail.com
3Jonh Doe10899781jonhdie@abc.com
2Sanjeev Kumar1908763sanjv @test.com

For viewing data in descending sorted order the word DESC must be mentioned after the column name and before the semi colon in the ORDER BY clause. In case there is no mention of the sort order, data will be sorted in ascending order by default.

Syntax:

SELECT * FROM table_name ORDER BY column-name SORT ORDER;

Recommended Tutorials for you


Tags

Anuj kumar

This is Anuj Kumar. I’m a professional web developer with 5+ years of experience. I write blogs in my free time. I love to learn new technologies and share with others. I founded PHPGurukul in September 2015. The main aim of this website to provide PHP, Jquery, MySQL, PHP Oops and other web development tutorials. .

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Check Also
Close
Back to top button
Close
Close