MySQL ORDER BY Clause – Sort Data in Descending Order

How to Use the ORDER BY Clause in MySQL with Examples

In this tutorial, you will learn to retrieve rows of a table sorted in ascending or descending order from a MySQL database.

To get the rows sorted in ascending or descending order, we use MySQL ORDER BY clause along with a SELECT statement.

The syntax of ORDER BY clause is as follows:

SELECT column1,column2,column3,...,columnN FROM table ORDER BY column1, column2,... ASC/DESC;

If you do not specify ASC or DESC in the ORDER BY clause, MySQL will return rows sorted in ascending order by default. So, if you want to get the rows sorted in descending order, specify the DESC keyword.

ORDER BY Clause Examples

Demo data:

Suppose you have a table named orders, and it consists of 5 columns: order_date, order_number, customer, amount, and salesperson, as shown below.

order_dateorder_numbercustomeramountsalesperson
11/7/2019#200001Erick Little1600.50Lisa Ramsey
11/7/2019#200002Ronald Houston1050.50William Barnett
11/7/2019#200003Sandy Howard480.00Iris Wilkerson
11/8/2019#200004Ronald Houston440.00Lisa Ramsey
11/8/2019#200005Nicole Morton570.00Lisa Ramsey
11/9/2019#200006Ronald Houston850.00William Barnett
11/9/2019#200007Gloria Harvey420.00Iris Wilkerson
11/10/2019#200008Margie Fowler230.00Iris Wilkerson
12/2/2019#200009Sandy Howard130.00Iris Wilkerson
12/2/2019#200010Margie Fowler285.00Lisa Ramsey
12/3/2019#200011Erick Little550.50William Barnett
12/4/2019#200012Ronald Houston340.50William Barnett
12/6/2019#200013Nicole Morton315.50Lisa Ramsey
12/8/2019#200014Erick Little460.00William Barnett

Example 1: ORDER BY a single column

The following SQL query retrieves all rows of the orders table sorted by the order_date column:

SELECT * FROM orders ORDER BY order_date;

Since ASC or DESC is not specified, MySQL will return a result set sorted in ascending order by default.

To get the rows of the order table and sort them in descending order, write an SQL query like this:

SELECT * FROM orders ORDER BY order_date DESC;

Example 2: ORDER BY multiple columns

You can also use the ORDER BY clause to sort rows in ascending or descending order by multiple columns.

Here is an example of retrieving all rows from the orders table and sorting them by the customer and order_date columns:

SELECT * FROM orders ORDER BY customer, order_date.

This SQL query retrieves all rows from the order table, sorted in ascending order by the customer column and the order_date column.

To get all the rows sorted in descending order by both columns from the orders table, write a SQL query like this:

SELECT * FROM orders ORDER BY customer DESC, order_date DESC.

You can also retrieve rows and sort them in ascending and then descending order by multiple columns, as shown in the following example:

SELECT * FROM orders ORDER BY customer ASC, order_date DESC

Example 3: ORDER BY and WHERE clause

You can also use the ORDER BY clause together with a WHERE clause.

The syntax for the ORDER BY and WHERE clauses is as follows:

SELECT column1, column2... FROM table WHERE where_conditions ORDER BY column1, column2 ASC/DESC;

Here is an example:

SELECT * FROM orders WHERE customer="Erick Little" ORDER BY order_date DESC;

This SQL query retrieves rows where the customer is Erick Little, sorted in descending order by the order_date column.

Summary

In this tutorial, you have learned how to use the MySQL ORDER BY clause with a SELECT statement.

You can use the ORDER BY clause to retrieve rows from a table sorted in ascending or descending order by one or more columns.


See also:
MySQL LIKE Operator Pattern Matching and Examples
MySQL SUBSTRING_INDEX Function with Examples
MySQL EXISTS Operator with Examples
MySQL ROW_NUMBER Function with Examples
MySQL CONCAT() Function | Concatenate Strings in MySQL

Leave a Comment