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 ...

Get SQL: Visual QuickStart Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.