Documentation Home
MySQL Tutorial
Related Documentation Download this Excerpt
PDF (US Ltr) - 199.2Kb
PDF (A4) - 200.1Kb


4.4.4 Sorting Rows

You may have noticed in the preceding examples that the result rows are displayed in no particular order. It is often easier to examine query output when the rows are sorted in some meaningful way. To sort a result, use anORDER BY clause.

Here are animal birthdays, sorted by date:

mysql> SELECT name, birth FROM pet ORDER BY birth;+----------+------------+| name     | birth      |+----------+------------+| Buffy    | 1989-05-13 || Bowser   | 1989-08-31 || Fang     | 1990-08-27 || Fluffy   | 1993-02-04 || Claws    | 1994-03-17 || Slim     | 1996-04-29 || Whistler | 1997-12-09 || Chirpy   | 1998-09-11 || Puffball | 1999-03-30 |+----------+------------+

On character type columns, sorting—like all other comparison operations—is normally performed in a case-insensitive fashion. This means that the order is undefined for columns that are identical except for their case. You can force a case-sensitive sort for a column by usingBINARY like so:ORDER BY BINARYcol_name.

The default sort order is ascending, with smallest values first. To sort in reverse (descending) order, add theDESC keyword to the name of the column you are sorting by:

mysql> SELECT name, birth FROM pet ORDER BY birth DESC;+----------+------------+| name     | birth      |+----------+------------+| Puffball | 1999-03-30 || Chirpy   | 1998-09-11 || Whistler | 1997-12-09 || Slim     | 1996-04-29 || Claws    | 1994-03-17 || Fluffy   | 1993-02-04 || Fang     | 1990-08-27 || Bowser   | 1989-08-31 || Buffy    | 1989-05-13 |+----------+------------+

You can sort on multiple columns, and you can sort different columns in different directions. For example, to sort by type of animal in ascending order, then by birth date within animal type in descending order (youngest animals first), use the following query:

mysql> SELECT name, species, birth FROM pet       ORDER BY species, birth DESC;+----------+---------+------------+| name     | species | birth      |+----------+---------+------------+| Chirpy   | bird    | 1998-09-11 || Whistler | bird    | 1997-12-09 || Claws    | cat     | 1994-03-17 || Fluffy   | cat     | 1993-02-04 || Fang     | dog     | 1990-08-27 || Bowser   | dog     | 1989-08-31 || Buffy    | dog     | 1989-05-13 || Puffball | hamster | 1999-03-30 || Slim     | snake   | 1996-04-29 |+----------+---------+------------+

TheDESC keyword applies only to the column name immediately preceding it (birth); it does not affect thespecies column sort order.