MySQL

Ascending and Descending Sorting Data

MySQL ORDER BY clause sort data in ascending order and it is the default sort order. However, data sorting is not limited to ascending sort orders (from A to Z). The ORDER BY clause can also be used to sort in descending order (from Z to A). To sort by descending order, the keyword DESC must be specified.

The following example sorts the products by price in descending order:

SELECT prod_id, prod_name, prod_price
FROM products
ORDER BY prod_price DESC;

The above statement displayed the following output:

+---------+------------+------------+
| prod_id | prod_name  | prod_price |
+---------+------------+------------+
| 9       | product 9  |      20.00 |
| 4       | product 4  |      13.99 |
| 11      | product 11 |      10.25 |
| 5       | product 5  |       9.09 |
| 3       | product 3  |       8.99 |
| 6       | product 6  |       8.99 |
| 7       | product 7  |       7.00 |
| 1       | product 1  |       5.99 |
| 10      | product 10 |       4.75 |
| 2       | product 2  |       3.99 |
| 8       | product 8  |       1.80 |
+---------+------------+------------+

But what if you were to sort by multiple columns? The following example sorts the products in descending order (most expensive first), plus product name:

SELECT prod_id, prod_name, prod_price
FROM products
ORDER BY prod_price DESC, prod_name;

The above statement displayed the following output:

+---------+------------+------------+
| prod_id | prod_name  | prod_price |
+---------+------------+------------+
| 9       | product 9  |      20.00 |
| 4       | product 4  |      13.99 |
| 11      | product 11 |      10.25 |
| 5       | product 5  |       9.09 |
| 3       | product 3  |       8.99 |
| 6       | product 6  |       8.99 |
| 7       | product 7  |       7.00 |
| 1       | product 1  |       5.99 |
| 10      | product 10 |       4.75 |
| 2       | product 2  |       3.99 |
| 8       | product 8  |       1.80 |
+---------+------------+------------+

The DESC keyword only applies to the column name that directly precedes it. In the previous example, DESC was specified for the prod_price column, but not for the prod_name column. Therefore, the prod_price column is sorted in descending order, but the prod_name column (within each price) is still sorted in standard ascending order.

Sorting Descending on Multiple Columns If you want to sort descending on multiple columns, be sure each column has its own DESC keyword.

The opposite of DESC is ASC (for ascending), which may be specified to sort in ascending order. In practice, however, ASC is not usually used because ascending order is the default sequence (and is assumed if neither ASC nor DESC are specified).

Case Sensitivity and Sort Orders When you are sorting textual data, is A the same as a? And does a come before B or after Z? These are not theoretical questions, and the answers depend on how the database is set up.

In dictionary sort order, A is treated the same as a, and that is the default behavior in MySQL (and indeed most DBMSs). However, administrators can change this behavior if needed. (If your database contains lots of foreign language characters, this might become necessary.)

Using a combination of ORDER BY and LIMIT, it is possible to find the highest or lowest value in a column. The following example demonstrates how to find the value of the most expensive item:

SELECT prod_price
FROM products
ORDER BY prod_price DESC
LIMIT 1;

The above statement displayed the following output:

+------------+
| prod_price |
+------------+
|      20.00 |
+------------+

prod_price DESC ensures that rows are retrieved from most to least expensive, and LIMIT 1 tells MySQL to just return one row.

Position of ORDER BY Clause When specifying an ORDER BY clause, be sure that it is after the FROM clause. If LIMIT is used, it must come after ORDER BY. Using clauses out of order will generate an error message.

by BrainBellupdated
Advertisement: