O'Reilly logo

Sams Teach Yourself MySQL by Chris Newman

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

The ORDER BY Clause

So far, you have seen that data is fetched from the database in no particular order. To specify the sorting on the result of a query, you can add an ORDER BY clause.

Sorting on a Single Column

The following example retrieves all the products in order of price. The keywords ORDER BY are followed by the name of the column on which you want to sort.

mysql> SELECT *
    -> FROM products
    -> ORDER BY price;
+------+----------------+--------+-------+
| code | name           | weight | price |
+------+----------------+--------+-------+
| MINI | Small product  |   1.50 |  5.99 |
| MIDI | Medium product |   4.50 |  9.99 |
| MAXI | Large product  |   8.00 | 15.99 |
+------+----------------+--------+-------+
3 rows in set (0.00 sec)

Sort Columns

The sort ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required