The ORDER BY clause: Ordering results

Unless otherwise requested, the database server returns the rows of a table in an order that has no meaning. Often it is useful to look at the rows in a table in a more meaningful sequence. For example, you might like to see products in alphabetical order.

You order the rows in a result set by adding an ORDER BY clause to the end of the SELECT statement. This SELECT statement has the following syntax:

SELECT column-name-1, column-name-2,...
FROM table-name
ORDER BY order-by-column-name

You must replace column-name-1, column-name-2, and table-name with the names of the columns and table you are querying, and order-by-column-name with a column in the table. As before, you can use the asterisk as a short form for all the columns in the table.

 List the products in alphabetical order
  • In Interactive SQL, execute the following query:

    SELECT ID, Name, Description
       FROM Products
       ORDER BY Name;
    ID Name Description
    400 Baseball Cap Cotton Cap
    401 Baseball Cap Wool cap
    700 Shorts Cotton Shorts
    600 Sweatshirt Hooded Sweatshirt
    ... ... ...
 Notes

Indexes that improve ORDER BY performance