O'Reilly logo

SQL: Visual QuickStart Guide by Chris Fehily

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

Sorting Rows with ORDER BY

Rows in a query result are unordered; you should view the order in which rows appear as being arbitrary. This situation arises because the relational model posits that row order is irrelevant for table operations. You can use the ORDER BY clause to sort rows by a specified column or columns in ascending (lowest to highest) or descending (highest to lowest) order; see the sidebar in this section. The ORDER BY clause always is the last clause in a SELECT statement.

To sort by a column:

  • Type:
    SELECT columns
      FROM table
      ORDER BY sort_column [ASC | DESC];
    
    columns is one or more comma-separated column names, sort_column is the name of the column on which to sort the result, and table is the name of the table that contains ...

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