PHPGurukul

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;

Anuj Kumar

This is Anuj Kumar. I’m a professional web developer with 4+ year 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.
.

Recommended Tutorials for you


Add comment

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

Web Hosting with a FREE domain. On sale ₹99.00/mo

Follow us

Don't be shy, get in touch. We love meeting interesting people and making new friends.

Most popular


Most discussed