MySQL OrderBy

MySQL orderby clause is used to order results based on a column. Orderby clause is used in select queries to do sort by specifying the column order (asc, desc). If no order is given, then asc is the default order. Syntax,

SELECT * FROM table_name ORDER BY column_name desc

Using Orderby clause we can get ordered results based on multiple columns.

mysql_order_by

Example: Sorting MySQL Table Records using ORDER BY

This is the student table containing student_name, student_email and applied_date columns.

mysql_student_table

First we are going to write a query to order this table data by using single column student_name in decending order. The query is like,

SELECT * FROM students ORDER BY student_name DESC

After running this query, the students table results will be like,

mysql_order_by_single_column

Next, we are writing a query with order by two columns applied_date and student_name . The query is,

SELECT * FROM students ORDER BY applied_date DESC, student_name DESC

This query will sort students records in descending order. If any number if these records has same value for applied_date, then, these records will be sorted with the student_name column in descending order. Now, the result is,

mysql_order_by_more_column

This MySQL code tutorial was published on July 16, 2013.

↑ Back to Top